blob: 3202462109f509c1f9eeebc2e45ef1fd678e83d9 [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;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200387
388 struct list_head tasks;
389 struct list_head *balance_iterator;
390
391 /*
392 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200393 * It is set to NULL otherwise (i.e when none are currently running).
394 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100395 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200396
397 unsigned long nr_spread_over;
398
Ingo Molnar62160e32007-10-15 17:00:03 +0200399#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200400 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
401
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100402 /*
403 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200404 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
405 * (like users, containers etc.)
406 *
407 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
408 * list is used during load balance.
409 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100410 struct list_head leaf_cfs_rq_list;
411 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200412
413#ifdef CONFIG_SMP
414 unsigned long task_weight;
415 unsigned long shares;
416 /*
417 * We need space to build a sched_domain wide view of the full task
418 * group tree, in order to avoid depending on dynamic memory allocation
419 * during the load balancing we place this in the per cpu task group
420 * hierarchy. This limits the load balancing to one instance per cpu,
421 * but more should not be needed anyway.
422 */
423 struct aggregate_struct {
424 /*
425 * load = weight(cpus) * f(tg)
426 *
427 * Where f(tg) is the recursive weight fraction assigned to
428 * this group.
429 */
430 unsigned long load;
431
432 /*
433 * part of the group weight distributed to this span.
434 */
435 unsigned long shares;
436
437 /*
438 * The sum of all runqueue weights within this span.
439 */
440 unsigned long rq_weight;
441
442 /*
443 * Weight contributed by tasks; this is the part we can
444 * influence by moving tasks around.
445 */
446 unsigned long task_weight;
447 } aggregate;
448#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200449#endif
450};
451
452/* Real-Time classes' related field in a runqueue: */
453struct rt_rq {
454 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100455 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100456#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100457 int highest_prio; /* highest queued rt task prio */
458#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100459#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100460 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100461 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100462#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100463 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100464 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200465 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100466 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200467 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100468
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100469#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100470 unsigned long rt_nr_boosted;
471
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100472 struct rq *rq;
473 struct list_head leaf_rt_rq_list;
474 struct task_group *tg;
475 struct sched_rt_entity *rt_se;
476#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200477};
478
Gregory Haskins57d885f2008-01-25 21:08:18 +0100479#ifdef CONFIG_SMP
480
481/*
482 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100483 * variables. Each exclusive cpuset essentially defines an island domain by
484 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100485 * exclusive cpuset is created, we also create and attach a new root-domain
486 * object.
487 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100488 */
489struct root_domain {
490 atomic_t refcount;
491 cpumask_t span;
492 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100493
Ingo Molnar0eab9142008-01-25 21:08:19 +0100494 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100495 * The "RT overload" flag: it gets set if a CPU has more than
496 * one runnable RT task.
497 */
498 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100499 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100500};
501
Gregory Haskinsdc938522008-01-25 21:08:26 +0100502/*
503 * By default the system creates a single root-domain with all cpus as
504 * members (mimicking the global state we have today).
505 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100506static struct root_domain def_root_domain;
507
508#endif
509
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200510/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * This is the main, per-CPU runqueue data structure.
512 *
513 * Locking rule: those places that want to lock multiple runqueues
514 * (such as the load balancing or the thread migration code), lock
515 * acquire operations must be ordered by ascending &runqueue.
516 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700517struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200518 /* runqueue lock: */
519 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 /*
522 * nr_running and cpu_load should be in the same cacheline because
523 * remote CPUs use both these fields when doing load calculation.
524 */
525 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200526 #define CPU_LOAD_IDX_MAX 5
527 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700528 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700529#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200530 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700531 unsigned char in_nohz_recently;
532#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200533 /* capture load from *all* tasks on this cpu: */
534 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200535 unsigned long nr_load_updates;
536 u64 nr_switches;
537
538 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100539 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100540
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200541#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200542 /* list of leaf cfs_rq on this cpu: */
543 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100544#endif
545#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100546 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /*
550 * This is part of a global counter where only the total sum
551 * over all CPUs matters. A task can increase this counter on
552 * one CPU and if it got migrated afterwards it may decrease
553 * it on another CPU. Always updated under the runqueue lock:
554 */
555 unsigned long nr_uninterruptible;
556
Ingo Molnar36c8b582006-07-03 00:25:41 -0700557 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800558 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200560
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200561 u64 clock, prev_clock_raw;
562 s64 clock_max_delta;
563
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100564 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200565 u64 idle_clock;
566 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200567 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 atomic_t nr_iowait;
570
571#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100572 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct sched_domain *sd;
574
575 /* For active balancing */
576 int active_balance;
577 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200578 /* cpu of this runqueue: */
579 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Ingo Molnar36c8b582006-07-03 00:25:41 -0700581 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct list_head migration_queue;
583#endif
584
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100585#ifdef CONFIG_SCHED_HRTICK
586 unsigned long hrtick_flags;
587 ktime_t hrtick_expire;
588 struct hrtimer hrtick_timer;
589#endif
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#ifdef CONFIG_SCHEDSTATS
592 /* latency stats */
593 struct sched_info rq_sched_info;
594
595 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200596 unsigned int yld_exp_empty;
597 unsigned int yld_act_empty;
598 unsigned int yld_both_empty;
599 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200602 unsigned int sched_switch;
603 unsigned int sched_count;
604 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200607 unsigned int ttwu_count;
608 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200609
610 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200611 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700613 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614};
615
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700616static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Ingo Molnardd41f592007-07-09 18:51:59 +0200618static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
619{
620 rq->curr->sched_class->check_preempt_curr(rq, p);
621}
622
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700623static inline int cpu_of(struct rq *rq)
624{
625#ifdef CONFIG_SMP
626 return rq->cpu;
627#else
628 return 0;
629#endif
630}
631
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200632#ifdef CONFIG_NO_HZ
633static inline bool nohz_on(int cpu)
634{
635 return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE;
636}
637
638static inline u64 max_skipped_ticks(struct rq *rq)
639{
640 return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1;
641}
642
643static inline void update_last_tick_seen(struct rq *rq)
644{
645 rq->last_tick_seen = jiffies;
646}
647#else
648static inline u64 max_skipped_ticks(struct rq *rq)
649{
650 return 1;
651}
652
653static inline void update_last_tick_seen(struct rq *rq)
654{
655}
656#endif
657
Nick Piggin674311d2005-06-25 14:57:27 -0700658/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200659 * Update the per-runqueue clock, as finegrained as the platform can give
660 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200661 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200662static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200663{
664 u64 prev_raw = rq->prev_clock_raw;
665 u64 now = sched_clock();
666 s64 delta = now - prev_raw;
667 u64 clock = rq->clock;
668
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200669#ifdef CONFIG_SCHED_DEBUG
670 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
671#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200672 /*
673 * Protect against sched_clock() occasionally going backwards:
674 */
675 if (unlikely(delta < 0)) {
676 clock++;
677 rq->clock_warps++;
678 } else {
679 /*
680 * Catch too large forward jumps too:
681 */
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200682 u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC;
683 u64 max_time = rq->tick_timestamp + max_jump;
684
685 if (unlikely(clock + delta > max_time)) {
686 if (clock < max_time)
687 clock = max_time;
Ingo Molnar529c7722007-08-10 23:05:11 +0200688 else
689 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200690 rq->clock_overflows++;
691 } else {
692 if (unlikely(delta > rq->clock_max_delta))
693 rq->clock_max_delta = delta;
694 clock += delta;
695 }
696 }
697
698 rq->prev_clock_raw = now;
699 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200700}
701
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200702static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200703{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200704 if (likely(smp_processor_id() == cpu_of(rq)))
705 __update_rq_clock(rq);
706}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200707
Ingo Molnar20d315d2007-07-09 18:51:58 +0200708/*
Nick Piggin674311d2005-06-25 14:57:27 -0700709 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700710 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700711 *
712 * The domain tree of any CPU may only be accessed from within
713 * preempt-disabled sections.
714 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700715#define for_each_domain(cpu, __sd) \
716 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
719#define this_rq() (&__get_cpu_var(runqueues))
720#define task_rq(p) cpu_rq(task_cpu(p))
721#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
722
Ingo Molnare436d802007-07-19 21:28:35 +0200723/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200724 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
725 */
726#ifdef CONFIG_SCHED_DEBUG
727# define const_debug __read_mostly
728#else
729# define const_debug static const
730#endif
731
732/*
733 * Debugging: various feature bits
734 */
735enum {
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200736 SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
Ingo Molnar96126332007-11-15 20:57:40 +0100737 SCHED_FEAT_WAKEUP_PREEMPT = 2,
738 SCHED_FEAT_START_DEBIT = 4,
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100739 SCHED_FEAT_AFFINE_WAKEUPS = 8,
740 SCHED_FEAT_CACHE_HOT_BUDDY = 16,
Ingo Molnar02e2b832008-03-19 01:37:10 +0100741 SCHED_FEAT_SYNC_WAKEUPS = 32,
742 SCHED_FEAT_HRTICK = 64,
743 SCHED_FEAT_DOUBLE_TICK = 128,
Peter Zijlstra112f53f2008-03-19 11:43:36 +0100744 SCHED_FEAT_NORMALIZED_SLEEPER = 256,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200745};
746
747const_debug unsigned int sysctl_sched_features =
Ingo Molnar8401f772007-10-18 21:32:55 +0200748 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
Ingo Molnar96126332007-11-15 20:57:40 +0100749 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
Ingo Molnar8401f772007-10-18 21:32:55 +0200750 SCHED_FEAT_START_DEBIT * 1 |
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100751 SCHED_FEAT_AFFINE_WAKEUPS * 1 |
752 SCHED_FEAT_CACHE_HOT_BUDDY * 1 |
Ingo Molnar02e2b832008-03-19 01:37:10 +0100753 SCHED_FEAT_SYNC_WAKEUPS * 1 |
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100754 SCHED_FEAT_HRTICK * 1 |
Peter Zijlstra112f53f2008-03-19 11:43:36 +0100755 SCHED_FEAT_DOUBLE_TICK * 0 |
756 SCHED_FEAT_NORMALIZED_SLEEPER * 1;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200757
758#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
759
760/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100761 * Number of tasks to iterate in a single balance run.
762 * Limited because this is done with IRQs disabled.
763 */
764const_debug unsigned int sysctl_sched_nr_migrate = 32;
765
766/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100767 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100768 * default: 1s
769 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100770unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100771
Ingo Molnar6892b752008-02-13 14:02:36 +0100772static __read_mostly int scheduler_running;
773
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100774/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100775 * part of the period that we allow rt tasks to run in us.
776 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100777 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100778int sysctl_sched_rt_runtime = 950000;
779
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200780static inline u64 global_rt_period(void)
781{
782 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
783}
784
785static inline u64 global_rt_runtime(void)
786{
787 if (sysctl_sched_rt_period < 0)
788 return RUNTIME_INF;
789
790 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
791}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100792
Ingo Molnar27ec4402008-02-28 21:00:21 +0100793static const unsigned long long time_sync_thresh = 100000;
794
795static DEFINE_PER_CPU(unsigned long long, time_offset);
796static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
797
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100798/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100799 * Global lock which we take every now and then to synchronize
800 * the CPUs time. This method is not warp-safe, but it's good
801 * enough to synchronize slowly diverging time sources and thus
802 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200803 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100804static DEFINE_SPINLOCK(time_sync_lock);
805static unsigned long long prev_global_time;
806
807static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
808{
809 unsigned long flags;
810
811 spin_lock_irqsave(&time_sync_lock, flags);
812
813 if (time < prev_global_time) {
814 per_cpu(time_offset, cpu) += prev_global_time - time;
815 time = prev_global_time;
816 } else {
817 prev_global_time = time;
818 }
819
820 spin_unlock_irqrestore(&time_sync_lock, flags);
821
822 return time;
823}
824
825static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200826{
Ingo Molnare436d802007-07-19 21:28:35 +0200827 unsigned long long now;
828 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200829 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200830
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100831 /*
832 * Only call sched_clock() if the scheduler has already been
833 * initialized (some code might call cpu_clock() very early):
834 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100835 if (unlikely(!scheduler_running))
836 return 0;
837
838 local_irq_save(flags);
839 rq = cpu_rq(cpu);
840 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200841 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200842 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200843
844 return now;
845}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100846
847/*
848 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
849 * clock constructed from sched_clock():
850 */
851unsigned long long cpu_clock(int cpu)
852{
853 unsigned long long prev_cpu_time, time, delta_time;
854
855 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
856 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
857 delta_time = time-prev_cpu_time;
858
859 if (unlikely(delta_time > time_sync_thresh))
860 time = __sync_cpu_clock(time, cpu);
861
862 return time;
863}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200864EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700867# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700869#ifndef finish_arch_switch
870# define finish_arch_switch(prev) do { } while (0)
871#endif
872
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100873static inline int task_current(struct rq *rq, struct task_struct *p)
874{
875 return rq->curr == p;
876}
877
Nick Piggin4866cde2005-06-25 14:57:23 -0700878#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700879static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700880{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100881 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700882}
883
Ingo Molnar70b97a72006-07-03 00:25:42 -0700884static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700885{
886}
887
Ingo Molnar70b97a72006-07-03 00:25:42 -0700888static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700889{
Ingo Molnarda04c032005-09-13 11:17:59 +0200890#ifdef CONFIG_DEBUG_SPINLOCK
891 /* this is a valid case when another task releases the spinlock */
892 rq->lock.owner = current;
893#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700894 /*
895 * If we are tracking spinlock dependencies then we have to
896 * fix up the runqueue lock - which gets 'carried over' from
897 * prev into current:
898 */
899 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
900
Nick Piggin4866cde2005-06-25 14:57:23 -0700901 spin_unlock_irq(&rq->lock);
902}
903
904#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700905static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700906{
907#ifdef CONFIG_SMP
908 return p->oncpu;
909#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100910 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700911#endif
912}
913
Ingo Molnar70b97a72006-07-03 00:25:42 -0700914static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700915{
916#ifdef CONFIG_SMP
917 /*
918 * We can optimise this out completely for !SMP, because the
919 * SMP rebalancing from interrupt is the only thing that cares
920 * here.
921 */
922 next->oncpu = 1;
923#endif
924#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
925 spin_unlock_irq(&rq->lock);
926#else
927 spin_unlock(&rq->lock);
928#endif
929}
930
Ingo Molnar70b97a72006-07-03 00:25:42 -0700931static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700932{
933#ifdef CONFIG_SMP
934 /*
935 * After ->oncpu is cleared, the task can be moved to a different CPU.
936 * We must ensure this doesn't happen until the switch is completely
937 * finished.
938 */
939 smp_wmb();
940 prev->oncpu = 0;
941#endif
942#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
943 local_irq_enable();
944#endif
945}
946#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700949 * __task_rq_lock - lock the runqueue a given task resides on.
950 * Must be called interrupts disabled.
951 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700952static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700953 __acquires(rq->lock)
954{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200955 for (;;) {
956 struct rq *rq = task_rq(p);
957 spin_lock(&rq->lock);
958 if (likely(rq == task_rq(p)))
959 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700960 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700961 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700962}
963
964/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100966 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * explicitly disabling preemption.
968 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700969static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 __acquires(rq->lock)
971{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700972 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Andi Kleen3a5c3592007-10-15 17:00:14 +0200974 for (;;) {
975 local_irq_save(*flags);
976 rq = task_rq(p);
977 spin_lock(&rq->lock);
978 if (likely(rq == task_rq(p)))
979 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Alexey Dobriyana9957442007-10-15 17:00:13 +0200984static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700985 __releases(rq->lock)
986{
987 spin_unlock(&rq->lock);
988}
989
Ingo Molnar70b97a72006-07-03 00:25:42 -0700990static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 __releases(rq->lock)
992{
993 spin_unlock_irqrestore(&rq->lock, *flags);
994}
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800997 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200999static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 __acquires(rq->lock)
1001{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001002 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 local_irq_disable();
1005 rq = this_rq();
1006 spin_lock(&rq->lock);
1007
1008 return rq;
1009}
1010
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001011/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001012 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001013 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001014void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001015{
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001016 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001017
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001018 spin_lock(&rq->lock);
1019 __update_rq_clock(rq);
1020 spin_unlock(&rq->lock);
1021 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001022}
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001023EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
1024
1025/*
1026 * We just idled delta nanoseconds (called with irqs disabled):
1027 */
1028void sched_clock_idle_wakeup_event(u64 delta_ns)
1029{
1030 struct rq *rq = cpu_rq(smp_processor_id());
1031 u64 now = sched_clock();
1032
1033 rq->idle_clock += delta_ns;
1034 /*
1035 * Override the previous timestamp and ignore all
1036 * sched_clock() deltas that occured while we idled,
1037 * and use the PM-provided delta_ns to advance the
1038 * rq clock:
1039 */
1040 spin_lock(&rq->lock);
1041 rq->prev_clock_raw = now;
1042 rq->clock += delta_ns;
1043 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +01001044 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001045}
1046EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001047
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001048static void __resched_task(struct task_struct *p, int tif_bit);
1049
1050static inline void resched_task(struct task_struct *p)
1051{
1052 __resched_task(p, TIF_NEED_RESCHED);
1053}
1054
1055#ifdef CONFIG_SCHED_HRTICK
1056/*
1057 * Use HR-timers to deliver accurate preemption points.
1058 *
1059 * Its all a bit involved since we cannot program an hrt while holding the
1060 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1061 * reschedule event.
1062 *
1063 * When we get rescheduled we reprogram the hrtick_timer outside of the
1064 * rq->lock.
1065 */
1066static inline void resched_hrt(struct task_struct *p)
1067{
1068 __resched_task(p, TIF_HRTICK_RESCHED);
1069}
1070
1071static inline void resched_rq(struct rq *rq)
1072{
1073 unsigned long flags;
1074
1075 spin_lock_irqsave(&rq->lock, flags);
1076 resched_task(rq->curr);
1077 spin_unlock_irqrestore(&rq->lock, flags);
1078}
1079
1080enum {
1081 HRTICK_SET, /* re-programm hrtick_timer */
1082 HRTICK_RESET, /* not a new slice */
1083};
1084
1085/*
1086 * Use hrtick when:
1087 * - enabled by features
1088 * - hrtimer is actually high res
1089 */
1090static inline int hrtick_enabled(struct rq *rq)
1091{
1092 if (!sched_feat(HRTICK))
1093 return 0;
1094 return hrtimer_is_hres_active(&rq->hrtick_timer);
1095}
1096
1097/*
1098 * Called to set the hrtick timer state.
1099 *
1100 * called with rq->lock held and irqs disabled
1101 */
1102static void hrtick_start(struct rq *rq, u64 delay, int reset)
1103{
1104 assert_spin_locked(&rq->lock);
1105
1106 /*
1107 * preempt at: now + delay
1108 */
1109 rq->hrtick_expire =
1110 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1111 /*
1112 * indicate we need to program the timer
1113 */
1114 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1115 if (reset)
1116 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1117
1118 /*
1119 * New slices are called from the schedule path and don't need a
1120 * forced reschedule.
1121 */
1122 if (reset)
1123 resched_hrt(rq->curr);
1124}
1125
1126static void hrtick_clear(struct rq *rq)
1127{
1128 if (hrtimer_active(&rq->hrtick_timer))
1129 hrtimer_cancel(&rq->hrtick_timer);
1130}
1131
1132/*
1133 * Update the timer from the possible pending state.
1134 */
1135static void hrtick_set(struct rq *rq)
1136{
1137 ktime_t time;
1138 int set, reset;
1139 unsigned long flags;
1140
1141 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1142
1143 spin_lock_irqsave(&rq->lock, flags);
1144 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1145 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1146 time = rq->hrtick_expire;
1147 clear_thread_flag(TIF_HRTICK_RESCHED);
1148 spin_unlock_irqrestore(&rq->lock, flags);
1149
1150 if (set) {
1151 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1152 if (reset && !hrtimer_active(&rq->hrtick_timer))
1153 resched_rq(rq);
1154 } else
1155 hrtick_clear(rq);
1156}
1157
1158/*
1159 * High-resolution timer tick.
1160 * Runs from hardirq context with interrupts disabled.
1161 */
1162static enum hrtimer_restart hrtick(struct hrtimer *timer)
1163{
1164 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1165
1166 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1167
1168 spin_lock(&rq->lock);
1169 __update_rq_clock(rq);
1170 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1171 spin_unlock(&rq->lock);
1172
1173 return HRTIMER_NORESTART;
1174}
1175
1176static inline void init_rq_hrtick(struct rq *rq)
1177{
1178 rq->hrtick_flags = 0;
1179 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1180 rq->hrtick_timer.function = hrtick;
1181 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1182}
1183
1184void hrtick_resched(void)
1185{
1186 struct rq *rq;
1187 unsigned long flags;
1188
1189 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1190 return;
1191
1192 local_irq_save(flags);
1193 rq = cpu_rq(smp_processor_id());
1194 hrtick_set(rq);
1195 local_irq_restore(flags);
1196}
1197#else
1198static inline void hrtick_clear(struct rq *rq)
1199{
1200}
1201
1202static inline void hrtick_set(struct rq *rq)
1203{
1204}
1205
1206static inline void init_rq_hrtick(struct rq *rq)
1207{
1208}
1209
1210void hrtick_resched(void)
1211{
1212}
1213#endif
1214
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001215/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001216 * resched_task - mark a task 'to be rescheduled now'.
1217 *
1218 * On UP this means the setting of the need_resched flag, on SMP it
1219 * might also involve a cross-CPU call to trigger the scheduler on
1220 * the target CPU.
1221 */
1222#ifdef CONFIG_SMP
1223
1224#ifndef tsk_is_polling
1225#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1226#endif
1227
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001228static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001229{
1230 int cpu;
1231
1232 assert_spin_locked(&task_rq(p)->lock);
1233
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001234 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001235 return;
1236
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001237 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001238
1239 cpu = task_cpu(p);
1240 if (cpu == smp_processor_id())
1241 return;
1242
1243 /* NEED_RESCHED must be visible before we test polling */
1244 smp_mb();
1245 if (!tsk_is_polling(p))
1246 smp_send_reschedule(cpu);
1247}
1248
1249static void resched_cpu(int cpu)
1250{
1251 struct rq *rq = cpu_rq(cpu);
1252 unsigned long flags;
1253
1254 if (!spin_trylock_irqsave(&rq->lock, flags))
1255 return;
1256 resched_task(cpu_curr(cpu));
1257 spin_unlock_irqrestore(&rq->lock, flags);
1258}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001259
1260#ifdef CONFIG_NO_HZ
1261/*
1262 * When add_timer_on() enqueues a timer into the timer wheel of an
1263 * idle CPU then this timer might expire before the next timer event
1264 * which is scheduled to wake up that CPU. In case of a completely
1265 * idle system the next event might even be infinite time into the
1266 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1267 * leaves the inner idle loop so the newly added timer is taken into
1268 * account when the CPU goes back to idle and evaluates the timer
1269 * wheel for the next timer event.
1270 */
1271void wake_up_idle_cpu(int cpu)
1272{
1273 struct rq *rq = cpu_rq(cpu);
1274
1275 if (cpu == smp_processor_id())
1276 return;
1277
1278 /*
1279 * This is safe, as this function is called with the timer
1280 * wheel base lock of (cpu) held. When the CPU is on the way
1281 * to idle and has not yet set rq->curr to idle then it will
1282 * be serialized on the timer wheel base lock and take the new
1283 * timer into account automatically.
1284 */
1285 if (rq->curr != rq->idle)
1286 return;
1287
1288 /*
1289 * We can set TIF_RESCHED on the idle task of the other CPU
1290 * lockless. The worst case is that the other CPU runs the
1291 * idle task through an additional NOOP schedule()
1292 */
1293 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1294
1295 /* NEED_RESCHED must be visible before we test polling */
1296 smp_mb();
1297 if (!tsk_is_polling(rq->idle))
1298 smp_send_reschedule(cpu);
1299}
1300#endif
1301
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001302#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001303static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001304{
1305 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001306 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001307}
1308#endif
1309
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001310#if BITS_PER_LONG == 32
1311# define WMULT_CONST (~0UL)
1312#else
1313# define WMULT_CONST (1UL << 32)
1314#endif
1315
1316#define WMULT_SHIFT 32
1317
Ingo Molnar194081e2007-08-09 11:16:51 +02001318/*
1319 * Shift right and round:
1320 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001321#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001322
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001323static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001324calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1325 struct load_weight *lw)
1326{
1327 u64 tmp;
1328
1329 if (unlikely(!lw->inv_weight))
Ingo Molnar27d11722008-03-14 22:20:01 +01001330 lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001331
1332 tmp = (u64)delta_exec * weight;
1333 /*
1334 * Check whether we'd overflow the 64-bit multiplication:
1335 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001336 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001337 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001338 WMULT_SHIFT/2);
1339 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001340 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001341
Ingo Molnarecf691d2007-08-02 17:41:40 +02001342 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001343}
1344
1345static inline unsigned long
1346calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1347{
1348 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1349}
1350
Ingo Molnar10919852007-10-15 17:00:04 +02001351static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001352{
1353 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001354 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001355}
1356
Ingo Molnar10919852007-10-15 17:00:04 +02001357static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001358{
1359 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001360 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001361}
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001364 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1365 * of tasks with abnormal "nice" values across CPUs the contribution that
1366 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001367 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001368 * scaled version of the new time slice allocation that they receive on time
1369 * slice expiry etc.
1370 */
1371
Ingo Molnardd41f592007-07-09 18:51:59 +02001372#define WEIGHT_IDLEPRIO 2
1373#define WMULT_IDLEPRIO (1 << 31)
1374
1375/*
1376 * Nice levels are multiplicative, with a gentle 10% change for every
1377 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1378 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1379 * that remained on nice 0.
1380 *
1381 * The "10% effect" is relative and cumulative: from _any_ nice level,
1382 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001383 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1384 * If a task goes up by ~10% and another task goes down by ~10% then
1385 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001386 */
1387static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001388 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1389 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1390 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1391 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1392 /* 0 */ 1024, 820, 655, 526, 423,
1393 /* 5 */ 335, 272, 215, 172, 137,
1394 /* 10 */ 110, 87, 70, 56, 45,
1395 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001396};
1397
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001398/*
1399 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1400 *
1401 * In cases where the weight does not change often, we can use the
1402 * precalculated inverse to speed up arithmetics by turning divisions
1403 * into multiplications:
1404 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001405static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001406 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1407 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1408 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1409 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1410 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1411 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1412 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1413 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001414};
Peter Williams2dd73a42006-06-27 02:54:34 -07001415
Ingo Molnardd41f592007-07-09 18:51:59 +02001416static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1417
1418/*
1419 * runqueue iterator, to support SMP load-balancing between different
1420 * scheduling classes, without having to expose their internal data
1421 * structures to the load-balancing proper:
1422 */
1423struct rq_iterator {
1424 void *arg;
1425 struct task_struct *(*start)(void *);
1426 struct task_struct *(*next)(void *);
1427};
1428
Peter Williamse1d14842007-10-24 18:23:51 +02001429#ifdef CONFIG_SMP
1430static unsigned long
1431balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1432 unsigned long max_load_move, struct sched_domain *sd,
1433 enum cpu_idle_type idle, int *all_pinned,
1434 int *this_best_prio, struct rq_iterator *iterator);
1435
1436static int
1437iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1438 struct sched_domain *sd, enum cpu_idle_type idle,
1439 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001440#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001441
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001442#ifdef CONFIG_CGROUP_CPUACCT
1443static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1444#else
1445static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1446#endif
1447
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001448static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1449{
1450 update_load_add(&rq->load, load);
1451}
1452
1453static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1454{
1455 update_load_sub(&rq->load, load);
1456}
1457
Gregory Haskinse7693a32008-01-25 21:08:09 +01001458#ifdef CONFIG_SMP
1459static unsigned long source_load(int cpu, int type);
1460static unsigned long target_load(int cpu, int type);
1461static unsigned long cpu_avg_load_per_task(int cpu);
1462static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001463
1464#ifdef CONFIG_FAIR_GROUP_SCHED
1465
1466/*
1467 * Group load balancing.
1468 *
1469 * We calculate a few balance domain wide aggregate numbers; load and weight.
1470 * Given the pictures below, and assuming each item has equal weight:
1471 *
1472 * root 1 - thread
1473 * / | \ A - group
1474 * A 1 B
1475 * /|\ / \
1476 * C 2 D 3 4
1477 * | |
1478 * 5 6
1479 *
1480 * load:
1481 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1482 * which equals 1/9-th of the total load.
1483 *
1484 * shares:
1485 * The weight of this group on the selected cpus.
1486 *
1487 * rq_weight:
1488 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1489 * B would get 2.
1490 *
1491 * task_weight:
1492 * Part of the rq_weight contributed by tasks; all groups except B would
1493 * get 1, B gets 2.
1494 */
1495
1496static inline struct aggregate_struct *
1497aggregate(struct task_group *tg, struct sched_domain *sd)
1498{
1499 return &tg->cfs_rq[sd->first_cpu]->aggregate;
1500}
1501
1502typedef void (*aggregate_func)(struct task_group *, struct sched_domain *);
1503
1504/*
1505 * Iterate the full tree, calling @down when first entering a node and @up when
1506 * leaving it for the final time.
1507 */
1508static
1509void aggregate_walk_tree(aggregate_func down, aggregate_func up,
1510 struct sched_domain *sd)
1511{
1512 struct task_group *parent, *child;
1513
1514 rcu_read_lock();
1515 parent = &root_task_group;
1516down:
1517 (*down)(parent, sd);
1518 list_for_each_entry_rcu(child, &parent->children, siblings) {
1519 parent = child;
1520 goto down;
1521
1522up:
1523 continue;
1524 }
1525 (*up)(parent, sd);
1526
1527 child = parent;
1528 parent = parent->parent;
1529 if (parent)
1530 goto up;
1531 rcu_read_unlock();
1532}
1533
1534/*
1535 * Calculate the aggregate runqueue weight.
1536 */
1537static
1538void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd)
1539{
1540 unsigned long rq_weight = 0;
1541 unsigned long task_weight = 0;
1542 int i;
1543
1544 for_each_cpu_mask(i, sd->span) {
1545 rq_weight += tg->cfs_rq[i]->load.weight;
1546 task_weight += tg->cfs_rq[i]->task_weight;
1547 }
1548
1549 aggregate(tg, sd)->rq_weight = rq_weight;
1550 aggregate(tg, sd)->task_weight = task_weight;
1551}
1552
1553/*
1554 * Redistribute tg->shares amongst all tg->cfs_rq[]s.
1555 */
1556static void __aggregate_redistribute_shares(struct task_group *tg)
1557{
1558 int i, max_cpu = smp_processor_id();
1559 unsigned long rq_weight = 0;
1560 unsigned long shares, max_shares = 0, shares_rem = tg->shares;
1561
1562 for_each_possible_cpu(i)
1563 rq_weight += tg->cfs_rq[i]->load.weight;
1564
1565 for_each_possible_cpu(i) {
1566 /*
1567 * divide shares proportional to the rq_weights.
1568 */
1569 shares = tg->shares * tg->cfs_rq[i]->load.weight;
1570 shares /= rq_weight + 1;
1571
1572 tg->cfs_rq[i]->shares = shares;
1573
1574 if (shares > max_shares) {
1575 max_shares = shares;
1576 max_cpu = i;
1577 }
1578 shares_rem -= shares;
1579 }
1580
1581 /*
1582 * Ensure it all adds up to tg->shares; we can loose a few
1583 * due to rounding down when computing the per-cpu shares.
1584 */
1585 if (shares_rem)
1586 tg->cfs_rq[max_cpu]->shares += shares_rem;
1587}
1588
1589/*
1590 * Compute the weight of this group on the given cpus.
1591 */
1592static
1593void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd)
1594{
1595 unsigned long shares = 0;
1596 int i;
1597
1598again:
1599 for_each_cpu_mask(i, sd->span)
1600 shares += tg->cfs_rq[i]->shares;
1601
1602 /*
1603 * When the span doesn't have any shares assigned, but does have
1604 * tasks to run do a machine wide rebalance (should be rare).
1605 */
1606 if (unlikely(!shares && aggregate(tg, sd)->rq_weight)) {
1607 __aggregate_redistribute_shares(tg);
1608 goto again;
1609 }
1610
1611 aggregate(tg, sd)->shares = shares;
1612}
1613
1614/*
1615 * Compute the load fraction assigned to this group, relies on the aggregate
1616 * weight and this group's parent's load, i.e. top-down.
1617 */
1618static
1619void aggregate_group_load(struct task_group *tg, struct sched_domain *sd)
1620{
1621 unsigned long load;
1622
1623 if (!tg->parent) {
1624 int i;
1625
1626 load = 0;
1627 for_each_cpu_mask(i, sd->span)
1628 load += cpu_rq(i)->load.weight;
1629
1630 } else {
1631 load = aggregate(tg->parent, sd)->load;
1632
1633 /*
1634 * shares is our weight in the parent's rq so
1635 * shares/parent->rq_weight gives our fraction of the load
1636 */
1637 load *= aggregate(tg, sd)->shares;
1638 load /= aggregate(tg->parent, sd)->rq_weight + 1;
1639 }
1640
1641 aggregate(tg, sd)->load = load;
1642}
1643
1644static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1645
1646/*
1647 * Calculate and set the cpu's group shares.
1648 */
1649static void
1650__update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,
1651 int tcpu)
1652{
1653 int boost = 0;
1654 unsigned long shares;
1655 unsigned long rq_weight;
1656
1657 if (!tg->se[tcpu])
1658 return;
1659
1660 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1661
1662 /*
1663 * If there are currently no tasks on the cpu pretend there is one of
1664 * average load so that when a new task gets to run here it will not
1665 * get delayed by group starvation.
1666 */
1667 if (!rq_weight) {
1668 boost = 1;
1669 rq_weight = NICE_0_LOAD;
1670 }
1671
1672 /*
1673 * \Sum shares * rq_weight
1674 * shares = -----------------------
1675 * \Sum rq_weight
1676 *
1677 */
1678 shares = aggregate(tg, sd)->shares * rq_weight;
1679 shares /= aggregate(tg, sd)->rq_weight + 1;
1680
1681 /*
1682 * record the actual number of shares, not the boosted amount.
1683 */
1684 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1685
1686 if (shares < MIN_SHARES)
1687 shares = MIN_SHARES;
1688
1689 __set_se_shares(tg->se[tcpu], shares);
1690}
1691
1692/*
1693 * Re-adjust the weights on the cpu the task came from and on the cpu the
1694 * task went to.
1695 */
1696static void
1697__move_group_shares(struct task_group *tg, struct sched_domain *sd,
1698 int scpu, int dcpu)
1699{
1700 unsigned long shares;
1701
1702 shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1703
1704 __update_group_shares_cpu(tg, sd, scpu);
1705 __update_group_shares_cpu(tg, sd, dcpu);
1706
1707 /*
1708 * ensure we never loose shares due to rounding errors in the
1709 * above redistribution.
1710 */
1711 shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1712 if (shares)
1713 tg->cfs_rq[dcpu]->shares += shares;
1714}
1715
1716/*
1717 * Because changing a group's shares changes the weight of the super-group
1718 * we need to walk up the tree and change all shares until we hit the root.
1719 */
1720static void
1721move_group_shares(struct task_group *tg, struct sched_domain *sd,
1722 int scpu, int dcpu)
1723{
1724 while (tg) {
1725 __move_group_shares(tg, sd, scpu, dcpu);
1726 tg = tg->parent;
1727 }
1728}
1729
1730static
1731void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd)
1732{
1733 unsigned long shares = aggregate(tg, sd)->shares;
1734 int i;
1735
1736 for_each_cpu_mask(i, sd->span) {
1737 struct rq *rq = cpu_rq(i);
1738 unsigned long flags;
1739
1740 spin_lock_irqsave(&rq->lock, flags);
1741 __update_group_shares_cpu(tg, sd, i);
1742 spin_unlock_irqrestore(&rq->lock, flags);
1743 }
1744
1745 aggregate_group_shares(tg, sd);
1746
1747 /*
1748 * ensure we never loose shares due to rounding errors in the
1749 * above redistribution.
1750 */
1751 shares -= aggregate(tg, sd)->shares;
1752 if (shares) {
1753 tg->cfs_rq[sd->first_cpu]->shares += shares;
1754 aggregate(tg, sd)->shares += shares;
1755 }
1756}
1757
1758/*
1759 * Calculate the accumulative weight and recursive load of each task group
1760 * while walking down the tree.
1761 */
1762static
1763void aggregate_get_down(struct task_group *tg, struct sched_domain *sd)
1764{
1765 aggregate_group_weight(tg, sd);
1766 aggregate_group_shares(tg, sd);
1767 aggregate_group_load(tg, sd);
1768}
1769
1770/*
1771 * Rebalance the cpu shares while walking back up the tree.
1772 */
1773static
1774void aggregate_get_up(struct task_group *tg, struct sched_domain *sd)
1775{
1776 aggregate_group_set_shares(tg, sd);
1777}
1778
1779static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1780
1781static void __init init_aggregate(void)
1782{
1783 int i;
1784
1785 for_each_possible_cpu(i)
1786 spin_lock_init(&per_cpu(aggregate_lock, i));
1787}
1788
1789static int get_aggregate(struct sched_domain *sd)
1790{
1791 if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu)))
1792 return 0;
1793
1794 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd);
1795 return 1;
1796}
1797
1798static void put_aggregate(struct sched_domain *sd)
1799{
1800 spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu));
1801}
1802
1803static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1804{
1805 cfs_rq->shares = shares;
1806}
1807
1808#else
1809
1810static inline void init_aggregate(void)
1811{
1812}
1813
1814static inline int get_aggregate(struct sched_domain *sd)
1815{
1816 return 0;
1817}
1818
1819static inline void put_aggregate(struct sched_domain *sd)
1820{
1821}
1822#endif
1823
1824#else /* CONFIG_SMP */
1825
1826#ifdef CONFIG_FAIR_GROUP_SCHED
1827static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1828{
1829}
1830#endif
1831
Gregory Haskinse7693a32008-01-25 21:08:09 +01001832#endif /* CONFIG_SMP */
1833
Ingo Molnardd41f592007-07-09 18:51:59 +02001834#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001835#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001836#include "sched_fair.c"
1837#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001838#ifdef CONFIG_SCHED_DEBUG
1839# include "sched_debug.c"
1840#endif
1841
1842#define sched_class_highest (&rt_sched_class)
1843
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001844static void inc_nr_running(struct rq *rq)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001845{
1846 rq->nr_running++;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001847}
1848
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001849static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001850{
1851 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001852}
1853
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001854static void set_load_weight(struct task_struct *p)
1855{
1856 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001857 p->se.load.weight = prio_to_weight[0] * 2;
1858 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1859 return;
1860 }
1861
1862 /*
1863 * SCHED_IDLE tasks get minimal weight:
1864 */
1865 if (p->policy == SCHED_IDLE) {
1866 p->se.load.weight = WEIGHT_IDLEPRIO;
1867 p->se.load.inv_weight = WMULT_IDLEPRIO;
1868 return;
1869 }
1870
1871 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1872 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001873}
1874
Ingo Molnar8159f872007-08-09 11:16:49 +02001875static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001876{
1877 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001878 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001879 p->se.on_rq = 1;
1880}
1881
Ingo Molnar69be72c2007-08-09 11:16:49 +02001882static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001883{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001884 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001885 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001886}
1887
1888/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001889 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001890 */
Ingo Molnar14531182007-07-09 18:51:59 +02001891static inline int __normal_prio(struct task_struct *p)
1892{
Ingo Molnardd41f592007-07-09 18:51:59 +02001893 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001894}
1895
1896/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001897 * Calculate the expected normal priority: i.e. priority
1898 * without taking RT-inheritance into account. Might be
1899 * boosted by interactivity modifiers. Changes upon fork,
1900 * setprio syscalls, and whenever the interactivity
1901 * estimator recalculates.
1902 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001903static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001904{
1905 int prio;
1906
Ingo Molnare05606d2007-07-09 18:51:59 +02001907 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001908 prio = MAX_RT_PRIO-1 - p->rt_priority;
1909 else
1910 prio = __normal_prio(p);
1911 return prio;
1912}
1913
1914/*
1915 * Calculate the current priority, i.e. the priority
1916 * taken into account by the scheduler. This value might
1917 * be boosted by RT tasks, or might be boosted by
1918 * interactivity modifiers. Will be RT if the task got
1919 * RT-boosted. If not then it returns p->normal_prio.
1920 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001921static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001922{
1923 p->normal_prio = normal_prio(p);
1924 /*
1925 * If we are RT tasks or we were boosted to RT priority,
1926 * keep the priority unchanged. Otherwise, update priority
1927 * to the normal priority:
1928 */
1929 if (!rt_prio(p->prio))
1930 return p->normal_prio;
1931 return p->prio;
1932}
1933
1934/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001935 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001937static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001939 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001940 rq->nr_uninterruptible--;
1941
Ingo Molnar8159f872007-08-09 11:16:49 +02001942 enqueue_task(rq, p, wakeup);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001943 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
1946/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 * deactivate_task - remove a task from the runqueue.
1948 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001949static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001951 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001952 rq->nr_uninterruptible++;
1953
Ingo Molnar69be72c2007-08-09 11:16:49 +02001954 dequeue_task(rq, p, sleep);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001955 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958/**
1959 * task_curr - is this task currently executing on a CPU?
1960 * @p: the task in question.
1961 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001962inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 return cpu_curr(task_cpu(p)) == p;
1965}
1966
Peter Williams2dd73a42006-06-27 02:54:34 -07001967/* Used instead of source_load when we know the type == 0 */
1968unsigned long weighted_cpuload(const int cpu)
1969{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001970 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001971}
1972
1973static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1974{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001975 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001976#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001977 /*
1978 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1979 * successfuly executed on another CPU. We must ensure that updates of
1980 * per-task data have been completed by this moment.
1981 */
1982 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001983 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001984#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001985}
1986
Steven Rostedtcb469842008-01-25 21:08:22 +01001987static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1988 const struct sched_class *prev_class,
1989 int oldprio, int running)
1990{
1991 if (prev_class != p->sched_class) {
1992 if (prev_class->switched_from)
1993 prev_class->switched_from(rq, p, running);
1994 p->sched_class->switched_to(rq, p, running);
1995 } else
1996 p->sched_class->prio_changed(rq, p, oldprio, running);
1997}
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02002000
Ingo Molnarcc367732007-10-15 17:00:18 +02002001/*
2002 * Is this task likely cache-hot:
2003 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002004static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002005task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2006{
2007 s64 delta;
2008
Ingo Molnarf540a602008-03-15 17:10:34 +01002009 /*
2010 * Buddy candidates are cache hot:
2011 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01002012 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01002013 return 1;
2014
Ingo Molnarcc367732007-10-15 17:00:18 +02002015 if (p->sched_class != &fair_sched_class)
2016 return 0;
2017
Ingo Molnar6bc16652007-10-15 17:00:18 +02002018 if (sysctl_sched_migration_cost == -1)
2019 return 1;
2020 if (sysctl_sched_migration_cost == 0)
2021 return 0;
2022
Ingo Molnarcc367732007-10-15 17:00:18 +02002023 delta = now - p->se.exec_start;
2024
2025 return delta < (s64)sysctl_sched_migration_cost;
2026}
2027
2028
Ingo Molnardd41f592007-07-09 18:51:59 +02002029void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002030{
Ingo Molnardd41f592007-07-09 18:51:59 +02002031 int old_cpu = task_cpu(p);
2032 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002033 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
2034 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02002035 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002036
2037 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002038
2039#ifdef CONFIG_SCHEDSTATS
2040 if (p->se.wait_start)
2041 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002042 if (p->se.sleep_start)
2043 p->se.sleep_start -= clock_offset;
2044 if (p->se.block_start)
2045 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02002046 if (old_cpu != new_cpu) {
2047 schedstat_inc(p, se.nr_migrations);
2048 if (task_hot(p, old_rq->clock, NULL))
2049 schedstat_inc(p, se.nr_forced2_migrations);
2050 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002051#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002052 p->se.vruntime -= old_cfsrq->min_vruntime -
2053 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02002054
2055 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002056}
2057
Ingo Molnar70b97a72006-07-03 00:25:42 -07002058struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Ingo Molnar36c8b582006-07-03 00:25:41 -07002061 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 int dest_cpu;
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002065};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067/*
2068 * The task's runqueue lock must be held.
2069 * Returns true if you have to wait for migration thread.
2070 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002071static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002072migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002074 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 /*
2077 * If the task is not on a runqueue (and not running), then
2078 * it is sufficient to simply update the task's cpu field.
2079 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002080 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 set_task_cpu(p, dest_cpu);
2082 return 0;
2083 }
2084
2085 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 req->task = p;
2087 req->dest_cpu = dest_cpu;
2088 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return 1;
2091}
2092
2093/*
2094 * wait_task_inactive - wait for a thread to unschedule.
2095 *
2096 * The caller must ensure that the task *will* unschedule sometime soon,
2097 * else this function might spin for a *long* time. This function can't
2098 * be called with interrupts off, or it may introduce deadlock with
2099 * smp_call_function() if an IPI is sent by the same process we are
2100 * waiting to become inactive.
2101 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002102void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002105 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002106 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Andi Kleen3a5c3592007-10-15 17:00:14 +02002108 for (;;) {
2109 /*
2110 * We do the initial early heuristics without holding
2111 * any task-queue locks at all. We'll only try to get
2112 * the runqueue lock when things look like they will
2113 * work out!
2114 */
2115 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002116
Andi Kleen3a5c3592007-10-15 17:00:14 +02002117 /*
2118 * If the task is actively running on another CPU
2119 * still, just relax and busy-wait without holding
2120 * any locks.
2121 *
2122 * NOTE! Since we don't hold any locks, it's not
2123 * even sure that "rq" stays as the right runqueue!
2124 * But we don't care, since "task_running()" will
2125 * return false if the runqueue has changed and p
2126 * is actually now running somewhere else!
2127 */
2128 while (task_running(rq, p))
2129 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002130
Andi Kleen3a5c3592007-10-15 17:00:14 +02002131 /*
2132 * Ok, time to look more closely! We need the rq
2133 * lock now, to be *sure*. If we're wrong, we'll
2134 * just go back and repeat.
2135 */
2136 rq = task_rq_lock(p, &flags);
2137 running = task_running(rq, p);
2138 on_rq = p->se.on_rq;
2139 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002140
Andi Kleen3a5c3592007-10-15 17:00:14 +02002141 /*
2142 * Was it really running after all now that we
2143 * checked with the proper locks actually held?
2144 *
2145 * Oops. Go back and try again..
2146 */
2147 if (unlikely(running)) {
2148 cpu_relax();
2149 continue;
2150 }
2151
2152 /*
2153 * It's not enough that it's not actively running,
2154 * it must be off the runqueue _entirely_, and not
2155 * preempted!
2156 *
2157 * So if it wa still runnable (but just not actively
2158 * running right now), it's preempted, and we should
2159 * yield - it could be a while.
2160 */
2161 if (unlikely(on_rq)) {
2162 schedule_timeout_uninterruptible(1);
2163 continue;
2164 }
2165
2166 /*
2167 * Ahh, all good. It wasn't running, and it wasn't
2168 * runnable, which means that it will never become
2169 * running in the future either. We're all done!
2170 */
2171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
2175/***
2176 * kick_process - kick a running thread to enter/exit the kernel
2177 * @p: the to-be-kicked thread
2178 *
2179 * Cause a process which is running on another CPU to enter
2180 * kernel-mode, without any delay. (to get signals handled.)
2181 *
2182 * NOTE: this function doesnt have to take the runqueue lock,
2183 * because all it wants to ensure is that the remote task enters
2184 * the kernel. If the IPI races and the task has been migrated
2185 * to another CPU then no harm is done and the purpose has been
2186 * achieved as well.
2187 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002188void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190 int cpu;
2191
2192 preempt_disable();
2193 cpu = task_cpu(p);
2194 if ((cpu != smp_processor_id()) && task_curr(p))
2195 smp_send_reschedule(cpu);
2196 preempt_enable();
2197}
2198
2199/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002200 * Return a low guess at the load of a migration-source cpu weighted
2201 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 *
2203 * We want to under-estimate the load of migration sources, to
2204 * balance conservatively.
2205 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002206static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002207{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002208 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002209 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002210
Peter Williams2dd73a42006-06-27 02:54:34 -07002211 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002212 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002213
Ingo Molnardd41f592007-07-09 18:51:59 +02002214 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
2217/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002218 * Return a high guess at the load of a migration-target cpu weighted
2219 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002221static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002222{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002223 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002224 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002225
Peter Williams2dd73a42006-06-27 02:54:34 -07002226 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002227 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002228
Ingo Molnardd41f592007-07-09 18:51:59 +02002229 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002230}
2231
2232/*
2233 * Return the average load per task on the cpu's run queue
2234 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002235static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07002236{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002237 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002238 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07002239 unsigned long n = rq->nr_running;
2240
Ingo Molnardd41f592007-07-09 18:51:59 +02002241 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Nick Piggin147cbb42005-06-25 14:57:19 -07002244/*
2245 * find_idlest_group finds and returns the least busy CPU group within the
2246 * domain.
2247 */
2248static struct sched_group *
2249find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2250{
2251 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2252 unsigned long min_load = ULONG_MAX, this_load = 0;
2253 int load_idx = sd->forkexec_idx;
2254 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2255
2256 do {
2257 unsigned long load, avg_load;
2258 int local_group;
2259 int i;
2260
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002261 /* Skip over this group if it has no CPUs allowed */
2262 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002263 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002264
Nick Piggin147cbb42005-06-25 14:57:19 -07002265 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002266
2267 /* Tally up the load of all CPUs in the group */
2268 avg_load = 0;
2269
2270 for_each_cpu_mask(i, group->cpumask) {
2271 /* Bias balancing toward cpus of our domain */
2272 if (local_group)
2273 load = source_load(i, load_idx);
2274 else
2275 load = target_load(i, load_idx);
2276
2277 avg_load += load;
2278 }
2279
2280 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002281 avg_load = sg_div_cpu_power(group,
2282 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002283
2284 if (local_group) {
2285 this_load = avg_load;
2286 this = group;
2287 } else if (avg_load < min_load) {
2288 min_load = avg_load;
2289 idlest = group;
2290 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002291 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002292
2293 if (!idlest || 100*this_load < imbalance*min_load)
2294 return NULL;
2295 return idlest;
2296}
2297
2298/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002299 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002300 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002301static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002302find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2303 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002304{
2305 unsigned long load, min_load = ULONG_MAX;
2306 int idlest = -1;
2307 int i;
2308
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002309 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002310 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002311
Mike Travis7c16ec52008-04-04 18:11:11 -07002312 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002313 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002314
2315 if (load < min_load || (load == min_load && i == this_cpu)) {
2316 min_load = load;
2317 idlest = i;
2318 }
2319 }
2320
2321 return idlest;
2322}
2323
Nick Piggin476d1392005-06-25 14:57:29 -07002324/*
2325 * sched_balance_self: balance the current task (running on cpu) in domains
2326 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2327 * SD_BALANCE_EXEC.
2328 *
2329 * Balance, ie. select the least loaded group.
2330 *
2331 * Returns the target CPU number, or the same CPU if no balancing is needed.
2332 *
2333 * preempt must be disabled.
2334 */
2335static int sched_balance_self(int cpu, int flag)
2336{
2337 struct task_struct *t = current;
2338 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002339
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002340 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002341 /*
2342 * If power savings logic is enabled for a domain, stop there.
2343 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002344 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2345 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002346 if (tmp->flags & flag)
2347 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002348 }
Nick Piggin476d1392005-06-25 14:57:29 -07002349
2350 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002351 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002352 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002353 int new_cpu, weight;
2354
2355 if (!(sd->flags & flag)) {
2356 sd = sd->child;
2357 continue;
2358 }
Nick Piggin476d1392005-06-25 14:57:29 -07002359
2360 span = sd->span;
2361 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002362 if (!group) {
2363 sd = sd->child;
2364 continue;
2365 }
Nick Piggin476d1392005-06-25 14:57:29 -07002366
Mike Travis7c16ec52008-04-04 18:11:11 -07002367 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002368 if (new_cpu == -1 || new_cpu == cpu) {
2369 /* Now try balancing at a lower domain level of cpu */
2370 sd = sd->child;
2371 continue;
2372 }
Nick Piggin476d1392005-06-25 14:57:29 -07002373
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002374 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002375 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002376 sd = NULL;
2377 weight = cpus_weight(span);
2378 for_each_domain(cpu, tmp) {
2379 if (weight <= cpus_weight(tmp->span))
2380 break;
2381 if (tmp->flags & flag)
2382 sd = tmp;
2383 }
2384 /* while loop will break here if sd == NULL */
2385 }
2386
2387 return cpu;
2388}
2389
2390#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392/***
2393 * try_to_wake_up - wake up a thread
2394 * @p: the to-be-woken-up thread
2395 * @state: the mask of task states that can be woken
2396 * @sync: do a synchronous wakeup?
2397 *
2398 * Put it on the run-queue if it's not already there. The "current"
2399 * thread is always on the run-queue (except when the actual
2400 * re-schedule is in progress), and as such you're allowed to do
2401 * the simpler "current->state = TASK_RUNNING" to mark yourself
2402 * runnable without the overhead of this.
2403 *
2404 * returns failure only if the task is already active.
2405 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002406static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Ingo Molnarcc367732007-10-15 17:00:18 +02002408 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 unsigned long flags;
2410 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002411 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Ingo Molnarb85d0662008-03-16 20:03:22 +01002413 if (!sched_feat(SYNC_WAKEUPS))
2414 sync = 0;
2415
Linus Torvalds04e2f172008-02-23 18:05:03 -08002416 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 rq = task_rq_lock(p, &flags);
2418 old_state = p->state;
2419 if (!(old_state & state))
2420 goto out;
2421
Ingo Molnardd41f592007-07-09 18:51:59 +02002422 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 goto out_running;
2424
2425 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002426 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 this_cpu = smp_processor_id();
2428
2429#ifdef CONFIG_SMP
2430 if (unlikely(task_running(rq, p)))
2431 goto out_activate;
2432
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002433 cpu = p->sched_class->select_task_rq(p, sync);
2434 if (cpu != orig_cpu) {
2435 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 task_rq_unlock(rq, &flags);
2437 /* might preempt at this point */
2438 rq = task_rq_lock(p, &flags);
2439 old_state = p->state;
2440 if (!(old_state & state))
2441 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002442 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto out_running;
2444
2445 this_cpu = smp_processor_id();
2446 cpu = task_cpu(p);
2447 }
2448
Gregory Haskinse7693a32008-01-25 21:08:09 +01002449#ifdef CONFIG_SCHEDSTATS
2450 schedstat_inc(rq, ttwu_count);
2451 if (cpu == this_cpu)
2452 schedstat_inc(rq, ttwu_local);
2453 else {
2454 struct sched_domain *sd;
2455 for_each_domain(this_cpu, sd) {
2456 if (cpu_isset(cpu, sd->span)) {
2457 schedstat_inc(sd, ttwu_wake_remote);
2458 break;
2459 }
2460 }
2461 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002462#endif
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464out_activate:
2465#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002466 schedstat_inc(p, se.nr_wakeups);
2467 if (sync)
2468 schedstat_inc(p, se.nr_wakeups_sync);
2469 if (orig_cpu != cpu)
2470 schedstat_inc(p, se.nr_wakeups_migrate);
2471 if (cpu == this_cpu)
2472 schedstat_inc(p, se.nr_wakeups_local);
2473 else
2474 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002475 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002476 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 success = 1;
2478
2479out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002480 check_preempt_curr(rq, p);
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002483#ifdef CONFIG_SMP
2484 if (p->sched_class->task_wake_up)
2485 p->sched_class->task_wake_up(rq, p);
2486#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487out:
2488 task_rq_unlock(rq, &flags);
2489
2490 return success;
2491}
2492
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002493int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002495 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497EXPORT_SYMBOL(wake_up_process);
2498
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002499int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
2501 return try_to_wake_up(p, state, 0);
2502}
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504/*
2505 * Perform scheduler related setup for a newly forked process p.
2506 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002507 *
2508 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002510static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Ingo Molnardd41f592007-07-09 18:51:59 +02002512 p->se.exec_start = 0;
2513 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002514 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002515 p->se.last_wakeup = 0;
2516 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002517
2518#ifdef CONFIG_SCHEDSTATS
2519 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002520 p->se.sum_sleep_runtime = 0;
2521 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002522 p->se.block_start = 0;
2523 p->se.sleep_max = 0;
2524 p->se.block_max = 0;
2525 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002526 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002527 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002528#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002529
Peter Zijlstrafa717062008-01-25 21:08:27 +01002530 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002531 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002532 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002533
Avi Kivitye107be32007-07-26 13:40:43 +02002534#ifdef CONFIG_PREEMPT_NOTIFIERS
2535 INIT_HLIST_HEAD(&p->preempt_notifiers);
2536#endif
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 /*
2539 * We mark the process as running here, but have not actually
2540 * inserted it onto the runqueue yet. This guarantees that
2541 * nobody will actually run it, and a signal or other external
2542 * event cannot wake it up and insert it on the runqueue either.
2543 */
2544 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002545}
2546
2547/*
2548 * fork()/clone()-time setup:
2549 */
2550void sched_fork(struct task_struct *p, int clone_flags)
2551{
2552 int cpu = get_cpu();
2553
2554 __sched_fork(p);
2555
2556#ifdef CONFIG_SMP
2557 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2558#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02002559 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002560
2561 /*
2562 * Make sure we do not leak PI boosting priority to the child:
2563 */
2564 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002565 if (!rt_prio(p->prio))
2566 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002567
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002568#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002569 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002570 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002572#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002573 p->oncpu = 0;
2574#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002576 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002577 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002579 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
2582/*
2583 * wake_up_new_task - wake up a newly created task for the first time.
2584 *
2585 * This function will do some initial scheduler statistics housekeeping
2586 * that must be done for every newly created context, then puts the task
2587 * on the runqueue and wakes it.
2588 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002589void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002592 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002596 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 p->prio = effective_prio(p);
2599
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002600 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002601 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002604 * Let the scheduling class do new task startup
2605 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002607 p->sched_class->task_new(rq, p);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002608 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002610 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002611#ifdef CONFIG_SMP
2612 if (p->sched_class->task_wake_up)
2613 p->sched_class->task_wake_up(rq, p);
2614#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002615 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
Avi Kivitye107be32007-07-26 13:40:43 +02002618#ifdef CONFIG_PREEMPT_NOTIFIERS
2619
2620/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002621 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2622 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002623 */
2624void preempt_notifier_register(struct preempt_notifier *notifier)
2625{
2626 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2627}
2628EXPORT_SYMBOL_GPL(preempt_notifier_register);
2629
2630/**
2631 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002632 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002633 *
2634 * This is safe to call from within a preemption notifier.
2635 */
2636void preempt_notifier_unregister(struct preempt_notifier *notifier)
2637{
2638 hlist_del(&notifier->link);
2639}
2640EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2641
2642static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2643{
2644 struct preempt_notifier *notifier;
2645 struct hlist_node *node;
2646
2647 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2648 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2649}
2650
2651static void
2652fire_sched_out_preempt_notifiers(struct task_struct *curr,
2653 struct task_struct *next)
2654{
2655 struct preempt_notifier *notifier;
2656 struct hlist_node *node;
2657
2658 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2659 notifier->ops->sched_out(notifier, next);
2660}
2661
2662#else
2663
2664static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2665{
2666}
2667
2668static void
2669fire_sched_out_preempt_notifiers(struct task_struct *curr,
2670 struct task_struct *next)
2671{
2672}
2673
2674#endif
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002677 * prepare_task_switch - prepare to switch tasks
2678 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002679 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002680 * @next: the task we are going to switch to.
2681 *
2682 * This is called with the rq lock held and interrupts off. It must
2683 * be paired with a subsequent finish_task_switch after the context
2684 * switch.
2685 *
2686 * prepare_task_switch sets up locking and calls architecture specific
2687 * hooks.
2688 */
Avi Kivitye107be32007-07-26 13:40:43 +02002689static inline void
2690prepare_task_switch(struct rq *rq, struct task_struct *prev,
2691 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002692{
Avi Kivitye107be32007-07-26 13:40:43 +02002693 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002694 prepare_lock_switch(rq, next);
2695 prepare_arch_switch(next);
2696}
2697
2698/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002700 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 * @prev: the thread we just switched away from.
2702 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002703 * finish_task_switch must be called after the context switch, paired
2704 * with a prepare_task_switch call before the context switch.
2705 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2706 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 *
2708 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002709 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 * with the lock held can cause deadlocks; see schedule() for
2711 * details.)
2712 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002713static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 __releases(rq->lock)
2715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002717 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719 rq->prev_mm = NULL;
2720
2721 /*
2722 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002723 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002724 * schedule one last time. The schedule call will never return, and
2725 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002726 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 * still held, otherwise prev could be scheduled on another cpu, die
2728 * there before we look at prev->state, and then the reference would
2729 * be dropped twice.
2730 * Manfred Spraul <manfred@colorfullife.com>
2731 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002732 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002733 finish_arch_switch(prev);
2734 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002735#ifdef CONFIG_SMP
2736 if (current->sched_class->post_schedule)
2737 current->sched_class->post_schedule(rq);
2738#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002739
Avi Kivitye107be32007-07-26 13:40:43 +02002740 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (mm)
2742 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002743 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002744 /*
2745 * Remove function-return probe instances associated with this
2746 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002747 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002748 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
2753/**
2754 * schedule_tail - first thing a freshly forked thread must call.
2755 * @prev: the thread we just switched away from.
2756 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002757asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 __releases(rq->lock)
2759{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002760 struct rq *rq = this_rq();
2761
Nick Piggin4866cde2005-06-25 14:57:23 -07002762 finish_task_switch(rq, prev);
2763#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2764 /* In this case, finish_task_switch does not reenable preemption */
2765 preempt_enable();
2766#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002768 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
2771/*
2772 * context_switch - switch to the new MM and the new
2773 * thread's register state.
2774 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002775static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002776context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002777 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
Ingo Molnardd41f592007-07-09 18:51:59 +02002779 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Avi Kivitye107be32007-07-26 13:40:43 +02002781 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002782 mm = next->mm;
2783 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002784 /*
2785 * For paravirt, this is coupled with an exit in switch_to to
2786 * combine the page table reload and the switch backend into
2787 * one hypercall.
2788 */
2789 arch_enter_lazy_cpu_mode();
2790
Ingo Molnardd41f592007-07-09 18:51:59 +02002791 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 next->active_mm = oldmm;
2793 atomic_inc(&oldmm->mm_count);
2794 enter_lazy_tlb(oldmm, next);
2795 } else
2796 switch_mm(oldmm, mm, next);
2797
Ingo Molnardd41f592007-07-09 18:51:59 +02002798 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 rq->prev_mm = oldmm;
2801 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002802 /*
2803 * Since the runqueue lock will be released by the next
2804 * task (which is an invalid locking op but in the case
2805 * of the scheduler it's an obvious special-case), so we
2806 * do an early lockdep release here:
2807 */
2808#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002809 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002810#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 /* Here we just switch the register state and the stack. */
2813 switch_to(prev, next, prev);
2814
Ingo Molnardd41f592007-07-09 18:51:59 +02002815 barrier();
2816 /*
2817 * this_rq must be evaluated again because prev may have moved
2818 * CPUs since it called schedule(), thus the 'rq' on its stack
2819 * frame will be invalid.
2820 */
2821 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
2823
2824/*
2825 * nr_running, nr_uninterruptible and nr_context_switches:
2826 *
2827 * externally visible scheduler statistics: current number of runnable
2828 * threads, current number of uninterruptible-sleeping threads, total
2829 * number of context switches performed since bootup.
2830 */
2831unsigned long nr_running(void)
2832{
2833 unsigned long i, sum = 0;
2834
2835 for_each_online_cpu(i)
2836 sum += cpu_rq(i)->nr_running;
2837
2838 return sum;
2839}
2840
2841unsigned long nr_uninterruptible(void)
2842{
2843 unsigned long i, sum = 0;
2844
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002845 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 sum += cpu_rq(i)->nr_uninterruptible;
2847
2848 /*
2849 * Since we read the counters lockless, it might be slightly
2850 * inaccurate. Do not allow it to go below zero though:
2851 */
2852 if (unlikely((long)sum < 0))
2853 sum = 0;
2854
2855 return sum;
2856}
2857
2858unsigned long long nr_context_switches(void)
2859{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002860 int i;
2861 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002863 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 sum += cpu_rq(i)->nr_switches;
2865
2866 return sum;
2867}
2868
2869unsigned long nr_iowait(void)
2870{
2871 unsigned long i, sum = 0;
2872
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002873 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2875
2876 return sum;
2877}
2878
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002879unsigned long nr_active(void)
2880{
2881 unsigned long i, running = 0, uninterruptible = 0;
2882
2883 for_each_online_cpu(i) {
2884 running += cpu_rq(i)->nr_running;
2885 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2886 }
2887
2888 if (unlikely((long)uninterruptible < 0))
2889 uninterruptible = 0;
2890
2891 return running + uninterruptible;
2892}
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002895 * Update rq->cpu_load[] statistics. This function is usually called every
2896 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002897 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002898static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002899{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002900 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002901 int i, scale;
2902
2903 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002904
2905 /* Update our load: */
2906 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2907 unsigned long old_load, new_load;
2908
2909 /* scale is effectively 1 << i now, and >> i divides by scale */
2910
2911 old_load = this_rq->cpu_load[i];
2912 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002913 /*
2914 * Round up the averaging division if load is increasing. This
2915 * prevents us from getting stuck on 9 if the load is 10, for
2916 * example.
2917 */
2918 if (new_load > old_load)
2919 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002920 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2921 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002922}
2923
Ingo Molnardd41f592007-07-09 18:51:59 +02002924#ifdef CONFIG_SMP
2925
Ingo Molnar48f24c42006-07-03 00:25:40 -07002926/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 * double_rq_lock - safely lock two runqueues
2928 *
2929 * Note this does not disable interrupts like task_rq_lock,
2930 * you need to do so manually before calling.
2931 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002932static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 __acquires(rq1->lock)
2934 __acquires(rq2->lock)
2935{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002936 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (rq1 == rq2) {
2938 spin_lock(&rq1->lock);
2939 __acquire(rq2->lock); /* Fake it out ;) */
2940 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002941 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 spin_lock(&rq1->lock);
2943 spin_lock(&rq2->lock);
2944 } else {
2945 spin_lock(&rq2->lock);
2946 spin_lock(&rq1->lock);
2947 }
2948 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002949 update_rq_clock(rq1);
2950 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951}
2952
2953/*
2954 * double_rq_unlock - safely unlock two runqueues
2955 *
2956 * Note this does not restore interrupts like task_rq_unlock,
2957 * you need to do so manually after calling.
2958 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002959static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 __releases(rq1->lock)
2961 __releases(rq2->lock)
2962{
2963 spin_unlock(&rq1->lock);
2964 if (rq1 != rq2)
2965 spin_unlock(&rq2->lock);
2966 else
2967 __release(rq2->lock);
2968}
2969
2970/*
2971 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2972 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002973static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 __releases(this_rq->lock)
2975 __acquires(busiest->lock)
2976 __acquires(this_rq->lock)
2977{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002978 int ret = 0;
2979
Kirill Korotaev054b9102006-12-10 02:20:11 -08002980 if (unlikely(!irqs_disabled())) {
2981 /* printk() doesn't work good under rq->lock */
2982 spin_unlock(&this_rq->lock);
2983 BUG_ON(1);
2984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002986 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 spin_unlock(&this_rq->lock);
2988 spin_lock(&busiest->lock);
2989 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002990 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 } else
2992 spin_lock(&busiest->lock);
2993 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002994 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
2996
2997/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 * If dest_cpu is allowed for this process, migrate the task to it.
2999 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003000 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 * the cpu_allowed mask is restored.
3002 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003003static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003005 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003007 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 rq = task_rq_lock(p, &flags);
3010 if (!cpu_isset(dest_cpu, p->cpus_allowed)
3011 || unlikely(cpu_is_offline(dest_cpu)))
3012 goto out;
3013
3014 /* force the process onto the specified CPU */
3015 if (migrate_task(p, dest_cpu, &req)) {
3016 /* Need to wait for migration thread (might exit: take ref). */
3017 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 get_task_struct(mt);
3020 task_rq_unlock(rq, &flags);
3021 wake_up_process(mt);
3022 put_task_struct(mt);
3023 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return;
3026 }
3027out:
3028 task_rq_unlock(rq, &flags);
3029}
3030
3031/*
Nick Piggin476d1392005-06-25 14:57:29 -07003032 * sched_exec - execve() is a valuable balancing opportunity, because at
3033 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 */
3035void sched_exec(void)
3036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003038 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003040 if (new_cpu != this_cpu)
3041 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
3044/*
3045 * pull_task - move a task from a remote runqueue to the local runqueue.
3046 * Both runqueues must be locked.
3047 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003048static void pull_task(struct rq *src_rq, struct task_struct *p,
3049 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003051 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003053 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 /*
3055 * Note that idle threads have a prio of MAX_PRIO, for this test
3056 * to be always true for them.
3057 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003058 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
3061/*
3062 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3063 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003064static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003065int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003066 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003067 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 /*
3070 * We do not migrate tasks that are:
3071 * 1) running (obviously), or
3072 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3073 * 3) are cache-hot on their current CPU.
3074 */
Ingo Molnarcc367732007-10-15 17:00:18 +02003075 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3076 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003078 }
Nick Piggin81026792005-06-25 14:57:07 -07003079 *all_pinned = 0;
3080
Ingo Molnarcc367732007-10-15 17:00:18 +02003081 if (task_running(rq, p)) {
3082 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003083 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Ingo Molnarda84d962007-10-15 17:00:18 +02003086 /*
3087 * Aggressive migration if:
3088 * 1) task is cache cold, or
3089 * 2) too many balance attempts have failed.
3090 */
3091
Ingo Molnar6bc16652007-10-15 17:00:18 +02003092 if (!task_hot(p, rq->clock, sd) ||
3093 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003094#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02003095 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003096 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003097 schedstat_inc(p, se.nr_forced_migrations);
3098 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003099#endif
3100 return 1;
3101 }
3102
Ingo Molnarcc367732007-10-15 17:00:18 +02003103 if (task_hot(p, rq->clock, sd)) {
3104 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003105 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return 1;
3108}
3109
Peter Williamse1d14842007-10-24 18:23:51 +02003110static unsigned long
3111balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3112 unsigned long max_load_move, struct sched_domain *sd,
3113 enum cpu_idle_type idle, int *all_pinned,
3114 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003115{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003116 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02003117 struct task_struct *p;
3118 long rem_load_move = max_load_move;
3119
Peter Williamse1d14842007-10-24 18:23:51 +02003120 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003121 goto out;
3122
3123 pinned = 1;
3124
3125 /*
3126 * Start the load-balancing iterator:
3127 */
3128 p = iterator->start(iterator->arg);
3129next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003130 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003131 goto out;
3132 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003133 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02003134 * skip a task if it will be the highest priority task (i.e. smallest
3135 * prio value) on its new queue regardless of its load weight
3136 */
3137 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
3138 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003139 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003140 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003141 p = iterator->next(iterator->arg);
3142 goto next;
3143 }
3144
3145 pull_task(busiest, p, this_rq, this_cpu);
3146 pulled++;
3147 rem_load_move -= p->se.load.weight;
3148
3149 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003150 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003151 */
Peter Williamse1d14842007-10-24 18:23:51 +02003152 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003153 if (p->prio < *this_best_prio)
3154 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003155 p = iterator->next(iterator->arg);
3156 goto next;
3157 }
3158out:
3159 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003160 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003161 * so we can safely collect pull_task() stats here rather than
3162 * inside pull_task().
3163 */
3164 schedstat_add(sd, lb_gained[idle], pulled);
3165
3166 if (all_pinned)
3167 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003168
3169 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003170}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172/*
Peter Williams43010652007-08-09 11:16:46 +02003173 * move_tasks tries to move up to max_load_move weighted load from busiest to
3174 * this_rq, as part of a balancing operation within domain "sd".
3175 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 *
3177 * Called with both runqueues locked.
3178 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003179static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003180 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003181 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003182 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003184 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003185 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003186 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Ingo Molnardd41f592007-07-09 18:51:59 +02003188 do {
Peter Williams43010652007-08-09 11:16:46 +02003189 total_load_moved +=
3190 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003191 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003192 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003193 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003194 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Peter Williams43010652007-08-09 11:16:46 +02003196 return total_load_moved > 0;
3197}
3198
Peter Williamse1d14842007-10-24 18:23:51 +02003199static int
3200iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3201 struct sched_domain *sd, enum cpu_idle_type idle,
3202 struct rq_iterator *iterator)
3203{
3204 struct task_struct *p = iterator->start(iterator->arg);
3205 int pinned = 0;
3206
3207 while (p) {
3208 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3209 pull_task(busiest, p, this_rq, this_cpu);
3210 /*
3211 * Right now, this is only the second place pull_task()
3212 * is called, so we can safely collect pull_task()
3213 * stats here rather than inside pull_task().
3214 */
3215 schedstat_inc(sd, lb_gained[idle]);
3216
3217 return 1;
3218 }
3219 p = iterator->next(iterator->arg);
3220 }
3221
3222 return 0;
3223}
3224
Peter Williams43010652007-08-09 11:16:46 +02003225/*
3226 * move_one_task tries to move exactly one task from busiest to this_rq, as
3227 * part of active balancing operations within "domain".
3228 * Returns 1 if successful and 0 otherwise.
3229 *
3230 * Called with both runqueues locked.
3231 */
3232static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3233 struct sched_domain *sd, enum cpu_idle_type idle)
3234{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003235 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003236
3237 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003238 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003239 return 1;
3240
3241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
3244/*
3245 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003246 * domain. It calculates and returns the amount of weighted load which
3247 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 */
3249static struct sched_group *
3250find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003251 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003252 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
3254 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3255 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003256 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003257 unsigned long busiest_load_per_task, busiest_nr_running;
3258 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003259 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003260#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3261 int power_savings_balance = 1;
3262 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3263 unsigned long min_nr_running = ULONG_MAX;
3264 struct sched_group *group_min = NULL, *group_leader = NULL;
3265#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
3267 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003268 busiest_load_per_task = busiest_nr_running = 0;
3269 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003270 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003271 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003272 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003273 load_idx = sd->newidle_idx;
3274 else
3275 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003278 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 int local_group;
3280 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003281 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003282 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003283 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
3285 local_group = cpu_isset(this_cpu, group->cpumask);
3286
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003287 if (local_group)
3288 balance_cpu = first_cpu(group->cpumask);
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003291 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02003292 max_cpu_load = 0;
3293 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003296 struct rq *rq;
3297
3298 if (!cpu_isset(i, *cpus))
3299 continue;
3300
3301 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003302
Suresh Siddha9439aab2007-07-19 21:28:35 +02003303 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003304 *sd_idle = 0;
3305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003307 if (local_group) {
3308 if (idle_cpu(i) && !first_idle_cpu) {
3309 first_idle_cpu = 1;
3310 balance_cpu = i;
3311 }
3312
Nick Piggina2000572006-02-10 01:51:02 -08003313 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003314 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003315 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003316 if (load > max_cpu_load)
3317 max_cpu_load = load;
3318 if (min_cpu_load > load)
3319 min_cpu_load = load;
3320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003323 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003324 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 }
3326
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003327 /*
3328 * First idle cpu or the first cpu(busiest) in this sched group
3329 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003330 * domains. In the newly idle case, we will allow all the cpu's
3331 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003332 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003333 if (idle != CPU_NEWLY_IDLE && local_group &&
3334 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003335 *balance = 0;
3336 goto ret;
3337 }
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003340 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003343 avg_load = sg_div_cpu_power(group,
3344 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Ken Chen908a7c12007-10-17 16:55:11 +02003346 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3347 __group_imb = 1;
3348
Eric Dumazet5517d862007-05-08 00:32:57 -07003349 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 if (local_group) {
3352 this_load = avg_load;
3353 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003354 this_nr_running = sum_nr_running;
3355 this_load_per_task = sum_weighted_load;
3356 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003357 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 max_load = avg_load;
3359 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003360 busiest_nr_running = sum_nr_running;
3361 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003362 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003364
3365#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3366 /*
3367 * Busy processors will not participate in power savings
3368 * balance.
3369 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003370 if (idle == CPU_NOT_IDLE ||
3371 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3372 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003373
3374 /*
3375 * If the local group is idle or completely loaded
3376 * no need to do power savings balance at this domain
3377 */
3378 if (local_group && (this_nr_running >= group_capacity ||
3379 !this_nr_running))
3380 power_savings_balance = 0;
3381
Ingo Molnardd41f592007-07-09 18:51:59 +02003382 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003383 * If a group is already running at full capacity or idle,
3384 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003385 */
3386 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003387 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003388 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003389
Ingo Molnardd41f592007-07-09 18:51:59 +02003390 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003391 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003392 * This is the group from where we need to pick up the load
3393 * for saving power
3394 */
3395 if ((sum_nr_running < min_nr_running) ||
3396 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003397 first_cpu(group->cpumask) <
3398 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003399 group_min = group;
3400 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003401 min_load_per_task = sum_weighted_load /
3402 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003403 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003404
Ingo Molnardd41f592007-07-09 18:51:59 +02003405 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003406 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003407 * capacity but still has some space to pick up some load
3408 * from other group and save more power
3409 */
3410 if (sum_nr_running <= group_capacity - 1) {
3411 if (sum_nr_running > leader_nr_running ||
3412 (sum_nr_running == leader_nr_running &&
3413 first_cpu(group->cpumask) >
3414 first_cpu(group_leader->cpumask))) {
3415 group_leader = group;
3416 leader_nr_running = sum_nr_running;
3417 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003418 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003419group_next:
3420#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 group = group->next;
3422 } while (group != sd->groups);
3423
Peter Williams2dd73a42006-06-27 02:54:34 -07003424 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 goto out_balanced;
3426
3427 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3428
3429 if (this_load >= avg_load ||
3430 100*max_load <= sd->imbalance_pct*this_load)
3431 goto out_balanced;
3432
Peter Williams2dd73a42006-06-27 02:54:34 -07003433 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003434 if (group_imb)
3435 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 /*
3438 * We're trying to get all the cpus to the average_load, so we don't
3439 * want to push ourselves above the average load, nor do we wish to
3440 * reduce the max loaded cpu below the average load, as either of these
3441 * actions would just result in more rebalancing later, and ping-pong
3442 * tasks around. Thus we look for the minimum possible imbalance.
3443 * Negative imbalances (*we* are more loaded than anyone else) will
3444 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003445 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 * appear as very large values with unsigned longs.
3447 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003448 if (max_load <= busiest_load_per_task)
3449 goto out_balanced;
3450
3451 /*
3452 * In the presence of smp nice balancing, certain scenarios can have
3453 * max load less than avg load(as we skip the groups at or below
3454 * its cpu_power, while calculating max_load..)
3455 */
3456 if (max_load < avg_load) {
3457 *imbalance = 0;
3458 goto small_imbalance;
3459 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003460
3461 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003462 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003465 *imbalance = min(max_pull * busiest->__cpu_power,
3466 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 / SCHED_LOAD_SCALE;
3468
Peter Williams2dd73a42006-06-27 02:54:34 -07003469 /*
3470 * if *imbalance is less than the average load per runnable task
3471 * there is no gaurantee that any tasks will be moved so we'll have
3472 * a think about bumping its value to force at least one task to be
3473 * moved
3474 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003475 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003476 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003477 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Peter Williams2dd73a42006-06-27 02:54:34 -07003479small_imbalance:
3480 pwr_move = pwr_now = 0;
3481 imbn = 2;
3482 if (this_nr_running) {
3483 this_load_per_task /= this_nr_running;
3484 if (busiest_load_per_task > this_load_per_task)
3485 imbn = 1;
3486 } else
3487 this_load_per_task = SCHED_LOAD_SCALE;
3488
Ingo Molnardd41f592007-07-09 18:51:59 +02003489 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3490 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003491 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 return busiest;
3493 }
3494
3495 /*
3496 * OK, we don't have enough imbalance to justify moving tasks,
3497 * however we may be able to increase total CPU power used by
3498 * moving them.
3499 */
3500
Eric Dumazet5517d862007-05-08 00:32:57 -07003501 pwr_now += busiest->__cpu_power *
3502 min(busiest_load_per_task, max_load);
3503 pwr_now += this->__cpu_power *
3504 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 pwr_now /= SCHED_LOAD_SCALE;
3506
3507 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003508 tmp = sg_div_cpu_power(busiest,
3509 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003511 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003512 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
3514 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003515 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003516 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003517 tmp = sg_div_cpu_power(this,
3518 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003520 tmp = sg_div_cpu_power(this,
3521 busiest_load_per_task * SCHED_LOAD_SCALE);
3522 pwr_move += this->__cpu_power *
3523 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 pwr_move /= SCHED_LOAD_SCALE;
3525
3526 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003527 if (pwr_move > pwr_now)
3528 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 }
3530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return busiest;
3532
3533out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003534#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003535 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003536 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003538 if (this == group_leader && group_leader != group_min) {
3539 *imbalance = min_load_per_task;
3540 return group_min;
3541 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003542#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003543ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 *imbalance = 0;
3545 return NULL;
3546}
3547
3548/*
3549 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3550 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003551static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003552find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003553 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003555 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003556 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 int i;
3558
3559 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003560 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003561
3562 if (!cpu_isset(i, *cpus))
3563 continue;
3564
Ingo Molnar48f24c42006-07-03 00:25:40 -07003565 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003566 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Ingo Molnardd41f592007-07-09 18:51:59 +02003568 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003569 continue;
3570
Ingo Molnardd41f592007-07-09 18:51:59 +02003571 if (wl > max_load) {
3572 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003573 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 }
3575 }
3576
3577 return busiest;
3578}
3579
3580/*
Nick Piggin77391d72005-06-25 14:57:30 -07003581 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3582 * so long as it is large enough.
3583 */
3584#define MAX_PINNED_INTERVAL 512
3585
3586/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3588 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003590static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003591 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003592 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
Peter Williams43010652007-08-09 11:16:46 +02003594 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003597 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003598 unsigned long flags;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003599 int unlock_aggregate;
Nick Piggin5969fe02005-09-10 00:26:19 -07003600
Mike Travis7c16ec52008-04-04 18:11:11 -07003601 cpus_setall(*cpus);
3602
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003603 unlock_aggregate = get_aggregate(sd);
3604
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003605 /*
3606 * When power savings policy is enabled for the parent domain, idle
3607 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003608 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003609 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003610 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003611 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003612 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003613 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Ingo Molnar2d723762007-10-15 17:00:12 +02003615 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003617redo:
3618 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003619 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003620
Chen, Kenneth W06066712006-12-10 02:20:35 -08003621 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003622 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 if (!group) {
3625 schedstat_inc(sd, lb_nobusyg[idle]);
3626 goto out_balanced;
3627 }
3628
Mike Travis7c16ec52008-04-04 18:11:11 -07003629 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 if (!busiest) {
3631 schedstat_inc(sd, lb_nobusyq[idle]);
3632 goto out_balanced;
3633 }
3634
Nick Piggindb935db2005-06-25 14:57:11 -07003635 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
3637 schedstat_add(sd, lb_imbalance[idle], imbalance);
3638
Peter Williams43010652007-08-09 11:16:46 +02003639 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 if (busiest->nr_running > 1) {
3641 /*
3642 * Attempt to move tasks. If find_busiest_group has found
3643 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003644 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 * correctly treated as an imbalance.
3646 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003647 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003648 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003649 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003650 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003651 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003652 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003653
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003654 /*
3655 * some other cpu did the load balance for us.
3656 */
Peter Williams43010652007-08-09 11:16:46 +02003657 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003658 resched_cpu(this_cpu);
3659
Nick Piggin81026792005-06-25 14:57:07 -07003660 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003661 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003662 cpu_clear(cpu_of(busiest), *cpus);
3663 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003664 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003665 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 }
Nick Piggin81026792005-06-25 14:57:07 -07003668
Peter Williams43010652007-08-09 11:16:46 +02003669 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 schedstat_inc(sd, lb_failed[idle]);
3671 sd->nr_balance_failed++;
3672
3673 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003675 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003676
3677 /* don't kick the migration_thread, if the curr
3678 * task on busiest cpu can't be moved to this_cpu
3679 */
3680 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003681 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003682 all_pinned = 1;
3683 goto out_one_pinned;
3684 }
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 if (!busiest->active_balance) {
3687 busiest->active_balance = 1;
3688 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003689 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003691 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003692 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 wake_up_process(busiest->migration_thread);
3694
3695 /*
3696 * We've kicked active balancing, reset the failure
3697 * counter.
3698 */
Nick Piggin39507452005-06-25 14:57:09 -07003699 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 }
Nick Piggin81026792005-06-25 14:57:07 -07003701 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 sd->nr_balance_failed = 0;
3703
Nick Piggin81026792005-06-25 14:57:07 -07003704 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 /* We were unbalanced, so reset the balancing interval */
3706 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003707 } else {
3708 /*
3709 * If we've begun active balancing, start to back off. This
3710 * case may not be covered by the all_pinned logic if there
3711 * is only 1 task on the busy runqueue (because we don't call
3712 * move_tasks).
3713 */
3714 if (sd->balance_interval < sd->max_interval)
3715 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 }
3717
Peter Williams43010652007-08-09 11:16:46 +02003718 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003719 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003720 ld_moved = -1;
3721
3722 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
3724out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 schedstat_inc(sd, lb_balanced[idle]);
3726
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003727 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003728
3729out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003731 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3732 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 sd->balance_interval *= 2;
3734
Ingo Molnar48f24c42006-07-03 00:25:40 -07003735 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003736 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003737 ld_moved = -1;
3738 else
3739 ld_moved = 0;
3740out:
3741 if (unlock_aggregate)
3742 put_aggregate(sd);
3743 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744}
3745
3746/*
3747 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3748 * tasks if there is an imbalance.
3749 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003750 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 * this_rq is locked.
3752 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003753static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003754load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3755 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
3757 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003758 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003760 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003761 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003762 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003763
3764 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003765
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003766 /*
3767 * When power savings policy is enabled for the parent domain, idle
3768 * sibling can pick up load irrespective of busy siblings. In this case,
3769 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003770 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003771 */
3772 if (sd->flags & SD_SHARE_CPUPOWER &&
3773 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003774 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Ingo Molnar2d723762007-10-15 17:00:12 +02003776 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003777redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003778 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003779 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003781 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003782 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 }
3784
Mike Travis7c16ec52008-04-04 18:11:11 -07003785 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003786 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003787 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003788 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 }
3790
Nick Piggindb935db2005-06-25 14:57:11 -07003791 BUG_ON(busiest == this_rq);
3792
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003793 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003794
Peter Williams43010652007-08-09 11:16:46 +02003795 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003796 if (busiest->nr_running > 1) {
3797 /* Attempt to move tasks */
3798 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003799 /* this_rq->clock is already updated */
3800 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003801 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003802 imbalance, sd, CPU_NEWLY_IDLE,
3803 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003804 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003805
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003806 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003807 cpu_clear(cpu_of(busiest), *cpus);
3808 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003809 goto redo;
3810 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003811 }
3812
Peter Williams43010652007-08-09 11:16:46 +02003813 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003814 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003815 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3816 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003817 return -1;
3818 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003819 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
Peter Williams43010652007-08-09 11:16:46 +02003821 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003822
3823out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003824 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003825 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003826 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003827 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003828 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003829
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831}
3832
3833/*
3834 * idle_balance is called by schedule() if this_cpu is about to become
3835 * idle. Attempts to pull tasks from other CPUs.
3836 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003837static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
3839 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003840 int pulled_task = -1;
3841 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003842 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
3844 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003845 unsigned long interval;
3846
3847 if (!(sd->flags & SD_LOAD_BALANCE))
3848 continue;
3849
3850 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003851 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003852 pulled_task = load_balance_newidle(this_cpu, this_rq,
3853 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003854
3855 interval = msecs_to_jiffies(sd->balance_interval);
3856 if (time_after(next_balance, sd->last_balance + interval))
3857 next_balance = sd->last_balance + interval;
3858 if (pulled_task)
3859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003861 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003862 /*
3863 * We are going idle. next_balance may be set based on
3864 * a busy processor. So reset next_balance.
3865 */
3866 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868}
3869
3870/*
3871 * active_load_balance is run by migration threads. It pushes running tasks
3872 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3873 * running on each physical CPU where possible, and avoids physical /
3874 * logical imbalances.
3875 *
3876 * Called with busiest_rq locked.
3877 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003878static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
Nick Piggin39507452005-06-25 14:57:09 -07003880 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003881 struct sched_domain *sd;
3882 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003883
Ingo Molnar48f24c42006-07-03 00:25:40 -07003884 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003885 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003886 return;
3887
3888 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890 /*
Nick Piggin39507452005-06-25 14:57:09 -07003891 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003892 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003893 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 */
Nick Piggin39507452005-06-25 14:57:09 -07003895 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Nick Piggin39507452005-06-25 14:57:09 -07003897 /* move a task from busiest_rq to target_rq */
3898 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003899 update_rq_clock(busiest_rq);
3900 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
Nick Piggin39507452005-06-25 14:57:09 -07003902 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003903 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003904 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003905 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003906 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Ingo Molnar48f24c42006-07-03 00:25:40 -07003909 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003910 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
Peter Williams43010652007-08-09 11:16:46 +02003912 if (move_one_task(target_rq, target_cpu, busiest_rq,
3913 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003914 schedstat_inc(sd, alb_pushed);
3915 else
3916 schedstat_inc(sd, alb_failed);
3917 }
Nick Piggin39507452005-06-25 14:57:09 -07003918 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919}
3920
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003921#ifdef CONFIG_NO_HZ
3922static struct {
3923 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003924 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003925} nohz ____cacheline_aligned = {
3926 .load_balancer = ATOMIC_INIT(-1),
3927 .cpu_mask = CPU_MASK_NONE,
3928};
3929
Christoph Lameter7835b982006-12-10 02:20:22 -08003930/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003931 * This routine will try to nominate the ilb (idle load balancing)
3932 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3933 * load balancing on behalf of all those cpus. If all the cpus in the system
3934 * go into this tickless mode, then there will be no ilb owner (as there is
3935 * no need for one) and all the cpus will sleep till the next wakeup event
3936 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003937 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003938 * For the ilb owner, tick is not stopped. And this tick will be used
3939 * for idle load balancing. ilb owner will still be part of
3940 * nohz.cpu_mask..
3941 *
3942 * While stopping the tick, this cpu will become the ilb owner if there
3943 * is no other owner. And will be the owner till that cpu becomes busy
3944 * or if all cpus in the system stop their ticks at which point
3945 * there is no need for ilb owner.
3946 *
3947 * When the ilb owner becomes busy, it nominates another owner, during the
3948 * next busy scheduler_tick()
3949 */
3950int select_nohz_load_balancer(int stop_tick)
3951{
3952 int cpu = smp_processor_id();
3953
3954 if (stop_tick) {
3955 cpu_set(cpu, nohz.cpu_mask);
3956 cpu_rq(cpu)->in_nohz_recently = 1;
3957
3958 /*
3959 * If we are going offline and still the leader, give up!
3960 */
3961 if (cpu_is_offline(cpu) &&
3962 atomic_read(&nohz.load_balancer) == cpu) {
3963 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3964 BUG();
3965 return 0;
3966 }
3967
3968 /* time for ilb owner also to sleep */
3969 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3970 if (atomic_read(&nohz.load_balancer) == cpu)
3971 atomic_set(&nohz.load_balancer, -1);
3972 return 0;
3973 }
3974
3975 if (atomic_read(&nohz.load_balancer) == -1) {
3976 /* make me the ilb owner */
3977 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3978 return 1;
3979 } else if (atomic_read(&nohz.load_balancer) == cpu)
3980 return 1;
3981 } else {
3982 if (!cpu_isset(cpu, nohz.cpu_mask))
3983 return 0;
3984
3985 cpu_clear(cpu, nohz.cpu_mask);
3986
3987 if (atomic_read(&nohz.load_balancer) == cpu)
3988 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3989 BUG();
3990 }
3991 return 0;
3992}
3993#endif
3994
3995static DEFINE_SPINLOCK(balancing);
3996
3997/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003998 * It checks each scheduling domain to see if it is due to be balanced,
3999 * and initiates a balancing operation if so.
4000 *
4001 * Balancing parameters are set up in arch_init_sched_domains.
4002 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004003static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08004004{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004005 int balance = 1;
4006 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004007 unsigned long interval;
4008 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004009 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004010 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004011 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07004012 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004014 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 if (!(sd->flags & SD_LOAD_BALANCE))
4016 continue;
4017
4018 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004019 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 interval *= sd->busy_factor;
4021
4022 /* scale ms to jiffies */
4023 interval = msecs_to_jiffies(interval);
4024 if (unlikely(!interval))
4025 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004026 if (interval > HZ*NR_CPUS/10)
4027 interval = HZ*NR_CPUS/10;
4028
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Christoph Lameter08c183f2006-12-10 02:20:29 -08004030 if (sd->flags & SD_SERIALIZE) {
4031 if (!spin_trylock(&balancing))
4032 goto out;
4033 }
4034
Christoph Lameterc9819f42006-12-10 02:20:25 -08004035 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07004036 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004037 /*
4038 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004039 * longer idle, or one of our SMT siblings is
4040 * not idle.
4041 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004042 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004044 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08004046 if (sd->flags & SD_SERIALIZE)
4047 spin_unlock(&balancing);
4048out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004049 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004050 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004051 update_next_balance = 1;
4052 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004053
4054 /*
4055 * Stop the load balance at this level. There is another
4056 * CPU in our sched group which is doing load balancing more
4057 * actively.
4058 */
4059 if (!balance)
4060 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004062
4063 /*
4064 * next_balance will be updated only when there is a need.
4065 * When the cpu is attached to null domain for ex, it will not be
4066 * updated.
4067 */
4068 if (likely(update_next_balance))
4069 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004070}
4071
4072/*
4073 * run_rebalance_domains is triggered when needed from the scheduler tick.
4074 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4075 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4076 */
4077static void run_rebalance_domains(struct softirq_action *h)
4078{
Ingo Molnardd41f592007-07-09 18:51:59 +02004079 int this_cpu = smp_processor_id();
4080 struct rq *this_rq = cpu_rq(this_cpu);
4081 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4082 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004083
Ingo Molnardd41f592007-07-09 18:51:59 +02004084 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004085
4086#ifdef CONFIG_NO_HZ
4087 /*
4088 * If this cpu is the owner for idle load balancing, then do the
4089 * balancing on behalf of the other idle cpus whose ticks are
4090 * stopped.
4091 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004092 if (this_rq->idle_at_tick &&
4093 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004094 cpumask_t cpus = nohz.cpu_mask;
4095 struct rq *rq;
4096 int balance_cpu;
4097
Ingo Molnardd41f592007-07-09 18:51:59 +02004098 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004099 for_each_cpu_mask(balance_cpu, cpus) {
4100 /*
4101 * If this cpu gets work to do, stop the load balancing
4102 * work being done for other cpus. Next load
4103 * balancing owner will pick it up.
4104 */
4105 if (need_resched())
4106 break;
4107
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004108 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004109
4110 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004111 if (time_after(this_rq->next_balance, rq->next_balance))
4112 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004113 }
4114 }
4115#endif
4116}
4117
4118/*
4119 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4120 *
4121 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4122 * idle load balancing owner or decide to stop the periodic load balancing,
4123 * if the whole system is idle.
4124 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004125static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004126{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004127#ifdef CONFIG_NO_HZ
4128 /*
4129 * If we were in the nohz mode recently and busy at the current
4130 * scheduler tick, then check if we need to nominate new idle
4131 * load balancer.
4132 */
4133 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4134 rq->in_nohz_recently = 0;
4135
4136 if (atomic_read(&nohz.load_balancer) == cpu) {
4137 cpu_clear(cpu, nohz.cpu_mask);
4138 atomic_set(&nohz.load_balancer, -1);
4139 }
4140
4141 if (atomic_read(&nohz.load_balancer) == -1) {
4142 /*
4143 * simple selection for now: Nominate the
4144 * first cpu in the nohz list to be the next
4145 * ilb owner.
4146 *
4147 * TBD: Traverse the sched domains and nominate
4148 * the nearest cpu in the nohz.cpu_mask.
4149 */
4150 int ilb = first_cpu(nohz.cpu_mask);
4151
Mike Travis434d53b2008-04-04 18:11:04 -07004152 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004153 resched_cpu(ilb);
4154 }
4155 }
4156
4157 /*
4158 * If this cpu is idle and doing idle load balancing for all the
4159 * cpus with ticks stopped, is it time for that to stop?
4160 */
4161 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4162 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4163 resched_cpu(cpu);
4164 return;
4165 }
4166
4167 /*
4168 * If this cpu is idle and the idle load balancing is done by
4169 * someone else, then no need raise the SCHED_SOFTIRQ
4170 */
4171 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4172 cpu_isset(cpu, nohz.cpu_mask))
4173 return;
4174#endif
4175 if (time_after_eq(jiffies, rq->next_balance))
4176 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177}
Ingo Molnardd41f592007-07-09 18:51:59 +02004178
4179#else /* CONFIG_SMP */
4180
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181/*
4182 * on UP we do not need to balance between CPUs:
4183 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004184static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185{
4186}
Ingo Molnardd41f592007-07-09 18:51:59 +02004187
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188#endif
4189
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190DEFINE_PER_CPU(struct kernel_stat, kstat);
4191
4192EXPORT_PER_CPU_SYMBOL(kstat);
4193
4194/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004195 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4196 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004198unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004201 u64 ns, delta_exec;
4202 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004203
Ingo Molnar41b86e92007-07-09 18:51:58 +02004204 rq = task_rq_lock(p, &flags);
4205 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004206 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004207 update_rq_clock(rq);
4208 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004209 if ((s64)delta_exec > 0)
4210 ns += delta_exec;
4211 }
4212 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004213
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 return ns;
4215}
4216
4217/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 * Account user cpu time to a process.
4219 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 * @cputime: the cpu time spent in user space since the last update
4221 */
4222void account_user_time(struct task_struct *p, cputime_t cputime)
4223{
4224 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4225 cputime64_t tmp;
4226
4227 p->utime = cputime_add(p->utime, cputime);
4228
4229 /* Add user time to cpustat. */
4230 tmp = cputime_to_cputime64(cputime);
4231 if (TASK_NICE(p) > 0)
4232 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4233 else
4234 cpustat->user = cputime64_add(cpustat->user, tmp);
4235}
4236
4237/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004238 * Account guest cpu time to a process.
4239 * @p: the process that the cpu time gets accounted to
4240 * @cputime: the cpu time spent in virtual machine since the last update
4241 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004242static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004243{
4244 cputime64_t tmp;
4245 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4246
4247 tmp = cputime_to_cputime64(cputime);
4248
4249 p->utime = cputime_add(p->utime, cputime);
4250 p->gtime = cputime_add(p->gtime, cputime);
4251
4252 cpustat->user = cputime64_add(cpustat->user, tmp);
4253 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4254}
4255
4256/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004257 * Account scaled user cpu time to a process.
4258 * @p: the process that the cpu time gets accounted to
4259 * @cputime: the cpu time spent in user space since the last update
4260 */
4261void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4262{
4263 p->utimescaled = cputime_add(p->utimescaled, cputime);
4264}
4265
4266/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 * Account system cpu time to a process.
4268 * @p: the process that the cpu time gets accounted to
4269 * @hardirq_offset: the offset to subtract from hardirq_count()
4270 * @cputime: the cpu time spent in kernel space since the last update
4271 */
4272void account_system_time(struct task_struct *p, int hardirq_offset,
4273 cputime_t cputime)
4274{
4275 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004276 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 cputime64_t tmp;
4278
Christian Borntraeger97783852007-11-15 20:57:39 +01004279 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
4280 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02004281
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 p->stime = cputime_add(p->stime, cputime);
4283
4284 /* Add system time to cpustat. */
4285 tmp = cputime_to_cputime64(cputime);
4286 if (hardirq_count() - hardirq_offset)
4287 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4288 else if (softirq_count())
4289 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004290 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004292 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4294 else
4295 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4296 /* Account for system time used */
4297 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298}
4299
4300/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004301 * Account scaled system cpu time to a process.
4302 * @p: the process that the cpu time gets accounted to
4303 * @hardirq_offset: the offset to subtract from hardirq_count()
4304 * @cputime: the cpu time spent in kernel space since the last update
4305 */
4306void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4307{
4308 p->stimescaled = cputime_add(p->stimescaled, cputime);
4309}
4310
4311/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 * Account for involuntary wait time.
4313 * @p: the process from which the cpu time has been stolen
4314 * @steal: the cpu time spent in involuntary wait
4315 */
4316void account_steal_time(struct task_struct *p, cputime_t steal)
4317{
4318 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4319 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004320 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
4322 if (p == rq->idle) {
4323 p->stime = cputime_add(p->stime, steal);
4324 if (atomic_read(&rq->nr_iowait) > 0)
4325 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4326 else
4327 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004328 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4330}
4331
Christoph Lameter7835b982006-12-10 02:20:22 -08004332/*
4333 * This function gets called by the timer code, with HZ frequency.
4334 * We call it with interrupts disabled.
4335 *
4336 * It also gets called by the fork code, when changing the parent's
4337 * timeslices.
4338 */
4339void scheduler_tick(void)
4340{
Christoph Lameter7835b982006-12-10 02:20:22 -08004341 int cpu = smp_processor_id();
4342 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004343 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02004344 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08004345
Ingo Molnardd41f592007-07-09 18:51:59 +02004346 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02004347 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02004348 /*
4349 * Let rq->clock advance by at least TICK_NSEC:
4350 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004351 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02004352 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004353 rq->clock_underflows++;
4354 }
Ingo Molnar529c7722007-08-10 23:05:11 +02004355 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02004356 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004357 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004358 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004359 spin_unlock(&rq->lock);
4360
Christoph Lametere418e1c2006-12-10 02:20:23 -08004361#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004362 rq->idle_at_tick = idle_cpu(cpu);
4363 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365}
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
4368
Srinivasa Ds43627582008-02-23 15:24:04 -08004369void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
4371 /*
4372 * Underflow?
4373 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004374 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4375 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 preempt_count() += val;
4377 /*
4378 * Spinlock count overflowing soon?
4379 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004380 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4381 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382}
4383EXPORT_SYMBOL(add_preempt_count);
4384
Srinivasa Ds43627582008-02-23 15:24:04 -08004385void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386{
4387 /*
4388 * Underflow?
4389 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004390 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4391 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 /*
4393 * Is the spinlock portion underflowing?
4394 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004395 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4396 !(preempt_count() & PREEMPT_MASK)))
4397 return;
4398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 preempt_count() -= val;
4400}
4401EXPORT_SYMBOL(sub_preempt_count);
4402
4403#endif
4404
4405/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004406 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004408static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
Satyam Sharma838225b2007-10-24 18:23:50 +02004410 struct pt_regs *regs = get_irq_regs();
4411
4412 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4413 prev->comm, prev->pid, preempt_count());
4414
Ingo Molnardd41f592007-07-09 18:51:59 +02004415 debug_show_held_locks(prev);
4416 if (irqs_disabled())
4417 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004418
4419 if (regs)
4420 show_regs(regs);
4421 else
4422 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004423}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Ingo Molnardd41f592007-07-09 18:51:59 +02004425/*
4426 * Various schedule()-time debugging checks and statistics:
4427 */
4428static inline void schedule_debug(struct task_struct *prev)
4429{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004431 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 * schedule() atomically, we ignore that path for now.
4433 * Otherwise, whine if we are scheduling when we should not be.
4434 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004435 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4436 __schedule_bug(prev);
4437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4439
Ingo Molnar2d723762007-10-15 17:00:12 +02004440 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004441#ifdef CONFIG_SCHEDSTATS
4442 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004443 schedstat_inc(this_rq(), bkl_count);
4444 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004445 }
4446#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004447}
4448
4449/*
4450 * Pick up the highest-prio task:
4451 */
4452static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004453pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004454{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004455 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004456 struct task_struct *p;
4457
4458 /*
4459 * Optimization: we know that if all tasks are in
4460 * the fair class we can call that function directly:
4461 */
4462 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004463 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004464 if (likely(p))
4465 return p;
4466 }
4467
4468 class = sched_class_highest;
4469 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004470 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004471 if (p)
4472 return p;
4473 /*
4474 * Will never be NULL as the idle class always
4475 * returns a non-NULL p:
4476 */
4477 class = class->next;
4478 }
4479}
4480
4481/*
4482 * schedule() is the main scheduler function.
4483 */
4484asmlinkage void __sched schedule(void)
4485{
4486 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004487 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004488 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004489 int cpu;
4490
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491need_resched:
4492 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004493 cpu = smp_processor_id();
4494 rq = cpu_rq(cpu);
4495 rcu_qsctr_inc(cpu);
4496 prev = rq->curr;
4497 switch_count = &prev->nivcsw;
4498
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 release_kernel_lock(prev);
4500need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Ingo Molnardd41f592007-07-09 18:51:59 +02004502 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004504 hrtick_clear(rq);
4505
Ingo Molnar1e819952007-10-15 17:00:13 +02004506 /*
4507 * Do the rq-clock update outside the rq lock:
4508 */
4509 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004510 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004511 spin_lock(&rq->lock);
4512 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Ingo Molnardd41f592007-07-09 18:51:59 +02004514 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4515 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004516 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004517 prev->state = TASK_RUNNING;
4518 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004519 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004520 }
4521 switch_count = &prev->nvcsw;
4522 }
4523
Steven Rostedt9a897c52008-01-25 21:08:22 +01004524#ifdef CONFIG_SMP
4525 if (prev->sched_class->pre_schedule)
4526 prev->sched_class->pre_schedule(rq, prev);
4527#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004528
Ingo Molnardd41f592007-07-09 18:51:59 +02004529 if (unlikely(!rq->nr_running))
4530 idle_balance(cpu, rq);
4531
Ingo Molnar31ee5292007-08-09 11:16:49 +02004532 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004533 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
4535 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02004536
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 rq->nr_switches++;
4539 rq->curr = next;
4540 ++*switch_count;
4541
Ingo Molnardd41f592007-07-09 18:51:59 +02004542 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004543 /*
4544 * the context switch might have flipped the stack from under
4545 * us, hence refresh the local variables.
4546 */
4547 cpu = smp_processor_id();
4548 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 } else
4550 spin_unlock_irq(&rq->lock);
4551
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004552 hrtick_set(rq);
4553
4554 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 preempt_enable_no_resched();
4558 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4559 goto need_resched;
4560}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561EXPORT_SYMBOL(schedule);
4562
4563#ifdef CONFIG_PREEMPT
4564/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004565 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004566 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 * occur there and call schedule directly.
4568 */
4569asmlinkage void __sched preempt_schedule(void)
4570{
4571 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 struct task_struct *task = current;
4573 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 /*
4576 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004577 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004579 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 return;
4581
Andi Kleen3a5c3592007-10-15 17:00:14 +02004582 do {
4583 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
Andi Kleen3a5c3592007-10-15 17:00:14 +02004585 /*
4586 * We keep the big kernel semaphore locked, but we
4587 * clear ->lock_depth so that schedule() doesnt
4588 * auto-release the semaphore:
4589 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004590 saved_lock_depth = task->lock_depth;
4591 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004592 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004593 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004594 sub_preempt_count(PREEMPT_ACTIVE);
4595
4596 /*
4597 * Check again in case we missed a preemption opportunity
4598 * between schedule and now.
4599 */
4600 barrier();
4601 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603EXPORT_SYMBOL(preempt_schedule);
4604
4605/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004606 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 * off of irq context.
4608 * Note, that this is called and return with irqs disabled. This will
4609 * protect us against recursive calling from irq.
4610 */
4611asmlinkage void __sched preempt_schedule_irq(void)
4612{
4613 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 struct task_struct *task = current;
4615 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004616
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004617 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 BUG_ON(ti->preempt_count || !irqs_disabled());
4619
Andi Kleen3a5c3592007-10-15 17:00:14 +02004620 do {
4621 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
Andi Kleen3a5c3592007-10-15 17:00:14 +02004623 /*
4624 * We keep the big kernel semaphore locked, but we
4625 * clear ->lock_depth so that schedule() doesnt
4626 * auto-release the semaphore:
4627 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004628 saved_lock_depth = task->lock_depth;
4629 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004630 local_irq_enable();
4631 schedule();
4632 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004633 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004634 sub_preempt_count(PREEMPT_ACTIVE);
4635
4636 /*
4637 * Check again in case we missed a preemption opportunity
4638 * between schedule and now.
4639 */
4640 barrier();
4641 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642}
4643
4644#endif /* CONFIG_PREEMPT */
4645
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004646int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4647 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004649 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651EXPORT_SYMBOL(default_wake_function);
4652
4653/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004654 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4655 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 * number) then we wake all the non-exclusive tasks and one exclusive task.
4657 *
4658 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004659 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4661 */
4662static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4663 int nr_exclusive, int sync, void *key)
4664{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004665 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004667 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004668 unsigned flags = curr->flags;
4669
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004671 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 break;
4673 }
4674}
4675
4676/**
4677 * __wake_up - wake up threads blocked on a waitqueue.
4678 * @q: the waitqueue
4679 * @mode: which threads
4680 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004681 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004683void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004684 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685{
4686 unsigned long flags;
4687
4688 spin_lock_irqsave(&q->lock, flags);
4689 __wake_up_common(q, mode, nr_exclusive, 0, key);
4690 spin_unlock_irqrestore(&q->lock, flags);
4691}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692EXPORT_SYMBOL(__wake_up);
4693
4694/*
4695 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4696 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004697void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698{
4699 __wake_up_common(q, mode, 1, 0, NULL);
4700}
4701
4702/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004703 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 * @q: the waitqueue
4705 * @mode: which threads
4706 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4707 *
4708 * The sync wakeup differs that the waker knows that it will schedule
4709 * away soon, so while the target thread will be woken up, it will not
4710 * be migrated to another CPU - ie. the two threads are 'synchronized'
4711 * with each other. This can prevent needless bouncing between CPUs.
4712 *
4713 * On UP it can prevent extra preemption.
4714 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004715void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004716__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717{
4718 unsigned long flags;
4719 int sync = 1;
4720
4721 if (unlikely(!q))
4722 return;
4723
4724 if (unlikely(!nr_exclusive))
4725 sync = 0;
4726
4727 spin_lock_irqsave(&q->lock, flags);
4728 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4729 spin_unlock_irqrestore(&q->lock, flags);
4730}
4731EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4732
Ingo Molnarb15136e2007-10-24 18:23:48 +02004733void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734{
4735 unsigned long flags;
4736
4737 spin_lock_irqsave(&x->wait.lock, flags);
4738 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004739 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 spin_unlock_irqrestore(&x->wait.lock, flags);
4741}
4742EXPORT_SYMBOL(complete);
4743
Ingo Molnarb15136e2007-10-24 18:23:48 +02004744void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745{
4746 unsigned long flags;
4747
4748 spin_lock_irqsave(&x->wait.lock, flags);
4749 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004750 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 spin_unlock_irqrestore(&x->wait.lock, flags);
4752}
4753EXPORT_SYMBOL(complete_all);
4754
Andi Kleen8cbbe862007-10-15 17:00:14 +02004755static inline long __sched
4756do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 if (!x->done) {
4759 DECLARE_WAITQUEUE(wait, current);
4760
4761 wait.flags |= WQ_FLAG_EXCLUSIVE;
4762 __add_wait_queue_tail(&x->wait, &wait);
4763 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004764 if ((state == TASK_INTERRUPTIBLE &&
4765 signal_pending(current)) ||
4766 (state == TASK_KILLABLE &&
4767 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004768 __remove_wait_queue(&x->wait, &wait);
4769 return -ERESTARTSYS;
4770 }
4771 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004773 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004775 if (!timeout) {
4776 __remove_wait_queue(&x->wait, &wait);
4777 return timeout;
4778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 } while (!x->done);
4780 __remove_wait_queue(&x->wait, &wait);
4781 }
4782 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004783 return timeout;
4784}
4785
4786static long __sched
4787wait_for_common(struct completion *x, long timeout, int state)
4788{
4789 might_sleep();
4790
4791 spin_lock_irq(&x->wait.lock);
4792 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004794 return timeout;
4795}
4796
Ingo Molnarb15136e2007-10-24 18:23:48 +02004797void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004798{
4799 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800}
4801EXPORT_SYMBOL(wait_for_completion);
4802
Ingo Molnarb15136e2007-10-24 18:23:48 +02004803unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4805{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004806 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807}
4808EXPORT_SYMBOL(wait_for_completion_timeout);
4809
Andi Kleen8cbbe862007-10-15 17:00:14 +02004810int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
Andi Kleen51e97992007-10-18 21:32:55 +02004812 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4813 if (t == -ERESTARTSYS)
4814 return t;
4815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817EXPORT_SYMBOL(wait_for_completion_interruptible);
4818
Ingo Molnarb15136e2007-10-24 18:23:48 +02004819unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820wait_for_completion_interruptible_timeout(struct completion *x,
4821 unsigned long timeout)
4822{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004823 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824}
4825EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4826
Matthew Wilcox009e5772007-12-06 12:29:54 -05004827int __sched wait_for_completion_killable(struct completion *x)
4828{
4829 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4830 if (t == -ERESTARTSYS)
4831 return t;
4832 return 0;
4833}
4834EXPORT_SYMBOL(wait_for_completion_killable);
4835
Andi Kleen8cbbe862007-10-15 17:00:14 +02004836static long __sched
4837sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004838{
4839 unsigned long flags;
4840 wait_queue_t wait;
4841
4842 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Andi Kleen8cbbe862007-10-15 17:00:14 +02004844 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Andi Kleen8cbbe862007-10-15 17:00:14 +02004846 spin_lock_irqsave(&q->lock, flags);
4847 __add_wait_queue(q, &wait);
4848 spin_unlock(&q->lock);
4849 timeout = schedule_timeout(timeout);
4850 spin_lock_irq(&q->lock);
4851 __remove_wait_queue(q, &wait);
4852 spin_unlock_irqrestore(&q->lock, flags);
4853
4854 return timeout;
4855}
4856
4857void __sched interruptible_sleep_on(wait_queue_head_t *q)
4858{
4859 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861EXPORT_SYMBOL(interruptible_sleep_on);
4862
Ingo Molnar0fec1712007-07-09 18:52:01 +02004863long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004864interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004866 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4869
Ingo Molnar0fec1712007-07-09 18:52:01 +02004870void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004872 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874EXPORT_SYMBOL(sleep_on);
4875
Ingo Molnar0fec1712007-07-09 18:52:01 +02004876long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004878 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880EXPORT_SYMBOL(sleep_on_timeout);
4881
Ingo Molnarb29739f2006-06-27 02:54:51 -07004882#ifdef CONFIG_RT_MUTEXES
4883
4884/*
4885 * rt_mutex_setprio - set the current priority of a task
4886 * @p: task
4887 * @prio: prio value (kernel-internal form)
4888 *
4889 * This function changes the 'effective' priority of a task. It does
4890 * not touch ->normal_prio like __setscheduler().
4891 *
4892 * Used by the rt_mutex code to implement priority inheritance logic.
4893 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004894void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004895{
4896 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004897 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004898 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004899 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004900
4901 BUG_ON(prio < 0 || prio > MAX_PRIO);
4902
4903 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004904 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004905
Andrew Mortond5f9f942007-05-08 20:27:06 -07004906 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004907 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004908 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004909 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004910 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004911 if (running)
4912 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004913
4914 if (rt_prio(prio))
4915 p->sched_class = &rt_sched_class;
4916 else
4917 p->sched_class = &fair_sched_class;
4918
Ingo Molnarb29739f2006-06-27 02:54:51 -07004919 p->prio = prio;
4920
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004921 if (running)
4922 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004923 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004924 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004925
4926 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004927 }
4928 task_rq_unlock(rq, &flags);
4929}
4930
4931#endif
4932
Ingo Molnar36c8b582006-07-03 00:25:41 -07004933void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934{
Ingo Molnardd41f592007-07-09 18:51:59 +02004935 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004937 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
4939 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4940 return;
4941 /*
4942 * We have to be careful, if called from sys_setpriority(),
4943 * the task might be in the middle of scheduling on another CPU.
4944 */
4945 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004946 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 /*
4948 * The RT priorities are set via sched_setscheduler(), but we still
4949 * allow the 'normal' nice value to be set - but as expected
4950 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004951 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004953 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 p->static_prio = NICE_TO_PRIO(nice);
4955 goto out_unlock;
4956 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004957 on_rq = p->se.on_rq;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02004958 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004959 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004962 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004963 old_prio = p->prio;
4964 p->prio = effective_prio(p);
4965 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
Ingo Molnardd41f592007-07-09 18:51:59 +02004967 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004968 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004970 * If the task increased its priority or is running and
4971 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004973 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 resched_task(rq->curr);
4975 }
4976out_unlock:
4977 task_rq_unlock(rq, &flags);
4978}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979EXPORT_SYMBOL(set_user_nice);
4980
Matt Mackalle43379f2005-05-01 08:59:00 -07004981/*
4982 * can_nice - check if a task can reduce its nice value
4983 * @p: task
4984 * @nice: nice value
4985 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004986int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004987{
Matt Mackall024f4742005-08-18 11:24:19 -07004988 /* convert nice value [19,-20] to rlimit style value [1,40] */
4989 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004990
Matt Mackalle43379f2005-05-01 08:59:00 -07004991 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4992 capable(CAP_SYS_NICE));
4993}
4994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995#ifdef __ARCH_WANT_SYS_NICE
4996
4997/*
4998 * sys_nice - change the priority of the current process.
4999 * @increment: priority increment
5000 *
5001 * sys_setpriority is a more generic, but much slower function that
5002 * does similar things.
5003 */
5004asmlinkage long sys_nice(int increment)
5005{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005006 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
5008 /*
5009 * Setpriority might change our priority at the same moment.
5010 * We don't have to worry. Conceptually one call occurs first
5011 * and we have a single winner.
5012 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005013 if (increment < -40)
5014 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 if (increment > 40)
5016 increment = 40;
5017
5018 nice = PRIO_TO_NICE(current->static_prio) + increment;
5019 if (nice < -20)
5020 nice = -20;
5021 if (nice > 19)
5022 nice = 19;
5023
Matt Mackalle43379f2005-05-01 08:59:00 -07005024 if (increment < 0 && !can_nice(current, nice))
5025 return -EPERM;
5026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 retval = security_task_setnice(current, nice);
5028 if (retval)
5029 return retval;
5030
5031 set_user_nice(current, nice);
5032 return 0;
5033}
5034
5035#endif
5036
5037/**
5038 * task_prio - return the priority value of a given task.
5039 * @p: the task in question.
5040 *
5041 * This is the priority value as seen by users in /proc.
5042 * RT tasks are offset by -200. Normal tasks are centered
5043 * around 0, value goes from -16 to +15.
5044 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005045int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046{
5047 return p->prio - MAX_RT_PRIO;
5048}
5049
5050/**
5051 * task_nice - return the nice value of a given task.
5052 * @p: the task in question.
5053 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005054int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055{
5056 return TASK_NICE(p);
5057}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005058EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
5060/**
5061 * idle_cpu - is a given cpu idle currently?
5062 * @cpu: the processor in question.
5063 */
5064int idle_cpu(int cpu)
5065{
5066 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5067}
5068
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069/**
5070 * idle_task - return the idle task for a given cpu.
5071 * @cpu: the processor in question.
5072 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005073struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074{
5075 return cpu_rq(cpu)->idle;
5076}
5077
5078/**
5079 * find_process_by_pid - find a process with a matching PID value.
5080 * @pid: the pid in question.
5081 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005082static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005084 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085}
5086
5087/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005088static void
5089__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090{
Ingo Molnardd41f592007-07-09 18:51:59 +02005091 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005092
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005094 switch (p->policy) {
5095 case SCHED_NORMAL:
5096 case SCHED_BATCH:
5097 case SCHED_IDLE:
5098 p->sched_class = &fair_sched_class;
5099 break;
5100 case SCHED_FIFO:
5101 case SCHED_RR:
5102 p->sched_class = &rt_sched_class;
5103 break;
5104 }
5105
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005107 p->normal_prio = normal_prio(p);
5108 /* we are holding p->pi_lock already */
5109 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005110 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111}
5112
5113/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005114 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 * @p: the task in question.
5116 * @policy: new policy.
5117 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005118 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005119 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005121int sched_setscheduler(struct task_struct *p, int policy,
5122 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005124 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005126 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005127 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
Steven Rostedt66e53932006-06-27 02:54:44 -07005129 /* may grab non-irq protected spin_locks */
5130 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131recheck:
5132 /* double check policy once rq lock held */
5133 if (policy < 0)
5134 policy = oldpolicy = p->policy;
5135 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005136 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5137 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005138 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 /*
5140 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005141 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5142 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 */
5144 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005145 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005146 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005148 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 return -EINVAL;
5150
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005151 /*
5152 * Allow unprivileged RT tasks to decrease priority:
5153 */
5154 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005155 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005156 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005157
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005158 if (!lock_task_sighand(p, &flags))
5159 return -ESRCH;
5160 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5161 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005162
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005163 /* can't set/change the rt policy */
5164 if (policy != p->policy && !rlim_rtprio)
5165 return -EPERM;
5166
5167 /* can't increase priority */
5168 if (param->sched_priority > p->rt_priority &&
5169 param->sched_priority > rlim_rtprio)
5170 return -EPERM;
5171 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005172 /*
5173 * Like positive nice levels, dont allow tasks to
5174 * move out of SCHED_IDLE either:
5175 */
5176 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5177 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005178
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005179 /* can't change other user's priorities */
5180 if ((current->euid != p->euid) &&
5181 (current->euid != p->uid))
5182 return -EPERM;
5183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005185#ifdef CONFIG_RT_GROUP_SCHED
5186 /*
5187 * Do not allow realtime tasks into groups that have no runtime
5188 * assigned.
5189 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02005190 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005191 return -EPERM;
5192#endif
5193
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 retval = security_task_setscheduler(p, policy, param);
5195 if (retval)
5196 return retval;
5197 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005198 * make sure no PI-waiters arrive (or leave) while we are
5199 * changing the priority of the task:
5200 */
5201 spin_lock_irqsave(&p->pi_lock, flags);
5202 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 * To be able to change p->policy safely, the apropriate
5204 * runqueue lock must be held.
5205 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005206 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 /* recheck policy now with rq lock held */
5208 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5209 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005210 __task_rq_unlock(rq);
5211 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 goto recheck;
5213 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005214 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005215 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005216 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005217 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005218 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005219 if (running)
5220 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005221
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005223 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005224
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005225 if (running)
5226 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005227 if (on_rq) {
5228 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005229
5230 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005232 __task_rq_unlock(rq);
5233 spin_unlock_irqrestore(&p->pi_lock, flags);
5234
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005235 rt_mutex_adjust_pi(p);
5236
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 return 0;
5238}
5239EXPORT_SYMBOL_GPL(sched_setscheduler);
5240
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005241static int
5242do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 struct sched_param lparam;
5245 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005246 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
5248 if (!param || pid < 0)
5249 return -EINVAL;
5250 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5251 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005252
5253 rcu_read_lock();
5254 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005256 if (p != NULL)
5257 retval = sched_setscheduler(p, policy, &lparam);
5258 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 return retval;
5261}
5262
5263/**
5264 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5265 * @pid: the pid in question.
5266 * @policy: new policy.
5267 * @param: structure containing the new RT priority.
5268 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005269asmlinkage long
5270sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271{
Jason Baronc21761f2006-01-18 17:43:03 -08005272 /* negative values for policy are not valid */
5273 if (policy < 0)
5274 return -EINVAL;
5275
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 return do_sched_setscheduler(pid, policy, param);
5277}
5278
5279/**
5280 * sys_sched_setparam - set/change the RT priority of a thread
5281 * @pid: the pid in question.
5282 * @param: structure containing the new RT priority.
5283 */
5284asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5285{
5286 return do_sched_setscheduler(pid, -1, param);
5287}
5288
5289/**
5290 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5291 * @pid: the pid in question.
5292 */
5293asmlinkage long sys_sched_getscheduler(pid_t pid)
5294{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005295 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005296 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
5298 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005299 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
5301 retval = -ESRCH;
5302 read_lock(&tasklist_lock);
5303 p = find_process_by_pid(pid);
5304 if (p) {
5305 retval = security_task_getscheduler(p);
5306 if (!retval)
5307 retval = p->policy;
5308 }
5309 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 return retval;
5311}
5312
5313/**
5314 * sys_sched_getscheduler - get the RT priority of a thread
5315 * @pid: the pid in question.
5316 * @param: structure containing the RT priority.
5317 */
5318asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5319{
5320 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005321 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005322 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
5324 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005325 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
5327 read_lock(&tasklist_lock);
5328 p = find_process_by_pid(pid);
5329 retval = -ESRCH;
5330 if (!p)
5331 goto out_unlock;
5332
5333 retval = security_task_getscheduler(p);
5334 if (retval)
5335 goto out_unlock;
5336
5337 lp.sched_priority = p->rt_priority;
5338 read_unlock(&tasklist_lock);
5339
5340 /*
5341 * This one might sleep, we cannot do it with a spinlock held ...
5342 */
5343 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5344
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 return retval;
5346
5347out_unlock:
5348 read_unlock(&tasklist_lock);
5349 return retval;
5350}
5351
Mike Travisb53e9212008-04-04 18:11:08 -07005352long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005355 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005356 struct task_struct *p;
5357 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005359 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 read_lock(&tasklist_lock);
5361
5362 p = find_process_by_pid(pid);
5363 if (!p) {
5364 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005365 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 return -ESRCH;
5367 }
5368
5369 /*
5370 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005371 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 * usage count and then drop tasklist_lock.
5373 */
5374 get_task_struct(p);
5375 read_unlock(&tasklist_lock);
5376
5377 retval = -EPERM;
5378 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5379 !capable(CAP_SYS_NICE))
5380 goto out_unlock;
5381
David Quigleye7834f82006-06-23 02:03:59 -07005382 retval = security_task_setscheduler(p, 0, NULL);
5383 if (retval)
5384 goto out_unlock;
5385
Mike Travisf9a86fc2008-04-04 18:11:07 -07005386 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005388 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005389 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390
Paul Menage8707d8b2007-10-18 23:40:22 -07005391 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005392 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005393 if (!cpus_subset(new_mask, cpus_allowed)) {
5394 /*
5395 * We must have raced with a concurrent cpuset
5396 * update. Just reset the cpus_allowed to the
5397 * cpuset's cpus_allowed
5398 */
5399 new_mask = cpus_allowed;
5400 goto again;
5401 }
5402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403out_unlock:
5404 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005405 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 return retval;
5407}
5408
5409static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5410 cpumask_t *new_mask)
5411{
5412 if (len < sizeof(cpumask_t)) {
5413 memset(new_mask, 0, sizeof(cpumask_t));
5414 } else if (len > sizeof(cpumask_t)) {
5415 len = sizeof(cpumask_t);
5416 }
5417 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5418}
5419
5420/**
5421 * sys_sched_setaffinity - set the cpu affinity of a process
5422 * @pid: pid of the process
5423 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5424 * @user_mask_ptr: user-space pointer to the new cpu mask
5425 */
5426asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5427 unsigned long __user *user_mask_ptr)
5428{
5429 cpumask_t new_mask;
5430 int retval;
5431
5432 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5433 if (retval)
5434 return retval;
5435
Mike Travisb53e9212008-04-04 18:11:08 -07005436 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437}
5438
5439/*
5440 * Represents all cpu's present in the system
5441 * In systems capable of hotplug, this map could dynamically grow
5442 * as new cpu's are detected in the system via any platform specific
5443 * method, such as ACPI for e.g.
5444 */
5445
Andi Kleen4cef0c62006-01-11 22:44:57 +01005446cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447EXPORT_SYMBOL(cpu_present_map);
5448
5449#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005450cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005451EXPORT_SYMBOL(cpu_online_map);
5452
Andi Kleen4cef0c62006-01-11 22:44:57 +01005453cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005454EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455#endif
5456
5457long sched_getaffinity(pid_t pid, cpumask_t *mask)
5458{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005459 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005462 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 read_lock(&tasklist_lock);
5464
5465 retval = -ESRCH;
5466 p = find_process_by_pid(pid);
5467 if (!p)
5468 goto out_unlock;
5469
David Quigleye7834f82006-06-23 02:03:59 -07005470 retval = security_task_getscheduler(p);
5471 if (retval)
5472 goto out_unlock;
5473
Jack Steiner2f7016d2006-02-01 03:05:18 -08005474 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475
5476out_unlock:
5477 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005478 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
Ulrich Drepper9531b622007-08-09 11:16:46 +02005480 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481}
5482
5483/**
5484 * sys_sched_getaffinity - get the cpu affinity of a process
5485 * @pid: pid of the process
5486 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5487 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5488 */
5489asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5490 unsigned long __user *user_mask_ptr)
5491{
5492 int ret;
5493 cpumask_t mask;
5494
5495 if (len < sizeof(cpumask_t))
5496 return -EINVAL;
5497
5498 ret = sched_getaffinity(pid, &mask);
5499 if (ret < 0)
5500 return ret;
5501
5502 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5503 return -EFAULT;
5504
5505 return sizeof(cpumask_t);
5506}
5507
5508/**
5509 * sys_sched_yield - yield the current processor to other threads.
5510 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005511 * This function yields the current CPU to other tasks. If there are no
5512 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 */
5514asmlinkage long sys_sched_yield(void)
5515{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005516 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
Ingo Molnar2d723762007-10-15 17:00:12 +02005518 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005519 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
5521 /*
5522 * Since we are going to call schedule() anyway, there's
5523 * no need to preempt or enable interrupts:
5524 */
5525 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005526 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 _raw_spin_unlock(&rq->lock);
5528 preempt_enable_no_resched();
5529
5530 schedule();
5531
5532 return 0;
5533}
5534
Andrew Mortone7b38402006-06-30 01:56:00 -07005535static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005537#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5538 __might_sleep(__FILE__, __LINE__);
5539#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005540 /*
5541 * The BKS might be reacquired before we have dropped
5542 * PREEMPT_ACTIVE, which could trigger a second
5543 * cond_resched() call.
5544 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 do {
5546 add_preempt_count(PREEMPT_ACTIVE);
5547 schedule();
5548 sub_preempt_count(PREEMPT_ACTIVE);
5549 } while (need_resched());
5550}
5551
Herbert Xu02b67cc2008-01-25 21:08:28 +01005552#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5553int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554{
Ingo Molnar94142322006-12-29 16:48:13 -08005555 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5556 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 __cond_resched();
5558 return 1;
5559 }
5560 return 0;
5561}
Herbert Xu02b67cc2008-01-25 21:08:28 +01005562EXPORT_SYMBOL(_cond_resched);
5563#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564
5565/*
5566 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5567 * call schedule, and on return reacquire the lock.
5568 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005569 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 * operations here to prevent schedule() from being called twice (once via
5571 * spin_unlock(), once by hand).
5572 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005573int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574{
Nick Piggin95c354f2008-01-30 13:31:20 +01005575 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005576 int ret = 0;
5577
Nick Piggin95c354f2008-01-30 13:31:20 +01005578 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005580 if (resched && need_resched())
5581 __cond_resched();
5582 else
5583 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005584 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005587 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589EXPORT_SYMBOL(cond_resched_lock);
5590
5591int __sched cond_resched_softirq(void)
5592{
5593 BUG_ON(!in_softirq());
5594
Ingo Molnar94142322006-12-29 16:48:13 -08005595 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d82562007-05-23 13:58:18 -07005596 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 __cond_resched();
5598 local_bh_disable();
5599 return 1;
5600 }
5601 return 0;
5602}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603EXPORT_SYMBOL(cond_resched_softirq);
5604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605/**
5606 * yield - yield the current processor to other threads.
5607 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005608 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 * thread runnable and calls sys_sched_yield().
5610 */
5611void __sched yield(void)
5612{
5613 set_current_state(TASK_RUNNING);
5614 sys_sched_yield();
5615}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616EXPORT_SYMBOL(yield);
5617
5618/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005619 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 * that process accounting knows that this is a task in IO wait state.
5621 *
5622 * But don't do that if it is a deliberate, throttling IO wait (this task
5623 * has set its backing_dev_info: the queue against which it should throttle)
5624 */
5625void __sched io_schedule(void)
5626{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005627 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005629 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 atomic_inc(&rq->nr_iowait);
5631 schedule();
5632 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005633 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635EXPORT_SYMBOL(io_schedule);
5636
5637long __sched io_schedule_timeout(long timeout)
5638{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005639 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 long ret;
5641
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005642 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 atomic_inc(&rq->nr_iowait);
5644 ret = schedule_timeout(timeout);
5645 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005646 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 return ret;
5648}
5649
5650/**
5651 * sys_sched_get_priority_max - return maximum RT priority.
5652 * @policy: scheduling class.
5653 *
5654 * this syscall returns the maximum rt_priority that can be used
5655 * by a given scheduling class.
5656 */
5657asmlinkage long sys_sched_get_priority_max(int policy)
5658{
5659 int ret = -EINVAL;
5660
5661 switch (policy) {
5662 case SCHED_FIFO:
5663 case SCHED_RR:
5664 ret = MAX_USER_RT_PRIO-1;
5665 break;
5666 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005667 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005668 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 ret = 0;
5670 break;
5671 }
5672 return ret;
5673}
5674
5675/**
5676 * sys_sched_get_priority_min - return minimum RT priority.
5677 * @policy: scheduling class.
5678 *
5679 * this syscall returns the minimum rt_priority that can be used
5680 * by a given scheduling class.
5681 */
5682asmlinkage long sys_sched_get_priority_min(int policy)
5683{
5684 int ret = -EINVAL;
5685
5686 switch (policy) {
5687 case SCHED_FIFO:
5688 case SCHED_RR:
5689 ret = 1;
5690 break;
5691 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005692 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005693 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 ret = 0;
5695 }
5696 return ret;
5697}
5698
5699/**
5700 * sys_sched_rr_get_interval - return the default timeslice of a process.
5701 * @pid: pid of the process.
5702 * @interval: userspace pointer to the timeslice value.
5703 *
5704 * this syscall writes the default timeslice value of a given process
5705 * into the user-space timespec buffer. A value of '0' means infinity.
5706 */
5707asmlinkage
5708long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5709{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005710 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005711 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005712 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
5715 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005716 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
5718 retval = -ESRCH;
5719 read_lock(&tasklist_lock);
5720 p = find_process_by_pid(pid);
5721 if (!p)
5722 goto out_unlock;
5723
5724 retval = security_task_getscheduler(p);
5725 if (retval)
5726 goto out_unlock;
5727
Ingo Molnar77034932007-12-04 17:04:39 +01005728 /*
5729 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5730 * tasks that are on an otherwise idle runqueue:
5731 */
5732 time_slice = 0;
5733 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005734 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005735 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005736 struct sched_entity *se = &p->se;
5737 unsigned long flags;
5738 struct rq *rq;
5739
5740 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005741 if (rq->cfs.load.weight)
5742 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005743 task_rq_unlock(rq, &flags);
5744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005746 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005749
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750out_unlock:
5751 read_unlock(&tasklist_lock);
5752 return retval;
5753}
5754
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005755static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005756
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005757void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005760 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005763 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005764 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005765#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005767 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005769 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770#else
5771 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005772 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005774 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775#endif
5776#ifdef CONFIG_DEBUG_STACK_USAGE
5777 {
Al Viro10ebffd2005-11-13 16:06:56 -08005778 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 while (!*n)
5780 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005781 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 }
5783#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005784 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005785 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005787 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788}
5789
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005790void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005792 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793
Ingo Molnar4bd77322007-07-11 21:21:47 +02005794#if BITS_PER_LONG == 32
5795 printk(KERN_INFO
5796 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005798 printk(KERN_INFO
5799 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800#endif
5801 read_lock(&tasklist_lock);
5802 do_each_thread(g, p) {
5803 /*
5804 * reset the NMI-timeout, listing all files on a slow
5805 * console might take alot of time:
5806 */
5807 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005808 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005809 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 } while_each_thread(g, p);
5811
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005812 touch_all_softlockup_watchdogs();
5813
Ingo Molnardd41f592007-07-09 18:51:59 +02005814#ifdef CONFIG_SCHED_DEBUG
5815 sysrq_sched_debug_show();
5816#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005818 /*
5819 * Only show locks if all tasks are dumped:
5820 */
5821 if (state_filter == -1)
5822 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823}
5824
Ingo Molnar1df21052007-07-09 18:51:58 +02005825void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5826{
Ingo Molnardd41f592007-07-09 18:51:59 +02005827 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005828}
5829
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005830/**
5831 * init_idle - set up an idle thread for a given CPU
5832 * @idle: task in question
5833 * @cpu: cpu the idle task belongs to
5834 *
5835 * NOTE: this function does not set the idle thread's NEED_RESCHED
5836 * flag, to make booting more robust.
5837 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005838void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005840 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 unsigned long flags;
5842
Ingo Molnardd41f592007-07-09 18:51:59 +02005843 __sched_fork(idle);
5844 idle->se.exec_start = sched_clock();
5845
Ingo Molnarb29739f2006-06-27 02:54:51 -07005846 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005848 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
5850 spin_lock_irqsave(&rq->lock, flags);
5851 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005852#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5853 idle->oncpu = 1;
5854#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 spin_unlock_irqrestore(&rq->lock, flags);
5856
5857 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08005858 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005859
Ingo Molnardd41f592007-07-09 18:51:59 +02005860 /*
5861 * The idle tasks have their own, simple scheduling class:
5862 */
5863 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864}
5865
5866/*
5867 * In a system that switches off the HZ timer nohz_cpu_mask
5868 * indicates which cpus entered this state. This is used
5869 * in the rcu update to wait only for active cpus. For system
5870 * which do not switch off the HZ timer nohz_cpu_mask should
5871 * always be CPU_MASK_NONE.
5872 */
5873cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5874
Ingo Molnar19978ca2007-11-09 22:39:38 +01005875/*
5876 * Increase the granularity value when there are more CPUs,
5877 * because with more CPUs the 'effective latency' as visible
5878 * to users decreases. But the relationship is not linear,
5879 * so pick a second-best guess by going with the log2 of the
5880 * number of CPUs.
5881 *
5882 * This idea comes from the SD scheduler of Con Kolivas:
5883 */
5884static inline void sched_init_granularity(void)
5885{
5886 unsigned int factor = 1 + ilog2(num_online_cpus());
5887 const unsigned long limit = 200000000;
5888
5889 sysctl_sched_min_granularity *= factor;
5890 if (sysctl_sched_min_granularity > limit)
5891 sysctl_sched_min_granularity = limit;
5892
5893 sysctl_sched_latency *= factor;
5894 if (sysctl_sched_latency > limit)
5895 sysctl_sched_latency = limit;
5896
5897 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005898}
5899
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900#ifdef CONFIG_SMP
5901/*
5902 * This is how migration works:
5903 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005904 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 * runqueue and wake up that CPU's migration thread.
5906 * 2) we down() the locked semaphore => thread blocks.
5907 * 3) migration thread wakes up (implicitly it forces the migrated
5908 * thread off the CPU)
5909 * 4) it gets the migration request and checks whether the migrated
5910 * task is still in the wrong runqueue.
5911 * 5) if it's in the wrong runqueue then the migration thread removes
5912 * it and puts it into the right queue.
5913 * 6) migration thread up()s the semaphore.
5914 * 7) we wake up and the migration is done.
5915 */
5916
5917/*
5918 * Change a given task's CPU affinity. Migrate the thread to a
5919 * proper CPU and schedule it away if the CPU it's executing on
5920 * is removed from the allowed bitmask.
5921 *
5922 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005923 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 * call is not atomic; no spinlocks may be held.
5925 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005926int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005928 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005930 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005931 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932
5933 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005934 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 ret = -EINVAL;
5936 goto out;
5937 }
5938
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005939 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005940 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005941 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005942 p->cpus_allowed = *new_mask;
5943 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005944 }
5945
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005947 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 goto out;
5949
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005950 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 /* Need help from migration thread: drop lock and wait. */
5952 task_rq_unlock(rq, &flags);
5953 wake_up_process(rq->migration_thread);
5954 wait_for_completion(&req.done);
5955 tlb_migrate_finish(p->mm);
5956 return 0;
5957 }
5958out:
5959 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005960
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 return ret;
5962}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005963EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964
5965/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005966 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 * this because either it can't run here any more (set_cpus_allowed()
5968 * away from this CPU, or CPU going down), or because we're
5969 * attempting to rebalance this task on exec (sched_exec).
5970 *
5971 * So we race with normal scheduler movements, but that's OK, as long
5972 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005973 *
5974 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005976static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005978 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005979 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
5981 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005982 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983
5984 rq_src = cpu_rq(src_cpu);
5985 rq_dest = cpu_rq(dest_cpu);
5986
5987 double_rq_lock(rq_src, rq_dest);
5988 /* Already moved. */
5989 if (task_cpu(p) != src_cpu)
5990 goto out;
5991 /* Affinity changed (again). */
5992 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5993 goto out;
5994
Ingo Molnardd41f592007-07-09 18:51:59 +02005995 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005996 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005997 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005998
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006000 if (on_rq) {
6001 activate_task(rq_dest, p, 0);
6002 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07006004 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005out:
6006 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006007 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008}
6009
6010/*
6011 * migration_thread - this is a highprio system thread that performs
6012 * thread migration by bumping thread off CPU then 'pushing' onto
6013 * another runqueue.
6014 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006015static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006018 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
6020 rq = cpu_rq(cpu);
6021 BUG_ON(rq->migration_thread != current);
6022
6023 set_current_state(TASK_INTERRUPTIBLE);
6024 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006025 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 spin_lock_irq(&rq->lock);
6029
6030 if (cpu_is_offline(cpu)) {
6031 spin_unlock_irq(&rq->lock);
6032 goto wait_to_die;
6033 }
6034
6035 if (rq->active_balance) {
6036 active_load_balance(rq, cpu);
6037 rq->active_balance = 0;
6038 }
6039
6040 head = &rq->migration_queue;
6041
6042 if (list_empty(head)) {
6043 spin_unlock_irq(&rq->lock);
6044 schedule();
6045 set_current_state(TASK_INTERRUPTIBLE);
6046 continue;
6047 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006048 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 list_del_init(head->next);
6050
Nick Piggin674311d2005-06-25 14:57:27 -07006051 spin_unlock(&rq->lock);
6052 __migrate_task(req->task, cpu, req->dest_cpu);
6053 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054
6055 complete(&req->done);
6056 }
6057 __set_current_state(TASK_RUNNING);
6058 return 0;
6059
6060wait_to_die:
6061 /* Wait for kthread_stop */
6062 set_current_state(TASK_INTERRUPTIBLE);
6063 while (!kthread_should_stop()) {
6064 schedule();
6065 set_current_state(TASK_INTERRUPTIBLE);
6066 }
6067 __set_current_state(TASK_RUNNING);
6068 return 0;
6069}
6070
6071#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006072
6073static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6074{
6075 int ret;
6076
6077 local_irq_disable();
6078 ret = __migrate_task(p, src_cpu, dest_cpu);
6079 local_irq_enable();
6080 return ret;
6081}
6082
Kirill Korotaev054b9102006-12-10 02:20:11 -08006083/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006084 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006085 * NOTE: interrupts should be disabled by the caller
6086 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006087static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006089 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006091 struct rq *rq;
6092 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
Andi Kleen3a5c3592007-10-15 17:00:14 +02006094 do {
6095 /* On same node? */
6096 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6097 cpus_and(mask, mask, p->cpus_allowed);
6098 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
Andi Kleen3a5c3592007-10-15 17:00:14 +02006100 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006101 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006102 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103
Andi Kleen3a5c3592007-10-15 17:00:14 +02006104 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006105 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006106 cpumask_t cpus_allowed;
6107
6108 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006109 /*
6110 * Try to stay on the same cpuset, where the
6111 * current cpuset may be a subset of all cpus.
6112 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006113 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006114 * called within calls to cpuset_lock/cpuset_unlock.
6115 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006116 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006117 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006118 dest_cpu = any_online_cpu(p->cpus_allowed);
6119 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
Andi Kleen3a5c3592007-10-15 17:00:14 +02006121 /*
6122 * Don't tell them about moving exiting tasks or
6123 * kernel threads (both mm NULL), since they never
6124 * leave kernel.
6125 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006126 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006127 printk(KERN_INFO "process %d (%s) no "
6128 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006129 task_pid_nr(p), p->comm, dead_cpu);
6130 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006131 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006132 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133}
6134
6135/*
6136 * While a dead CPU has no uninterruptible tasks queued at this point,
6137 * it might still have a nonzero ->nr_uninterruptible counter, because
6138 * for performance reasons the counter is not stricly tracking tasks to
6139 * their home CPUs. So we just add the counter to another CPU's counter,
6140 * to keep the global sum constant after CPU-down:
6141 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006142static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143{
Mike Travis7c16ec52008-04-04 18:11:11 -07006144 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 unsigned long flags;
6146
6147 local_irq_save(flags);
6148 double_rq_lock(rq_src, rq_dest);
6149 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6150 rq_src->nr_uninterruptible = 0;
6151 double_rq_unlock(rq_src, rq_dest);
6152 local_irq_restore(flags);
6153}
6154
6155/* Run through task list and migrate tasks from the dead cpu. */
6156static void migrate_live_tasks(int src_cpu)
6157{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006158 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006160 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161
Ingo Molnar48f24c42006-07-03 00:25:40 -07006162 do_each_thread(t, p) {
6163 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 continue;
6165
Ingo Molnar48f24c42006-07-03 00:25:40 -07006166 if (task_cpu(p) == src_cpu)
6167 move_task_off_dead_cpu(src_cpu, p);
6168 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006170 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171}
6172
Ingo Molnardd41f592007-07-09 18:51:59 +02006173/*
6174 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006175 * It does so by boosting its priority to highest possible.
6176 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 */
6178void sched_idle_next(void)
6179{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006180 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006181 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 struct task_struct *p = rq->idle;
6183 unsigned long flags;
6184
6185 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006186 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187
Ingo Molnar48f24c42006-07-03 00:25:40 -07006188 /*
6189 * Strictly not necessary since rest of the CPUs are stopped by now
6190 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 */
6192 spin_lock_irqsave(&rq->lock, flags);
6193
Ingo Molnardd41f592007-07-09 18:51:59 +02006194 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006195
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006196 update_rq_clock(rq);
6197 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198
6199 spin_unlock_irqrestore(&rq->lock, flags);
6200}
6201
Ingo Molnar48f24c42006-07-03 00:25:40 -07006202/*
6203 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 * offline.
6205 */
6206void idle_task_exit(void)
6207{
6208 struct mm_struct *mm = current->active_mm;
6209
6210 BUG_ON(cpu_online(smp_processor_id()));
6211
6212 if (mm != &init_mm)
6213 switch_mm(mm, &init_mm, current);
6214 mmdrop(mm);
6215}
6216
Kirill Korotaev054b9102006-12-10 02:20:11 -08006217/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006218static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006220 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
6222 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006223 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224
6225 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006226 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
Ingo Molnar48f24c42006-07-03 00:25:40 -07006228 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
6230 /*
6231 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006232 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 * fine.
6234 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006235 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006236 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006237 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238
Ingo Molnar48f24c42006-07-03 00:25:40 -07006239 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240}
6241
6242/* release_task() removes task from tasklist, so we won't find dead tasks. */
6243static void migrate_dead_tasks(unsigned int dead_cpu)
6244{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006245 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006246 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247
Ingo Molnardd41f592007-07-09 18:51:59 +02006248 for ( ; ; ) {
6249 if (!rq->nr_running)
6250 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006251 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006252 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006253 if (!next)
6254 break;
6255 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006256
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 }
6258}
6259#endif /* CONFIG_HOTPLUG_CPU */
6260
Nick Piggine692ab52007-07-26 13:40:43 +02006261#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6262
6263static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006264 {
6265 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006266 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006267 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006268 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006269};
6270
6271static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006272 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006273 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006274 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006275 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006276 .child = sd_ctl_dir,
6277 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006278 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006279};
6280
6281static struct ctl_table *sd_alloc_ctl_entry(int n)
6282{
6283 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006284 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006285
Nick Piggine692ab52007-07-26 13:40:43 +02006286 return entry;
6287}
6288
Milton Miller6382bc92007-10-15 17:00:19 +02006289static void sd_free_ctl_entry(struct ctl_table **tablep)
6290{
Milton Millercd790072007-10-17 16:55:11 +02006291 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006292
Milton Millercd790072007-10-17 16:55:11 +02006293 /*
6294 * In the intermediate directories, both the child directory and
6295 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006296 * will always be set. In the lowest directory the names are
Milton Millercd790072007-10-17 16:55:11 +02006297 * static strings and all have proc handlers.
6298 */
6299 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006300 if (entry->child)
6301 sd_free_ctl_entry(&entry->child);
Milton Millercd790072007-10-17 16:55:11 +02006302 if (entry->proc_handler == NULL)
6303 kfree(entry->procname);
6304 }
Milton Miller6382bc92007-10-15 17:00:19 +02006305
6306 kfree(*tablep);
6307 *tablep = NULL;
6308}
6309
Nick Piggine692ab52007-07-26 13:40:43 +02006310static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006311set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006312 const char *procname, void *data, int maxlen,
6313 mode_t mode, proc_handler *proc_handler)
6314{
Nick Piggine692ab52007-07-26 13:40:43 +02006315 entry->procname = procname;
6316 entry->data = data;
6317 entry->maxlen = maxlen;
6318 entry->mode = mode;
6319 entry->proc_handler = proc_handler;
6320}
6321
6322static struct ctl_table *
6323sd_alloc_ctl_domain_table(struct sched_domain *sd)
6324{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006325 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006326
Milton Millerad1cdc12007-10-15 17:00:19 +02006327 if (table == NULL)
6328 return NULL;
6329
Alexey Dobriyane0361852007-08-09 11:16:46 +02006330 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006331 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006332 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006333 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006334 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006335 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006336 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006337 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006338 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006339 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006340 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006341 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006342 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006343 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006344 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006345 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006346 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006347 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006348 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006349 &sd->cache_nice_tries,
6350 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006351 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006352 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006353 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006354
6355 return table;
6356}
6357
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006358static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006359{
6360 struct ctl_table *entry, *table;
6361 struct sched_domain *sd;
6362 int domain_num = 0, i;
6363 char buf[32];
6364
6365 for_each_domain(cpu, sd)
6366 domain_num++;
6367 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006368 if (table == NULL)
6369 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006370
6371 i = 0;
6372 for_each_domain(cpu, sd) {
6373 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006374 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006375 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006376 entry->child = sd_alloc_ctl_domain_table(sd);
6377 entry++;
6378 i++;
6379 }
6380 return table;
6381}
6382
6383static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006384static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006385{
6386 int i, cpu_num = num_online_cpus();
6387 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6388 char buf[32];
6389
Milton Miller73785472007-10-24 18:23:48 +02006390 WARN_ON(sd_ctl_dir[0].child);
6391 sd_ctl_dir[0].child = entry;
6392
Milton Millerad1cdc12007-10-15 17:00:19 +02006393 if (entry == NULL)
6394 return;
6395
Milton Miller97b6ea72007-10-15 17:00:19 +02006396 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006397 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006398 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006399 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006400 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006401 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006402 }
Milton Miller73785472007-10-24 18:23:48 +02006403
6404 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006405 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6406}
Milton Miller6382bc92007-10-15 17:00:19 +02006407
Milton Miller73785472007-10-24 18:23:48 +02006408/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006409static void unregister_sched_domain_sysctl(void)
6410{
Milton Miller73785472007-10-24 18:23:48 +02006411 if (sd_sysctl_header)
6412 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006413 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006414 if (sd_ctl_dir[0].child)
6415 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006416}
Nick Piggine692ab52007-07-26 13:40:43 +02006417#else
Milton Miller6382bc92007-10-15 17:00:19 +02006418static void register_sched_domain_sysctl(void)
6419{
6420}
6421static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006422{
6423}
6424#endif
6425
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426/*
6427 * migration_call - callback that gets triggered when a CPU is added.
6428 * Here we can start up the necessary migration thread for the new CPU.
6429 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006430static int __cpuinit
6431migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006434 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006436 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437
6438 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006439
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006441 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006442 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 if (IS_ERR(p))
6444 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 kthread_bind(p, cpu);
6446 /* Must be high prio: stop_machine expects to yield to it. */
6447 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006448 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 task_rq_unlock(rq, &flags);
6450 cpu_rq(cpu)->migration_thread = p;
6451 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006452
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006454 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006455 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006457
6458 /* Update our root-domain */
6459 rq = cpu_rq(cpu);
6460 spin_lock_irqsave(&rq->lock, flags);
6461 if (rq->rd) {
6462 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6463 cpu_set(cpu, rq->rd->online);
6464 }
6465 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006467
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468#ifdef CONFIG_HOTPLUG_CPU
6469 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006470 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006471 if (!cpu_rq(cpu)->migration_thread)
6472 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006473 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006474 kthread_bind(cpu_rq(cpu)->migration_thread,
6475 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 kthread_stop(cpu_rq(cpu)->migration_thread);
6477 cpu_rq(cpu)->migration_thread = NULL;
6478 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006479
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006481 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006482 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 migrate_live_tasks(cpu);
6484 rq = cpu_rq(cpu);
6485 kthread_stop(rq->migration_thread);
6486 rq->migration_thread = NULL;
6487 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006488 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006489 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006490 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006492 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6493 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006495 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006496 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 migrate_nr_uninterruptible(rq);
6498 BUG_ON(rq->nr_running != 0);
6499
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006500 /*
6501 * No need to migrate the tasks: it was best-effort if
6502 * they didn't take sched_hotcpu_mutex. Just wake up
6503 * the requestors.
6504 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 spin_lock_irq(&rq->lock);
6506 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006507 struct migration_req *req;
6508
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006510 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 list_del_init(&req->list);
6512 complete(&req->done);
6513 }
6514 spin_unlock_irq(&rq->lock);
6515 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006516
Gregory Haskins08f503b2008-03-10 17:59:11 -04006517 case CPU_DYING:
6518 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006519 /* Update our root-domain */
6520 rq = cpu_rq(cpu);
6521 spin_lock_irqsave(&rq->lock, flags);
6522 if (rq->rd) {
6523 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6524 cpu_clear(cpu, rq->rd->online);
6525 }
6526 spin_unlock_irqrestore(&rq->lock, flags);
6527 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528#endif
6529 }
6530 return NOTIFY_OK;
6531}
6532
6533/* Register at highest priority so that task migration (migrate_all_tasks)
6534 * happens before everything else.
6535 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006536static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 .notifier_call = migration_call,
6538 .priority = 10
6539};
6540
Adrian Bunke6fe6642007-11-09 22:39:39 +01006541void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542{
6543 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006544 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006545
6546 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006547 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6548 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6550 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551}
6552#endif
6553
6554#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006555
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006556#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006557
Mike Travis7c16ec52008-04-04 18:11:11 -07006558static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6559 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006560{
6561 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006562 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006563
Mike Travis434d53b2008-04-04 18:11:04 -07006564 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006565 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006566
6567 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6568
6569 if (!(sd->flags & SD_LOAD_BALANCE)) {
6570 printk("does not load-balance\n");
6571 if (sd->parent)
6572 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6573 " has parent");
6574 return -1;
6575 }
6576
6577 printk(KERN_CONT "span %s\n", str);
6578
6579 if (!cpu_isset(cpu, sd->span)) {
6580 printk(KERN_ERR "ERROR: domain->span does not contain "
6581 "CPU%d\n", cpu);
6582 }
6583 if (!cpu_isset(cpu, group->cpumask)) {
6584 printk(KERN_ERR "ERROR: domain->groups does not contain"
6585 " CPU%d\n", cpu);
6586 }
6587
6588 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6589 do {
6590 if (!group) {
6591 printk("\n");
6592 printk(KERN_ERR "ERROR: group is NULL\n");
6593 break;
6594 }
6595
6596 if (!group->__cpu_power) {
6597 printk(KERN_CONT "\n");
6598 printk(KERN_ERR "ERROR: domain->cpu_power not "
6599 "set\n");
6600 break;
6601 }
6602
6603 if (!cpus_weight(group->cpumask)) {
6604 printk(KERN_CONT "\n");
6605 printk(KERN_ERR "ERROR: empty group\n");
6606 break;
6607 }
6608
Mike Travis7c16ec52008-04-04 18:11:11 -07006609 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006610 printk(KERN_CONT "\n");
6611 printk(KERN_ERR "ERROR: repeated CPUs\n");
6612 break;
6613 }
6614
Mike Travis7c16ec52008-04-04 18:11:11 -07006615 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006616
Mike Travis434d53b2008-04-04 18:11:04 -07006617 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006618 printk(KERN_CONT " %s", str);
6619
6620 group = group->next;
6621 } while (group != sd->groups);
6622 printk(KERN_CONT "\n");
6623
Mike Travis7c16ec52008-04-04 18:11:11 -07006624 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006625 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6626
Mike Travis7c16ec52008-04-04 18:11:11 -07006627 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006628 printk(KERN_ERR "ERROR: parent span is not a superset "
6629 "of domain->span\n");
6630 return 0;
6631}
6632
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633static void sched_domain_debug(struct sched_domain *sd, int cpu)
6634{
Mike Travis7c16ec52008-04-04 18:11:11 -07006635 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 int level = 0;
6637
Nick Piggin41c7ce92005-06-25 14:57:24 -07006638 if (!sd) {
6639 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6640 return;
6641 }
6642
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6644
Mike Travis7c16ec52008-04-04 18:11:11 -07006645 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6646 if (!groupmask) {
6647 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6648 return;
6649 }
6650
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006651 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006652 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 level++;
6655 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006656 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006657 break;
6658 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006659 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660}
6661#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006662# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663#endif
6664
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006665static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006666{
6667 if (cpus_weight(sd->span) == 1)
6668 return 1;
6669
6670 /* Following flags need at least 2 groups */
6671 if (sd->flags & (SD_LOAD_BALANCE |
6672 SD_BALANCE_NEWIDLE |
6673 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006674 SD_BALANCE_EXEC |
6675 SD_SHARE_CPUPOWER |
6676 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006677 if (sd->groups != sd->groups->next)
6678 return 0;
6679 }
6680
6681 /* Following flags don't use groups */
6682 if (sd->flags & (SD_WAKE_IDLE |
6683 SD_WAKE_AFFINE |
6684 SD_WAKE_BALANCE))
6685 return 0;
6686
6687 return 1;
6688}
6689
Ingo Molnar48f24c42006-07-03 00:25:40 -07006690static int
6691sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006692{
6693 unsigned long cflags = sd->flags, pflags = parent->flags;
6694
6695 if (sd_degenerate(parent))
6696 return 1;
6697
6698 if (!cpus_equal(sd->span, parent->span))
6699 return 0;
6700
6701 /* Does parent contain flags not in child? */
6702 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6703 if (cflags & SD_WAKE_AFFINE)
6704 pflags &= ~SD_WAKE_BALANCE;
6705 /* Flags needing groups don't count if only 1 group in parent */
6706 if (parent->groups == parent->groups->next) {
6707 pflags &= ~(SD_LOAD_BALANCE |
6708 SD_BALANCE_NEWIDLE |
6709 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006710 SD_BALANCE_EXEC |
6711 SD_SHARE_CPUPOWER |
6712 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006713 }
6714 if (~cflags & pflags)
6715 return 0;
6716
6717 return 1;
6718}
6719
Gregory Haskins57d885f2008-01-25 21:08:18 +01006720static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6721{
6722 unsigned long flags;
6723 const struct sched_class *class;
6724
6725 spin_lock_irqsave(&rq->lock, flags);
6726
6727 if (rq->rd) {
6728 struct root_domain *old_rd = rq->rd;
6729
Ingo Molnar0eab9142008-01-25 21:08:19 +01006730 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006731 if (class->leave_domain)
6732 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006733 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006734
Gregory Haskinsdc938522008-01-25 21:08:26 +01006735 cpu_clear(rq->cpu, old_rd->span);
6736 cpu_clear(rq->cpu, old_rd->online);
6737
Gregory Haskins57d885f2008-01-25 21:08:18 +01006738 if (atomic_dec_and_test(&old_rd->refcount))
6739 kfree(old_rd);
6740 }
6741
6742 atomic_inc(&rd->refcount);
6743 rq->rd = rd;
6744
Gregory Haskinsdc938522008-01-25 21:08:26 +01006745 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006746 if (cpu_isset(rq->cpu, cpu_online_map))
6747 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006748
Ingo Molnar0eab9142008-01-25 21:08:19 +01006749 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006750 if (class->join_domain)
6751 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006752 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006753
6754 spin_unlock_irqrestore(&rq->lock, flags);
6755}
6756
Gregory Haskinsdc938522008-01-25 21:08:26 +01006757static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006758{
6759 memset(rd, 0, sizeof(*rd));
6760
Gregory Haskinsdc938522008-01-25 21:08:26 +01006761 cpus_clear(rd->span);
6762 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006763}
6764
6765static void init_defrootdomain(void)
6766{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006767 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006768 atomic_set(&def_root_domain.refcount, 1);
6769}
6770
Gregory Haskinsdc938522008-01-25 21:08:26 +01006771static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006772{
6773 struct root_domain *rd;
6774
6775 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6776 if (!rd)
6777 return NULL;
6778
Gregory Haskinsdc938522008-01-25 21:08:26 +01006779 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006780
6781 return rd;
6782}
6783
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006785 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 * hold the hotplug lock.
6787 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006788static void
6789cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006791 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006792 struct sched_domain *tmp;
6793
6794 /* Remove the sched domains which do not contribute to scheduling. */
6795 for (tmp = sd; tmp; tmp = tmp->parent) {
6796 struct sched_domain *parent = tmp->parent;
6797 if (!parent)
6798 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006799 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006800 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006801 if (parent->parent)
6802 parent->parent->child = tmp;
6803 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006804 }
6805
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006806 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006807 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006808 if (sd)
6809 sd->child = NULL;
6810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811
6812 sched_domain_debug(sd, cpu);
6813
Gregory Haskins57d885f2008-01-25 21:08:18 +01006814 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006815 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816}
6817
6818/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006819static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820
6821/* Setup the mask of cpus configured for isolated domains */
6822static int __init isolated_cpu_setup(char *str)
6823{
6824 int ints[NR_CPUS], i;
6825
6826 str = get_options(str, ARRAY_SIZE(ints), ints);
6827 cpus_clear(cpu_isolated_map);
6828 for (i = 1; i <= ints[0]; i++)
6829 if (ints[i] < NR_CPUS)
6830 cpu_set(ints[i], cpu_isolated_map);
6831 return 1;
6832}
6833
Ingo Molnar8927f492007-10-15 17:00:13 +02006834__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835
6836/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006837 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6838 * to a function which identifies what group(along with sched group) a CPU
6839 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6840 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 *
6842 * init_sched_build_groups will build a circular linked list of the groups
6843 * covered by the given span, and will set each group's ->cpumask correctly,
6844 * and ->cpu_power to 0.
6845 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006846static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006847init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006848 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006849 struct sched_group **sg,
6850 cpumask_t *tmpmask),
6851 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852{
6853 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 int i;
6855
Mike Travis7c16ec52008-04-04 18:11:11 -07006856 cpus_clear(*covered);
6857
6858 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006859 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006860 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 int j;
6862
Mike Travis7c16ec52008-04-04 18:11:11 -07006863 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 continue;
6865
Mike Travis7c16ec52008-04-04 18:11:11 -07006866 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006867 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868
Mike Travis7c16ec52008-04-04 18:11:11 -07006869 for_each_cpu_mask(j, *span) {
6870 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871 continue;
6872
Mike Travis7c16ec52008-04-04 18:11:11 -07006873 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 cpu_set(j, sg->cpumask);
6875 }
6876 if (!first)
6877 first = sg;
6878 if (last)
6879 last->next = sg;
6880 last = sg;
6881 }
6882 last->next = first;
6883}
6884
John Hawkes9c1cfda2005-09-06 15:18:14 -07006885#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
John Hawkes9c1cfda2005-09-06 15:18:14 -07006887#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006888
John Hawkes9c1cfda2005-09-06 15:18:14 -07006889/**
6890 * find_next_best_node - find the next node to include in a sched_domain
6891 * @node: node whose sched_domain we're building
6892 * @used_nodes: nodes already in the sched_domain
6893 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006894 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006895 * finds the closest node not already in the @used_nodes map.
6896 *
6897 * Should use nodemask_t.
6898 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006899static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006900{
6901 int i, n, val, min_val, best_node = 0;
6902
6903 min_val = INT_MAX;
6904
6905 for (i = 0; i < MAX_NUMNODES; i++) {
6906 /* Start at @node */
6907 n = (node + i) % MAX_NUMNODES;
6908
6909 if (!nr_cpus_node(n))
6910 continue;
6911
6912 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006913 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006914 continue;
6915
6916 /* Simple min distance search */
6917 val = node_distance(node, n);
6918
6919 if (val < min_val) {
6920 min_val = val;
6921 best_node = n;
6922 }
6923 }
6924
Mike Travisc5f59f02008-04-04 18:11:10 -07006925 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006926 return best_node;
6927}
6928
6929/**
6930 * sched_domain_node_span - get a cpumask for a node's sched_domain
6931 * @node: node whose cpumask we're constructing
John Hawkes9c1cfda2005-09-06 15:18:14 -07006932 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006933 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006934 * should be one that prevents unnecessary balancing, but also spreads tasks
6935 * out optimally.
6936 */
Mike Travis4bdbaad2008-04-15 16:35:52 -07006937static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006938{
Mike Travisc5f59f02008-04-04 18:11:10 -07006939 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07006940 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006941 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006942
Mike Travis4bdbaad2008-04-15 16:35:52 -07006943 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07006944 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006945
Mike Travis4bdbaad2008-04-15 16:35:52 -07006946 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07006947 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006948
6949 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07006950 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006951
Mike Travisc5f59f02008-04-04 18:11:10 -07006952 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad2008-04-15 16:35:52 -07006953 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006954 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006955}
6956#endif
6957
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006958int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006959
John Hawkes9c1cfda2005-09-06 15:18:14 -07006960/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006961 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006962 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963#ifdef CONFIG_SCHED_SMT
6964static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006965static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006966
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006967static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006968cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6969 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006971 if (sg)
6972 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 return cpu;
6974}
6975#endif
6976
Ingo Molnar48f24c42006-07-03 00:25:40 -07006977/*
6978 * multi-core sched-domains:
6979 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006980#ifdef CONFIG_SCHED_MC
6981static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006982static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006983#endif
6984
6985#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006986static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006987cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6988 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006989{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006990 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07006991
6992 *mask = per_cpu(cpu_sibling_map, cpu);
6993 cpus_and(*mask, *mask, *cpu_map);
6994 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006995 if (sg)
6996 *sg = &per_cpu(sched_group_core, group);
6997 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006998}
6999#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007000static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007001cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7002 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007003{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007004 if (sg)
7005 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007006 return cpu;
7007}
7008#endif
7009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007011static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007012
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007013static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007014cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7015 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007017 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007018#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007019 *mask = cpu_coregroup_map(cpu);
7020 cpus_and(*mask, *mask, *cpu_map);
7021 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007022#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007023 *mask = per_cpu(cpu_sibling_map, cpu);
7024 cpus_and(*mask, *mask, *cpu_map);
7025 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007027 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007029 if (sg)
7030 *sg = &per_cpu(sched_group_phys, group);
7031 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032}
7033
7034#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007035/*
7036 * The init_sched_build_groups can't handle what we want to do with node
7037 * groups, so roll our own. Now each node has its own list of groups which
7038 * gets dynamically allocated.
7039 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007041static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007042
7043static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007044static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007045
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007046static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007047 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007049 int group;
7050
Mike Travis7c16ec52008-04-04 18:11:11 -07007051 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7052 cpus_and(*nodemask, *nodemask, *cpu_map);
7053 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007054
7055 if (sg)
7056 *sg = &per_cpu(sched_group_allnodes, group);
7057 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007059
Siddha, Suresh B08069032006-03-27 01:15:23 -08007060static void init_numa_sched_groups_power(struct sched_group *group_head)
7061{
7062 struct sched_group *sg = group_head;
7063 int j;
7064
7065 if (!sg)
7066 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007067 do {
7068 for_each_cpu_mask(j, sg->cpumask) {
7069 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007070
Andi Kleen3a5c3592007-10-15 17:00:14 +02007071 sd = &per_cpu(phys_domains, j);
7072 if (j != first_cpu(sd->groups->cpumask)) {
7073 /*
7074 * Only add "power" once for each
7075 * physical package.
7076 */
7077 continue;
7078 }
7079
7080 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007081 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007082 sg = sg->next;
7083 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085#endif
7086
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007087#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007088/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007089static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007090{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007091 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007092
7093 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007094 struct sched_group **sched_group_nodes
7095 = sched_group_nodes_bycpu[cpu];
7096
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007097 if (!sched_group_nodes)
7098 continue;
7099
7100 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007101 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7102
Mike Travis7c16ec52008-04-04 18:11:11 -07007103 *nodemask = node_to_cpumask(i);
7104 cpus_and(*nodemask, *nodemask, *cpu_map);
7105 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007106 continue;
7107
7108 if (sg == NULL)
7109 continue;
7110 sg = sg->next;
7111next_sg:
7112 oldsg = sg;
7113 sg = sg->next;
7114 kfree(oldsg);
7115 if (oldsg != sched_group_nodes[i])
7116 goto next_sg;
7117 }
7118 kfree(sched_group_nodes);
7119 sched_group_nodes_bycpu[cpu] = NULL;
7120 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007121}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007122#else
Mike Travis7c16ec52008-04-04 18:11:11 -07007123static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007124{
7125}
7126#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007127
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007129 * Initialize sched groups cpu_power.
7130 *
7131 * cpu_power indicates the capacity of sched group, which is used while
7132 * distributing the load between different sched groups in a sched domain.
7133 * Typically cpu_power for all the groups in a sched domain will be same unless
7134 * there are asymmetries in the topology. If there are asymmetries, group
7135 * having more cpu_power will pickup more load compared to the group having
7136 * less cpu_power.
7137 *
7138 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7139 * the maximum number of tasks a group can handle in the presence of other idle
7140 * or lightly loaded groups in the same sched domain.
7141 */
7142static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7143{
7144 struct sched_domain *child;
7145 struct sched_group *group;
7146
7147 WARN_ON(!sd || !sd->groups);
7148
7149 if (cpu != first_cpu(sd->groups->cpumask))
7150 return;
7151
7152 child = sd->child;
7153
Eric Dumazet5517d862007-05-08 00:32:57 -07007154 sd->groups->__cpu_power = 0;
7155
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007156 /*
7157 * For perf policy, if the groups in child domain share resources
7158 * (for example cores sharing some portions of the cache hierarchy
7159 * or SMT), then set this domain groups cpu_power such that each group
7160 * can handle only one task, when there are other idle groups in the
7161 * same sched domain.
7162 */
7163 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7164 (child->flags &
7165 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007166 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007167 return;
7168 }
7169
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007170 /*
7171 * add cpu_power of each child group to this groups cpu_power
7172 */
7173 group = child->groups;
7174 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007175 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007176 group = group->next;
7177 } while (group != child->groups);
7178}
7179
7180/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007181 * Initializers for schedule domains
7182 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7183 */
7184
7185#define SD_INIT(sd, type) sd_init_##type(sd)
7186#define SD_INIT_FUNC(type) \
7187static noinline void sd_init_##type(struct sched_domain *sd) \
7188{ \
7189 memset(sd, 0, sizeof(*sd)); \
7190 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007191 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007192}
7193
7194SD_INIT_FUNC(CPU)
7195#ifdef CONFIG_NUMA
7196 SD_INIT_FUNC(ALLNODES)
7197 SD_INIT_FUNC(NODE)
7198#endif
7199#ifdef CONFIG_SCHED_SMT
7200 SD_INIT_FUNC(SIBLING)
7201#endif
7202#ifdef CONFIG_SCHED_MC
7203 SD_INIT_FUNC(MC)
7204#endif
7205
7206/*
7207 * To minimize stack usage kmalloc room for cpumasks and share the
7208 * space as the usage in build_sched_domains() dictates. Used only
7209 * if the amount of space is significant.
7210 */
7211struct allmasks {
7212 cpumask_t tmpmask; /* make this one first */
7213 union {
7214 cpumask_t nodemask;
7215 cpumask_t this_sibling_map;
7216 cpumask_t this_core_map;
7217 };
7218 cpumask_t send_covered;
7219
7220#ifdef CONFIG_NUMA
7221 cpumask_t domainspan;
7222 cpumask_t covered;
7223 cpumask_t notcovered;
7224#endif
7225};
7226
7227#if NR_CPUS > 128
7228#define SCHED_CPUMASK_ALLOC 1
7229#define SCHED_CPUMASK_FREE(v) kfree(v)
7230#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7231#else
7232#define SCHED_CPUMASK_ALLOC 0
7233#define SCHED_CPUMASK_FREE(v)
7234#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7235#endif
7236
7237#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7238 ((unsigned long)(a) + offsetof(struct allmasks, v))
7239
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007240static int default_relax_domain_level = -1;
7241
7242static int __init setup_relax_domain_level(char *str)
7243{
7244 default_relax_domain_level = simple_strtoul(str, NULL, 0);
7245 return 1;
7246}
7247__setup("relax_domain_level=", setup_relax_domain_level);
7248
7249static void set_domain_attribute(struct sched_domain *sd,
7250 struct sched_domain_attr *attr)
7251{
7252 int request;
7253
7254 if (!attr || attr->relax_domain_level < 0) {
7255 if (default_relax_domain_level < 0)
7256 return;
7257 else
7258 request = default_relax_domain_level;
7259 } else
7260 request = attr->relax_domain_level;
7261 if (request < sd->level) {
7262 /* turn off idle balance on this domain */
7263 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7264 } else {
7265 /* turn on idle balance on this domain */
7266 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7267 }
7268}
7269
Mike Travis7c16ec52008-04-04 18:11:11 -07007270/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007271 * Build sched domains for a given set of cpus and attach the sched domains
7272 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007274static int __build_sched_domains(const cpumask_t *cpu_map,
7275 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276{
7277 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007278 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007279 SCHED_CPUMASK_DECLARE(allmasks);
7280 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007281#ifdef CONFIG_NUMA
7282 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007283 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007284
7285 /*
7286 * Allocate the per-node list of sched groups
7287 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007288 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007289 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007290 if (!sched_group_nodes) {
7291 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007292 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007293 }
John Hawkesd1b55132005-09-06 15:18:14 -07007294#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295
Gregory Haskinsdc938522008-01-25 21:08:26 +01007296 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007297 if (!rd) {
7298 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007299#ifdef CONFIG_NUMA
7300 kfree(sched_group_nodes);
7301#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007302 return -ENOMEM;
7303 }
7304
Mike Travis7c16ec52008-04-04 18:11:11 -07007305#if SCHED_CPUMASK_ALLOC
7306 /* get space for all scratch cpumask variables */
7307 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7308 if (!allmasks) {
7309 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7310 kfree(rd);
7311#ifdef CONFIG_NUMA
7312 kfree(sched_group_nodes);
7313#endif
7314 return -ENOMEM;
7315 }
7316#endif
7317 tmpmask = (cpumask_t *)allmasks;
7318
7319
7320#ifdef CONFIG_NUMA
7321 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7322#endif
7323
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007325 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007327 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007329 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330
Mike Travis7c16ec52008-04-04 18:11:11 -07007331 *nodemask = node_to_cpumask(cpu_to_node(i));
7332 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333
7334#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007335 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007336 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007337 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007338 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007339 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007340 sd->span = *cpu_map;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007341 sd->first_cpu = first_cpu(sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07007342 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007343 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007344 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007345 } else
7346 p = NULL;
7347
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007349 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007350 set_domain_attribute(sd, attr);
Mike Travis4bdbaad2008-04-15 16:35:52 -07007351 sched_domain_node_span(cpu_to_node(i), &sd->span);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007352 sd->first_cpu = first_cpu(sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007353 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007354 if (p)
7355 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007356 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357#endif
7358
7359 p = sd;
7360 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007361 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007362 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007363 sd->span = *nodemask;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007364 sd->first_cpu = first_cpu(sd->span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007366 if (p)
7367 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007368 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007370#ifdef CONFIG_SCHED_MC
7371 p = sd;
7372 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007373 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007374 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007375 sd->span = cpu_coregroup_map(i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007376 sd->first_cpu = first_cpu(sd->span);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007377 cpus_and(sd->span, sd->span, *cpu_map);
7378 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007379 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007380 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007381#endif
7382
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383#ifdef CONFIG_SCHED_SMT
7384 p = sd;
7385 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007386 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007387 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007388 sd->span = per_cpu(cpu_sibling_map, i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007389 sd->first_cpu = first_cpu(sd->span);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007390 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007392 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007393 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394#endif
7395 }
7396
7397#ifdef CONFIG_SCHED_SMT
7398 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007399 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007400 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7401 SCHED_CPUMASK_VAR(send_covered, allmasks);
7402
7403 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7404 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7405 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 continue;
7407
Ingo Molnardd41f592007-07-09 18:51:59 +02007408 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007409 &cpu_to_cpu_group,
7410 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 }
7412#endif
7413
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007414#ifdef CONFIG_SCHED_MC
7415 /* Set up multi-core groups */
7416 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007417 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7418 SCHED_CPUMASK_VAR(send_covered, allmasks);
7419
7420 *this_core_map = cpu_coregroup_map(i);
7421 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7422 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007423 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007424
Ingo Molnardd41f592007-07-09 18:51:59 +02007425 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007426 &cpu_to_core_group,
7427 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007428 }
7429#endif
7430
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431 /* Set up physical groups */
7432 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007433 SCHED_CPUMASK_VAR(nodemask, allmasks);
7434 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435
Mike Travis7c16ec52008-04-04 18:11:11 -07007436 *nodemask = node_to_cpumask(i);
7437 cpus_and(*nodemask, *nodemask, *cpu_map);
7438 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 continue;
7440
Mike Travis7c16ec52008-04-04 18:11:11 -07007441 init_sched_build_groups(nodemask, cpu_map,
7442 &cpu_to_phys_group,
7443 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 }
7445
7446#ifdef CONFIG_NUMA
7447 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007448 if (sd_allnodes) {
7449 SCHED_CPUMASK_VAR(send_covered, allmasks);
7450
7451 init_sched_build_groups(cpu_map, cpu_map,
7452 &cpu_to_allnodes_group,
7453 send_covered, tmpmask);
7454 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007455
7456 for (i = 0; i < MAX_NUMNODES; i++) {
7457 /* Set up node groups */
7458 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007459 SCHED_CPUMASK_VAR(nodemask, allmasks);
7460 SCHED_CPUMASK_VAR(domainspan, allmasks);
7461 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007462 int j;
7463
Mike Travis7c16ec52008-04-04 18:11:11 -07007464 *nodemask = node_to_cpumask(i);
7465 cpus_clear(*covered);
7466
7467 cpus_and(*nodemask, *nodemask, *cpu_map);
7468 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007469 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007470 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007471 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007472
Mike Travis4bdbaad2008-04-15 16:35:52 -07007473 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007474 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007475
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007476 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007477 if (!sg) {
7478 printk(KERN_WARNING "Can not alloc domain group for "
7479 "node %d\n", i);
7480 goto error;
7481 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007482 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007483 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007484 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007485
John Hawkes9c1cfda2005-09-06 15:18:14 -07007486 sd = &per_cpu(node_domains, j);
7487 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007488 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007489 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007490 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007491 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007492 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007493 prev = sg;
7494
7495 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007496 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007497 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007498 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007499
Mike Travis7c16ec52008-04-04 18:11:11 -07007500 cpus_complement(*notcovered, *covered);
7501 cpus_and(*tmpmask, *notcovered, *cpu_map);
7502 cpus_and(*tmpmask, *tmpmask, *domainspan);
7503 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007504 break;
7505
Mike Travis7c16ec52008-04-04 18:11:11 -07007506 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7507 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007508 continue;
7509
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007510 sg = kmalloc_node(sizeof(struct sched_group),
7511 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007512 if (!sg) {
7513 printk(KERN_WARNING
7514 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007515 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007516 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007517 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007518 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007519 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007520 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007521 prev->next = sg;
7522 prev = sg;
7523 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525#endif
7526
7527 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007528#ifdef CONFIG_SCHED_SMT
7529 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007530 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7531
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007532 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007533 }
7534#endif
7535#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007536 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007537 struct sched_domain *sd = &per_cpu(core_domains, i);
7538
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007539 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007540 }
7541#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007543 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007544 struct sched_domain *sd = &per_cpu(phys_domains, i);
7545
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007546 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 }
7548
John Hawkes9c1cfda2005-09-06 15:18:14 -07007549#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007550 for (i = 0; i < MAX_NUMNODES; i++)
7551 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007552
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007553 if (sd_allnodes) {
7554 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007555
Mike Travis7c16ec52008-04-04 18:11:11 -07007556 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7557 tmpmask);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007558 init_numa_sched_groups_power(sg);
7559 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007560#endif
7561
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007563 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564 struct sched_domain *sd;
7565#ifdef CONFIG_SCHED_SMT
7566 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007567#elif defined(CONFIG_SCHED_MC)
7568 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569#else
7570 sd = &per_cpu(phys_domains, i);
7571#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007572 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007574
Mike Travis7c16ec52008-04-04 18:11:11 -07007575 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007576 return 0;
7577
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007578#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007579error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007580 free_sched_groups(cpu_map, tmpmask);
7581 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007582 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007583#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584}
Paul Jackson029190c2007-10-18 23:40:20 -07007585
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007586static int build_sched_domains(const cpumask_t *cpu_map)
7587{
7588 return __build_sched_domains(cpu_map, NULL);
7589}
7590
Paul Jackson029190c2007-10-18 23:40:20 -07007591static cpumask_t *doms_cur; /* current sched domains */
7592static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007593static struct sched_domain_attr *dattr_cur; /* attribues of custom domains
7594 in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007595
7596/*
7597 * Special case: If a kmalloc of a doms_cur partition (array of
7598 * cpumask_t) fails, then fallback to a single sched domain,
7599 * as determined by the single cpumask_t fallback_doms.
7600 */
7601static cpumask_t fallback_doms;
7602
Heiko Carstens22e52b02008-03-12 18:31:59 +01007603void __attribute__((weak)) arch_update_cpu_topology(void)
7604{
7605}
7606
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007607/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007608 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007609 * For now this just excludes isolated cpus, but could be used to
7610 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007611 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007612static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007613{
Milton Miller73785472007-10-24 18:23:48 +02007614 int err;
7615
Heiko Carstens22e52b02008-03-12 18:31:59 +01007616 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007617 ndoms_cur = 1;
7618 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7619 if (!doms_cur)
7620 doms_cur = &fallback_doms;
7621 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007622 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007623 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007624 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007625
7626 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007627}
7628
Mike Travis7c16ec52008-04-04 18:11:11 -07007629static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7630 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631{
Mike Travis7c16ec52008-04-04 18:11:11 -07007632 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007633}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007635/*
7636 * Detach sched domains from a group of cpus specified in cpu_map
7637 * These cpus will now be attached to the NULL domain
7638 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007639static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007640{
Mike Travis7c16ec52008-04-04 18:11:11 -07007641 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007642 int i;
7643
Milton Miller6382bc92007-10-15 17:00:19 +02007644 unregister_sched_domain_sysctl();
7645
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007646 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007647 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007648 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007649 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007650}
7651
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007652/* handle null as "default" */
7653static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7654 struct sched_domain_attr *new, int idx_new)
7655{
7656 struct sched_domain_attr tmp;
7657
7658 /* fast path */
7659 if (!new && !cur)
7660 return 1;
7661
7662 tmp = SD_ATTR_INIT;
7663 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7664 new ? (new + idx_new) : &tmp,
7665 sizeof(struct sched_domain_attr));
7666}
7667
Paul Jackson029190c2007-10-18 23:40:20 -07007668/*
7669 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007670 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007671 * doms_new[] to the current sched domain partitioning, doms_cur[].
7672 * It destroys each deleted domain and builds each new domain.
7673 *
7674 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007675 * The masks don't intersect (don't overlap.) We should setup one
7676 * sched domain for each mask. CPUs not in any of the cpumasks will
7677 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007678 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7679 * it as it is.
7680 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007681 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7682 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007683 * failed the kmalloc call, then it can pass in doms_new == NULL,
7684 * and partition_sched_domains() will fallback to the single partition
7685 * 'fallback_doms'.
7686 *
7687 * Call with hotplug lock held
7688 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007689void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7690 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007691{
7692 int i, j;
7693
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007694 lock_doms_cur();
7695
Milton Miller73785472007-10-24 18:23:48 +02007696 /* always unregister in case we don't destroy any domains */
7697 unregister_sched_domain_sysctl();
7698
Paul Jackson029190c2007-10-18 23:40:20 -07007699 if (doms_new == NULL) {
7700 ndoms_new = 1;
7701 doms_new = &fallback_doms;
7702 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007703 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007704 }
7705
7706 /* Destroy deleted domains */
7707 for (i = 0; i < ndoms_cur; i++) {
7708 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007709 if (cpus_equal(doms_cur[i], doms_new[j])
7710 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007711 goto match1;
7712 }
7713 /* no match - a current sched domain not in new doms_new[] */
7714 detach_destroy_domains(doms_cur + i);
7715match1:
7716 ;
7717 }
7718
7719 /* Build new domains */
7720 for (i = 0; i < ndoms_new; i++) {
7721 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007722 if (cpus_equal(doms_new[i], doms_cur[j])
7723 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007724 goto match2;
7725 }
7726 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007727 __build_sched_domains(doms_new + i,
7728 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007729match2:
7730 ;
7731 }
7732
7733 /* Remember the new sched domains */
7734 if (doms_cur != &fallback_doms)
7735 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007736 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007737 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007738 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007739 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007740
7741 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007742
7743 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007744}
7745
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007746#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007747int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007748{
7749 int err;
7750
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007751 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007752 detach_destroy_domains(&cpu_online_map);
7753 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007754 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007755
7756 return err;
7757}
7758
7759static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7760{
7761 int ret;
7762
7763 if (buf[0] != '0' && buf[0] != '1')
7764 return -EINVAL;
7765
7766 if (smt)
7767 sched_smt_power_savings = (buf[0] == '1');
7768 else
7769 sched_mc_power_savings = (buf[0] == '1');
7770
7771 ret = arch_reinit_sched_domains();
7772
7773 return ret ? ret : count;
7774}
7775
Adrian Bunk6707de002007-08-12 18:08:19 +02007776#ifdef CONFIG_SCHED_MC
7777static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7778{
7779 return sprintf(page, "%u\n", sched_mc_power_savings);
7780}
7781static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7782 const char *buf, size_t count)
7783{
7784 return sched_power_savings_store(buf, count, 0);
7785}
7786static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7787 sched_mc_power_savings_store);
7788#endif
7789
7790#ifdef CONFIG_SCHED_SMT
7791static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7792{
7793 return sprintf(page, "%u\n", sched_smt_power_savings);
7794}
7795static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7796 const char *buf, size_t count)
7797{
7798 return sched_power_savings_store(buf, count, 1);
7799}
7800static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7801 sched_smt_power_savings_store);
7802#endif
7803
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007804int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7805{
7806 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007807
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007808#ifdef CONFIG_SCHED_SMT
7809 if (smt_capable())
7810 err = sysfs_create_file(&cls->kset.kobj,
7811 &attr_sched_smt_power_savings.attr);
7812#endif
7813#ifdef CONFIG_SCHED_MC
7814 if (!err && mc_capable())
7815 err = sysfs_create_file(&cls->kset.kobj,
7816 &attr_sched_mc_power_savings.attr);
7817#endif
7818 return err;
7819}
7820#endif
7821
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007823 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007825 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826 * which will prevent rebalancing while the sched domains are recalculated.
7827 */
7828static int update_sched_domains(struct notifier_block *nfb,
7829 unsigned long action, void *hcpu)
7830{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831 switch (action) {
7832 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007833 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007835 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007836 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 return NOTIFY_OK;
7838
7839 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007840 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007842 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007844 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007846 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847 /*
7848 * Fall through and re-initialise the domains.
7849 */
7850 break;
7851 default:
7852 return NOTIFY_DONE;
7853 }
7854
7855 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007856 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857
7858 return NOTIFY_OK;
7859}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860
7861void __init sched_init_smp(void)
7862{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007863 cpumask_t non_isolated_cpus;
7864
Mike Travis434d53b2008-04-04 18:11:04 -07007865#if defined(CONFIG_NUMA)
7866 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7867 GFP_KERNEL);
7868 BUG_ON(sched_group_nodes_bycpu == NULL);
7869#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007870 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007871 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007872 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007873 if (cpus_empty(non_isolated_cpus))
7874 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007875 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 /* XXX: Theoretical race here - CPU may be hotplugged now */
7877 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007878
7879 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07007880 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007881 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007882 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883}
7884#else
7885void __init sched_init_smp(void)
7886{
Mike Travis434d53b2008-04-04 18:11:04 -07007887#if defined(CONFIG_NUMA)
7888 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7889 GFP_KERNEL);
7890 BUG_ON(sched_group_nodes_bycpu == NULL);
7891#endif
Ingo Molnar19978ca2007-11-09 22:39:38 +01007892 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893}
7894#endif /* CONFIG_SMP */
7895
7896int in_sched_functions(unsigned long addr)
7897{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 return in_lock_functions(addr) ||
7899 (addr >= (unsigned long)__sched_text_start
7900 && addr < (unsigned long)__sched_text_end);
7901}
7902
Alexey Dobriyana9957442007-10-15 17:00:13 +02007903static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007904{
7905 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007906 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02007907#ifdef CONFIG_FAIR_GROUP_SCHED
7908 cfs_rq->rq = rq;
7909#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007910 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007911}
7912
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007913static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7914{
7915 struct rt_prio_array *array;
7916 int i;
7917
7918 array = &rt_rq->active;
7919 for (i = 0; i < MAX_RT_PRIO; i++) {
7920 INIT_LIST_HEAD(array->queue + i);
7921 __clear_bit(i, array->bitmap);
7922 }
7923 /* delimiter for bitsearch: */
7924 __set_bit(MAX_RT_PRIO, array->bitmap);
7925
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007926#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007927 rt_rq->highest_prio = MAX_RT_PRIO;
7928#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007929#ifdef CONFIG_SMP
7930 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007931 rt_rq->overloaded = 0;
7932#endif
7933
7934 rt_rq->rt_time = 0;
7935 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007936 rt_rq->rt_runtime = 0;
7937 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007938
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007939#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007940 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007941 rt_rq->rq = rq;
7942#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007943}
7944
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007945#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007946static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7947 struct sched_entity *se, int cpu, int add,
7948 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007949{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007950 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007951 tg->cfs_rq[cpu] = cfs_rq;
7952 init_cfs_rq(cfs_rq, rq);
7953 cfs_rq->tg = tg;
7954 if (add)
7955 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7956
7957 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007958 /* se could be NULL for init_task_group */
7959 if (!se)
7960 return;
7961
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007962 if (!parent)
7963 se->cfs_rq = &rq->cfs;
7964 else
7965 se->cfs_rq = parent->my_q;
7966
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007967 se->my_q = cfs_rq;
7968 se->load.weight = tg->shares;
7969 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007970 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007971}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007972#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007973
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007974#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007975static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
7976 struct sched_rt_entity *rt_se, int cpu, int add,
7977 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007978{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007979 struct rq *rq = cpu_rq(cpu);
7980
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007981 tg->rt_rq[cpu] = rt_rq;
7982 init_rt_rq(rt_rq, rq);
7983 rt_rq->tg = tg;
7984 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007985 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007986 if (add)
7987 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7988
7989 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007990 if (!rt_se)
7991 return;
7992
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007993 if (!parent)
7994 rt_se->rt_rq = &rq->rt;
7995 else
7996 rt_se->rt_rq = parent->my_q;
7997
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007998 rt_se->rt_rq = &rq->rt;
7999 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008000 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008001 INIT_LIST_HEAD(&rt_se->run_list);
8002}
8003#endif
8004
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005void __init sched_init(void)
8006{
Ingo Molnardd41f592007-07-09 18:51:59 +02008007 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008008 unsigned long alloc_size = 0, ptr;
8009
8010#ifdef CONFIG_FAIR_GROUP_SCHED
8011 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8012#endif
8013#ifdef CONFIG_RT_GROUP_SCHED
8014 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8015#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008016#ifdef CONFIG_USER_SCHED
8017 alloc_size *= 2;
8018#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008019 /*
8020 * As sched_init() is called before page_alloc is setup,
8021 * we use alloc_bootmem().
8022 */
8023 if (alloc_size) {
8024 ptr = (unsigned long)alloc_bootmem_low(alloc_size);
8025
8026#ifdef CONFIG_FAIR_GROUP_SCHED
8027 init_task_group.se = (struct sched_entity **)ptr;
8028 ptr += nr_cpu_ids * sizeof(void **);
8029
8030 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8031 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008032
8033#ifdef CONFIG_USER_SCHED
8034 root_task_group.se = (struct sched_entity **)ptr;
8035 ptr += nr_cpu_ids * sizeof(void **);
8036
8037 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8038 ptr += nr_cpu_ids * sizeof(void **);
8039#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008040#endif
8041#ifdef CONFIG_RT_GROUP_SCHED
8042 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8043 ptr += nr_cpu_ids * sizeof(void **);
8044
8045 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008046 ptr += nr_cpu_ids * sizeof(void **);
8047
8048#ifdef CONFIG_USER_SCHED
8049 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8050 ptr += nr_cpu_ids * sizeof(void **);
8051
8052 root_task_group.rt_rq = (struct rt_rq **)ptr;
8053 ptr += nr_cpu_ids * sizeof(void **);
8054#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008055#endif
8056 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008057
Gregory Haskins57d885f2008-01-25 21:08:18 +01008058#ifdef CONFIG_SMP
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008059 init_aggregate();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008060 init_defrootdomain();
8061#endif
8062
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008063 init_rt_bandwidth(&def_rt_bandwidth,
8064 global_rt_period(), global_rt_runtime());
8065
8066#ifdef CONFIG_RT_GROUP_SCHED
8067 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8068 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008069#ifdef CONFIG_USER_SCHED
8070 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8071 global_rt_period(), RUNTIME_INF);
8072#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008073#endif
8074
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008075#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008076 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008077 INIT_LIST_HEAD(&init_task_group.children);
8078
8079#ifdef CONFIG_USER_SCHED
8080 INIT_LIST_HEAD(&root_task_group.children);
8081 init_task_group.parent = &root_task_group;
8082 list_add(&init_task_group.siblings, &root_task_group.children);
8083#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008084#endif
8085
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008086 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008087 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088
8089 rq = cpu_rq(i);
8090 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07008091 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07008092 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008093 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02008094 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02008095 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008096 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008097#ifdef CONFIG_FAIR_GROUP_SCHED
8098 init_task_group.shares = init_task_group_load;
8099 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008100#ifdef CONFIG_CGROUP_SCHED
8101 /*
8102 * How much cpu bandwidth does init_task_group get?
8103 *
8104 * In case of task-groups formed thr' the cgroup filesystem, it
8105 * gets 100% of the cpu resources in the system. This overall
8106 * system cpu resource is divided among the tasks of
8107 * init_task_group and its child task-groups in a fair manner,
8108 * based on each entity's (task or task-group's) weight
8109 * (se->load.weight).
8110 *
8111 * In other words, if init_task_group has 10 tasks of weight
8112 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8113 * then A0's share of the cpu resource is:
8114 *
8115 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8116 *
8117 * We achieve this by letting init_task_group's tasks sit
8118 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8119 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008120 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008121#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008122 root_task_group.shares = NICE_0_LOAD;
8123 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008124 /*
8125 * In case of task-groups formed thr' the user id of tasks,
8126 * init_task_group represents tasks belonging to root user.
8127 * Hence it forms a sibling of all subsequent groups formed.
8128 * In this case, init_task_group gets only a fraction of overall
8129 * system cpu resource, based on the weight assigned to root
8130 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8131 * by letting tasks of init_task_group sit in a separate cfs_rq
8132 * (init_cfs_rq) and having one entity represent this group of
8133 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8134 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008135 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008136 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008137 &per_cpu(init_sched_entity, i), i, 1,
8138 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008139
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008140#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008141#endif /* CONFIG_FAIR_GROUP_SCHED */
8142
8143 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008144#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008145 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008146#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008147 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008148#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008149 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008150 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008151 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008152 &per_cpu(init_sched_rt_entity, i), i, 1,
8153 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008154#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008155#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156
Ingo Molnardd41f592007-07-09 18:51:59 +02008157 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8158 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008160 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008161 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008162 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008163 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008164 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008165 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166 rq->migration_thread = NULL;
8167 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008168 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008170 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008172 }
8173
Peter Williams2dd73a42006-06-27 02:54:34 -07008174 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008175
Avi Kivitye107be32007-07-26 13:40:43 +02008176#ifdef CONFIG_PREEMPT_NOTIFIERS
8177 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8178#endif
8179
Christoph Lameterc9819f42006-12-10 02:20:25 -08008180#ifdef CONFIG_SMP
8181 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8182#endif
8183
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008184#ifdef CONFIG_RT_MUTEXES
8185 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8186#endif
8187
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 /*
8189 * The boot idle thread does lazy MMU switching as well:
8190 */
8191 atomic_inc(&init_mm.mm_count);
8192 enter_lazy_tlb(&init_mm, current);
8193
8194 /*
8195 * Make us the idle thread. Technically, schedule() should not be
8196 * called from this thread, however somewhere below it might be,
8197 * but because we are the idle thread, we just pick up running again
8198 * when this runqueue becomes "idle".
8199 */
8200 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008201 /*
8202 * During early bootup we pretend to be a normal task:
8203 */
8204 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008205
8206 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207}
8208
8209#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8210void __might_sleep(char *file, int line)
8211{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008212#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213 static unsigned long prev_jiffy; /* ratelimiting */
8214
8215 if ((in_atomic() || irqs_disabled()) &&
8216 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8217 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8218 return;
8219 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008220 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 " context at %s:%d\n", file, line);
8222 printk("in_atomic():%d, irqs_disabled():%d\n",
8223 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008224 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008225 if (irqs_disabled())
8226 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 dump_stack();
8228 }
8229#endif
8230}
8231EXPORT_SYMBOL(__might_sleep);
8232#endif
8233
8234#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008235static void normalize_task(struct rq *rq, struct task_struct *p)
8236{
8237 int on_rq;
8238 update_rq_clock(rq);
8239 on_rq = p->se.on_rq;
8240 if (on_rq)
8241 deactivate_task(rq, p, 0);
8242 __setscheduler(rq, p, SCHED_NORMAL, 0);
8243 if (on_rq) {
8244 activate_task(rq, p, 0);
8245 resched_task(rq->curr);
8246 }
8247}
8248
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249void normalize_rt_tasks(void)
8250{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008251 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008253 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008255 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008256 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008257 /*
8258 * Only normalize user tasks:
8259 */
8260 if (!p->mm)
8261 continue;
8262
Ingo Molnardd41f592007-07-09 18:51:59 +02008263 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008264#ifdef CONFIG_SCHEDSTATS
8265 p->se.wait_start = 0;
8266 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008267 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008268#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008269 task_rq(p)->clock = 0;
8270
8271 if (!rt_task(p)) {
8272 /*
8273 * Renice negative nice level userspace
8274 * tasks back to 0:
8275 */
8276 if (TASK_NICE(p) < 0 && p->mm)
8277 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008278 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008280
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008281 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008282 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008283
Ingo Molnar178be792007-10-15 17:00:18 +02008284 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008285
Ingo Molnarb29739f2006-06-27 02:54:51 -07008286 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008287 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008288 } while_each_thread(g, p);
8289
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008290 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291}
8292
8293#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008294
8295#ifdef CONFIG_IA64
8296/*
8297 * These functions are only useful for the IA64 MCA handling.
8298 *
8299 * They can only be called when the whole system has been
8300 * stopped - every CPU needs to be quiescent, and no scheduling
8301 * activity can take place. Using them for anything else would
8302 * be a serious bug, and as a result, they aren't even visible
8303 * under any other configuration.
8304 */
8305
8306/**
8307 * curr_task - return the current task for a given cpu.
8308 * @cpu: the processor in question.
8309 *
8310 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8311 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008312struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008313{
8314 return cpu_curr(cpu);
8315}
8316
8317/**
8318 * set_curr_task - set the current task for a given cpu.
8319 * @cpu: the processor in question.
8320 * @p: the task pointer to set.
8321 *
8322 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008323 * are serviced on a separate stack. It allows the architecture to switch the
8324 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008325 * must be called with all CPU's synchronized, and interrupts disabled, the
8326 * and caller must save the original value of the current task (see
8327 * curr_task() above) and restore that value before reenabling interrupts and
8328 * re-starting the system.
8329 *
8330 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8331 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008332void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008333{
8334 cpu_curr(cpu) = p;
8335}
8336
8337#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008338
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008339#ifdef CONFIG_FAIR_GROUP_SCHED
8340static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008341{
8342 int i;
8343
8344 for_each_possible_cpu(i) {
8345 if (tg->cfs_rq)
8346 kfree(tg->cfs_rq[i]);
8347 if (tg->se)
8348 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008349 }
8350
8351 kfree(tg->cfs_rq);
8352 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008353}
8354
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008355static
8356int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008357{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008358 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008359 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008360 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008361 int i;
8362
Mike Travis434d53b2008-04-04 18:11:04 -07008363 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008364 if (!tg->cfs_rq)
8365 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008366 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008367 if (!tg->se)
8368 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008369
8370 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008371
8372 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008373 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008374
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008375 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8376 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008377 if (!cfs_rq)
8378 goto err;
8379
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008380 se = kmalloc_node(sizeof(struct sched_entity),
8381 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008382 if (!se)
8383 goto err;
8384
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008385 parent_se = parent ? parent->se[i] : NULL;
8386 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008387 }
8388
8389 return 1;
8390
8391 err:
8392 return 0;
8393}
8394
8395static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8396{
8397 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8398 &cpu_rq(cpu)->leaf_cfs_rq_list);
8399}
8400
8401static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8402{
8403 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8404}
8405#else
8406static inline void free_fair_sched_group(struct task_group *tg)
8407{
8408}
8409
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008410static inline
8411int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008412{
8413 return 1;
8414}
8415
8416static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8417{
8418}
8419
8420static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8421{
8422}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008423#endif
8424
8425#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008426static void free_rt_sched_group(struct task_group *tg)
8427{
8428 int i;
8429
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008430 destroy_rt_bandwidth(&tg->rt_bandwidth);
8431
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008432 for_each_possible_cpu(i) {
8433 if (tg->rt_rq)
8434 kfree(tg->rt_rq[i]);
8435 if (tg->rt_se)
8436 kfree(tg->rt_se[i]);
8437 }
8438
8439 kfree(tg->rt_rq);
8440 kfree(tg->rt_se);
8441}
8442
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008443static
8444int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008445{
8446 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008447 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008448 struct rq *rq;
8449 int i;
8450
Mike Travis434d53b2008-04-04 18:11:04 -07008451 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008452 if (!tg->rt_rq)
8453 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008454 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008455 if (!tg->rt_se)
8456 goto err;
8457
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008458 init_rt_bandwidth(&tg->rt_bandwidth,
8459 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008460
8461 for_each_possible_cpu(i) {
8462 rq = cpu_rq(i);
8463
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008464 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8465 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8466 if (!rt_rq)
8467 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008468
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008469 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8470 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8471 if (!rt_se)
8472 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008473
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008474 parent_se = parent ? parent->rt_se[i] : NULL;
8475 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008476 }
8477
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008478 return 1;
8479
8480 err:
8481 return 0;
8482}
8483
8484static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8485{
8486 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8487 &cpu_rq(cpu)->leaf_rt_rq_list);
8488}
8489
8490static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8491{
8492 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8493}
8494#else
8495static inline void free_rt_sched_group(struct task_group *tg)
8496{
8497}
8498
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008499static inline
8500int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008501{
8502 return 1;
8503}
8504
8505static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8506{
8507}
8508
8509static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8510{
8511}
8512#endif
8513
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008514#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008515static void free_sched_group(struct task_group *tg)
8516{
8517 free_fair_sched_group(tg);
8518 free_rt_sched_group(tg);
8519 kfree(tg);
8520}
8521
8522/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008523struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008524{
8525 struct task_group *tg;
8526 unsigned long flags;
8527 int i;
8528
8529 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8530 if (!tg)
8531 return ERR_PTR(-ENOMEM);
8532
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008533 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008534 goto err;
8535
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008536 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008537 goto err;
8538
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008539 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008540 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008541 register_fair_sched_group(tg, i);
8542 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008543 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008544 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008545
8546 WARN_ON(!parent); /* root should already exist */
8547
8548 tg->parent = parent;
8549 list_add_rcu(&tg->siblings, &parent->children);
8550 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008551 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008552
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008553 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008554
8555err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008556 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008557 return ERR_PTR(-ENOMEM);
8558}
8559
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008560/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008561static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008562{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008563 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008564 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008565}
8566
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008567/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008568void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008569{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008570 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008571 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008572
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008573 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008574 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008575 unregister_fair_sched_group(tg, i);
8576 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008577 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008578 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008579 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008580 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008581
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008582 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008583 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008584}
8585
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008586/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008587 * The caller of this function should have put the task in its new group
8588 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8589 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008590 */
8591void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008592{
8593 int on_rq, running;
8594 unsigned long flags;
8595 struct rq *rq;
8596
8597 rq = task_rq_lock(tsk, &flags);
8598
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008599 update_rq_clock(rq);
8600
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008601 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008602 on_rq = tsk->se.on_rq;
8603
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008604 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008605 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008606 if (unlikely(running))
8607 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008608
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008609 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008610
Peter Zijlstra810b3812008-02-29 15:21:01 -05008611#ifdef CONFIG_FAIR_GROUP_SCHED
8612 if (tsk->sched_class->moved_group)
8613 tsk->sched_class->moved_group(tsk);
8614#endif
8615
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008616 if (unlikely(running))
8617 tsk->sched_class->set_curr_task(rq);
8618 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008619 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008620
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008621 task_rq_unlock(rq, &flags);
8622}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008623#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008624
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008625#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008626static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008627{
8628 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008629 int on_rq;
8630
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008631 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008632 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008633 dequeue_entity(cfs_rq, se, 0);
8634
8635 se->load.weight = shares;
8636 se->load.inv_weight = div64_64((1ULL<<32), shares);
8637
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008638 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008639 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008640}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008641
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008642static void set_se_shares(struct sched_entity *se, unsigned long shares)
8643{
8644 struct cfs_rq *cfs_rq = se->cfs_rq;
8645 struct rq *rq = cfs_rq->rq;
8646 unsigned long flags;
8647
8648 spin_lock_irqsave(&rq->lock, flags);
8649 __set_se_shares(se, shares);
8650 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008651}
8652
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008653static DEFINE_MUTEX(shares_mutex);
8654
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008655int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008656{
8657 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008658 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008659
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008660 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008661 * We can't change the weight of the root cgroup.
8662 */
8663 if (!tg->se[0])
8664 return -EINVAL;
8665
8666 /*
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008667 * A weight of 0 or 1 can cause arithmetics problems.
8668 * (The default weight is 1024 - so there's no practical
8669 * limitation from this.)
8670 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008671 if (shares < MIN_SHARES)
8672 shares = MIN_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008673
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008674 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008675 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008676 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008677
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008678 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008679 for_each_possible_cpu(i)
8680 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008681 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008682 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008683
8684 /* wait for any ongoing reference to this group to finish */
8685 synchronize_sched();
8686
8687 /*
8688 * Now we are free to modify the group's share on each cpu
8689 * w/o tripping rebalance_share or load_balance_fair.
8690 */
8691 tg->shares = shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008692 for_each_possible_cpu(i) {
8693 /*
8694 * force a rebalance
8695 */
8696 cfs_rq_set_shares(tg->cfs_rq[i], 0);
8697 set_se_shares(tg->se[i], shares/nr_cpu_ids);
8698 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008699
8700 /*
8701 * Enable load balance activity on this group, by inserting it back on
8702 * each cpu's rq->leaf_cfs_rq_list.
8703 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008704 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008705 for_each_possible_cpu(i)
8706 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008707 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008708 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008709done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008710 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008711 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008712}
8713
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008714unsigned long sched_group_shares(struct task_group *tg)
8715{
8716 return tg->shares;
8717}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008718#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008719
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008720#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008721/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008722 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008723 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008724static DEFINE_MUTEX(rt_constraints_mutex);
8725
8726static unsigned long to_ratio(u64 period, u64 runtime)
8727{
8728 if (runtime == RUNTIME_INF)
8729 return 1ULL << 16;
8730
Peter Zijlstra2692a242008-02-27 12:00:46 +01008731 return div64_64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008732}
8733
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008734#ifdef CONFIG_CGROUP_SCHED
8735static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8736{
8737 struct task_group *tgi, *parent = tg->parent;
8738 unsigned long total = 0;
8739
8740 if (!parent) {
8741 if (global_rt_period() < period)
8742 return 0;
8743
8744 return to_ratio(period, runtime) <
8745 to_ratio(global_rt_period(), global_rt_runtime());
8746 }
8747
8748 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8749 return 0;
8750
8751 rcu_read_lock();
8752 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8753 if (tgi == tg)
8754 continue;
8755
8756 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8757 tgi->rt_bandwidth.rt_runtime);
8758 }
8759 rcu_read_unlock();
8760
8761 return total + to_ratio(period, runtime) <
8762 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8763 parent->rt_bandwidth.rt_runtime);
8764}
8765#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008766static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008767{
8768 struct task_group *tgi;
8769 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008770 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008771 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008772
8773 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008774 list_for_each_entry_rcu(tgi, &task_groups, list) {
8775 if (tgi == tg)
8776 continue;
8777
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008778 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8779 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008780 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008781 rcu_read_unlock();
8782
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008783 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008784}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008785#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008786
Dhaval Giani521f1a242008-02-28 15:21:56 +05308787/* Must be called with tasklist_lock held */
8788static inline int tg_has_rt_tasks(struct task_group *tg)
8789{
8790 struct task_struct *g, *p;
8791 do_each_thread(g, p) {
8792 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8793 return 1;
8794 } while_each_thread(g, p);
8795 return 0;
8796}
8797
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008798static int tg_set_bandwidth(struct task_group *tg,
8799 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008800{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008801 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008802
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008803 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308804 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008805 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308806 err = -EBUSY;
8807 goto unlock;
8808 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008809 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8810 err = -EINVAL;
8811 goto unlock;
8812 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008813
8814 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008815 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8816 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008817
8818 for_each_possible_cpu(i) {
8819 struct rt_rq *rt_rq = tg->rt_rq[i];
8820
8821 spin_lock(&rt_rq->rt_runtime_lock);
8822 rt_rq->rt_runtime = rt_runtime;
8823 spin_unlock(&rt_rq->rt_runtime_lock);
8824 }
8825 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008826 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308827 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008828 mutex_unlock(&rt_constraints_mutex);
8829
8830 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008831}
8832
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008833int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8834{
8835 u64 rt_runtime, rt_period;
8836
8837 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8838 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8839 if (rt_runtime_us < 0)
8840 rt_runtime = RUNTIME_INF;
8841
8842 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8843}
8844
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008845long sched_group_rt_runtime(struct task_group *tg)
8846{
8847 u64 rt_runtime_us;
8848
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008849 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008850 return -1;
8851
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008852 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008853 do_div(rt_runtime_us, NSEC_PER_USEC);
8854 return rt_runtime_us;
8855}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008856
8857int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8858{
8859 u64 rt_runtime, rt_period;
8860
8861 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8862 rt_runtime = tg->rt_bandwidth.rt_runtime;
8863
8864 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8865}
8866
8867long sched_group_rt_period(struct task_group *tg)
8868{
8869 u64 rt_period_us;
8870
8871 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8872 do_div(rt_period_us, NSEC_PER_USEC);
8873 return rt_period_us;
8874}
8875
8876static int sched_rt_global_constraints(void)
8877{
8878 int ret = 0;
8879
8880 mutex_lock(&rt_constraints_mutex);
8881 if (!__rt_schedulable(NULL, 1, 0))
8882 ret = -EINVAL;
8883 mutex_unlock(&rt_constraints_mutex);
8884
8885 return ret;
8886}
8887#else
8888static int sched_rt_global_constraints(void)
8889{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008890 unsigned long flags;
8891 int i;
8892
8893 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
8894 for_each_possible_cpu(i) {
8895 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8896
8897 spin_lock(&rt_rq->rt_runtime_lock);
8898 rt_rq->rt_runtime = global_rt_runtime();
8899 spin_unlock(&rt_rq->rt_runtime_lock);
8900 }
8901 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
8902
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008903 return 0;
8904}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008905#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008906
8907int sched_rt_handler(struct ctl_table *table, int write,
8908 struct file *filp, void __user *buffer, size_t *lenp,
8909 loff_t *ppos)
8910{
8911 int ret;
8912 int old_period, old_runtime;
8913 static DEFINE_MUTEX(mutex);
8914
8915 mutex_lock(&mutex);
8916 old_period = sysctl_sched_rt_period;
8917 old_runtime = sysctl_sched_rt_runtime;
8918
8919 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8920
8921 if (!ret && write) {
8922 ret = sched_rt_global_constraints();
8923 if (ret) {
8924 sysctl_sched_rt_period = old_period;
8925 sysctl_sched_rt_runtime = old_runtime;
8926 } else {
8927 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8928 def_rt_bandwidth.rt_period =
8929 ns_to_ktime(global_rt_period());
8930 }
8931 }
8932 mutex_unlock(&mutex);
8933
8934 return ret;
8935}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008936
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008937#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008938
8939/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008940static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008941{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008942 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8943 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008944}
8945
8946static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008947cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008948{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008949 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008950
Paul Menage2b01dfe2007-10-24 18:23:50 +02008951 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008952 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008953 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008954 return &init_task_group.css;
8955 }
8956
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008957 parent = cgroup_tg(cgrp->parent);
8958 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008959 if (IS_ERR(tg))
8960 return ERR_PTR(-ENOMEM);
8961
8962 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008963 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008964
8965 return &tg->css;
8966}
8967
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008968static void
8969cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008970{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008971 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008972
8973 sched_destroy_group(tg);
8974}
8975
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008976static int
8977cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8978 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008979{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008980#ifdef CONFIG_RT_GROUP_SCHED
8981 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008982 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008983 return -EINVAL;
8984#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008985 /* We don't support RT-tasks being in separate groups */
8986 if (tsk->sched_class != &fair_sched_class)
8987 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008988#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008989
8990 return 0;
8991}
8992
8993static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008994cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008995 struct cgroup *old_cont, struct task_struct *tsk)
8996{
8997 sched_move_task(tsk);
8998}
8999
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009000#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menage2b01dfe2007-10-24 18:23:50 +02009001static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9002 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009003{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009004 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009005}
9006
Paul Menage2b01dfe2007-10-24 18:23:50 +02009007static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009008{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009009 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009010
9011 return (u64) tg->shares;
9012}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009013#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009014
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009015#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009016static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009017 struct file *file,
9018 const char __user *userbuf,
9019 size_t nbytes, loff_t *unused_ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009020{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009021 char buffer[64];
9022 int retval = 0;
9023 s64 val;
9024 char *end;
9025
9026 if (!nbytes)
9027 return -EINVAL;
9028 if (nbytes >= sizeof(buffer))
9029 return -E2BIG;
9030 if (copy_from_user(buffer, userbuf, nbytes))
9031 return -EFAULT;
9032
9033 buffer[nbytes] = 0; /* nul-terminate */
9034
9035 /* strip newline if necessary */
9036 if (nbytes && (buffer[nbytes-1] == '\n'))
9037 buffer[nbytes-1] = 0;
9038 val = simple_strtoll(buffer, &end, 0);
9039 if (*end)
9040 return -EINVAL;
9041
9042 /* Pass to subsystem */
9043 retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
9044 if (!retval)
9045 retval = nbytes;
9046 return retval;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009047}
9048
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009049static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
9050 struct file *file,
9051 char __user *buf, size_t nbytes,
9052 loff_t *ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009053{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009054 char tmp[64];
9055 long val = sched_group_rt_runtime(cgroup_tg(cgrp));
9056 int len = sprintf(tmp, "%ld\n", val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009057
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009058 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009059}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009060
9061static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9062 u64 rt_period_us)
9063{
9064 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9065}
9066
9067static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9068{
9069 return sched_group_rt_period(cgroup_tg(cgrp));
9070}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009071#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009072
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009073static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009074#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009075 {
9076 .name = "shares",
9077 .read_uint = cpu_shares_read_uint,
9078 .write_uint = cpu_shares_write_uint,
9079 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009080#endif
9081#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009082 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009083 .name = "rt_runtime_us",
9084 .read = cpu_rt_runtime_read,
9085 .write = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009086 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009087 {
9088 .name = "rt_period_us",
9089 .read_uint = cpu_rt_period_read_uint,
9090 .write_uint = cpu_rt_period_write_uint,
9091 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009092#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009093};
9094
9095static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9096{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009097 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009098}
9099
9100struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009101 .name = "cpu",
9102 .create = cpu_cgroup_create,
9103 .destroy = cpu_cgroup_destroy,
9104 .can_attach = cpu_cgroup_can_attach,
9105 .attach = cpu_cgroup_attach,
9106 .populate = cpu_cgroup_populate,
9107 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009108 .early_init = 1,
9109};
9110
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009111#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009112
9113#ifdef CONFIG_CGROUP_CPUACCT
9114
9115/*
9116 * CPU accounting code for task groups.
9117 *
9118 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9119 * (balbir@in.ibm.com).
9120 */
9121
9122/* track cpu usage of a group of tasks */
9123struct cpuacct {
9124 struct cgroup_subsys_state css;
9125 /* cpuusage holds pointer to a u64-type object on every cpu */
9126 u64 *cpuusage;
9127};
9128
9129struct cgroup_subsys cpuacct_subsys;
9130
9131/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309132static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009133{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309134 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009135 struct cpuacct, css);
9136}
9137
9138/* return cpu accounting group to which this task belongs */
9139static inline struct cpuacct *task_ca(struct task_struct *tsk)
9140{
9141 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9142 struct cpuacct, css);
9143}
9144
9145/* create a new cpu accounting group */
9146static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309147 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009148{
9149 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9150
9151 if (!ca)
9152 return ERR_PTR(-ENOMEM);
9153
9154 ca->cpuusage = alloc_percpu(u64);
9155 if (!ca->cpuusage) {
9156 kfree(ca);
9157 return ERR_PTR(-ENOMEM);
9158 }
9159
9160 return &ca->css;
9161}
9162
9163/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009164static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309165cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009166{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309167 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009168
9169 free_percpu(ca->cpuusage);
9170 kfree(ca);
9171}
9172
9173/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309174static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009175{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309176 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009177 u64 totalcpuusage = 0;
9178 int i;
9179
9180 for_each_possible_cpu(i) {
9181 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9182
9183 /*
9184 * Take rq->lock to make 64-bit addition safe on 32-bit
9185 * platforms.
9186 */
9187 spin_lock_irq(&cpu_rq(i)->lock);
9188 totalcpuusage += *cpuusage;
9189 spin_unlock_irq(&cpu_rq(i)->lock);
9190 }
9191
9192 return totalcpuusage;
9193}
9194
Dhaval Giani0297b802008-02-29 10:02:44 +05309195static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9196 u64 reset)
9197{
9198 struct cpuacct *ca = cgroup_ca(cgrp);
9199 int err = 0;
9200 int i;
9201
9202 if (reset) {
9203 err = -EINVAL;
9204 goto out;
9205 }
9206
9207 for_each_possible_cpu(i) {
9208 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9209
9210 spin_lock_irq(&cpu_rq(i)->lock);
9211 *cpuusage = 0;
9212 spin_unlock_irq(&cpu_rq(i)->lock);
9213 }
9214out:
9215 return err;
9216}
9217
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009218static struct cftype files[] = {
9219 {
9220 .name = "usage",
9221 .read_uint = cpuusage_read,
Dhaval Giani0297b802008-02-29 10:02:44 +05309222 .write_uint = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009223 },
9224};
9225
Dhaval Giani32cd7562008-02-29 10:02:43 +05309226static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009227{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309228 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009229}
9230
9231/*
9232 * charge this task's execution time to its accounting group.
9233 *
9234 * called with rq->lock held.
9235 */
9236static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9237{
9238 struct cpuacct *ca;
9239
9240 if (!cpuacct_subsys.active)
9241 return;
9242
9243 ca = task_ca(tsk);
9244 if (ca) {
9245 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9246
9247 *cpuusage += cputime;
9248 }
9249}
9250
9251struct cgroup_subsys cpuacct_subsys = {
9252 .name = "cpuacct",
9253 .create = cpuacct_create,
9254 .destroy = cpuacct_destroy,
9255 .populate = cpuacct_populate,
9256 .subsys_id = cpuacct_subsys_id,
9257};
9258#endif /* CONFIG_CGROUP_CPUACCT */