blob: 8f03817e7dd37594470edc097aa774cca15b3d57 [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 Molnar62160e3f2007-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 Haskinsa22d7fc12008-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 Molnar06379ab2008-04-19 09:25:58 +0200745 SCHED_FEAT_DEADLINE = 512,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200746};
747
748const_debug unsigned int sysctl_sched_features =
Ingo Molnar8401f772007-10-18 21:32:55 +0200749 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
Ingo Molnar96126332007-11-15 20:57:40 +0100750 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
Ingo Molnar8401f772007-10-18 21:32:55 +0200751 SCHED_FEAT_START_DEBIT * 1 |
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100752 SCHED_FEAT_AFFINE_WAKEUPS * 1 |
753 SCHED_FEAT_CACHE_HOT_BUDDY * 1 |
Ingo Molnar02e2b832008-03-19 01:37:10 +0100754 SCHED_FEAT_SYNC_WAKEUPS * 1 |
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100755 SCHED_FEAT_HRTICK * 1 |
Peter Zijlstra112f53f2008-03-19 11:43:36 +0100756 SCHED_FEAT_DOUBLE_TICK * 0 |
Ingo Molnar06379ab2008-04-19 09:25:58 +0200757 SCHED_FEAT_NORMALIZED_SLEEPER * 1 |
758 SCHED_FEAT_DEADLINE * 1;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200759
760#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
761
762/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100763 * Number of tasks to iterate in a single balance run.
764 * Limited because this is done with IRQs disabled.
765 */
766const_debug unsigned int sysctl_sched_nr_migrate = 32;
767
768/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100769 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100770 * default: 1s
771 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100772unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100773
Ingo Molnar6892b752008-02-13 14:02:36 +0100774static __read_mostly int scheduler_running;
775
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100776/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100777 * part of the period that we allow rt tasks to run in us.
778 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100779 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100780int sysctl_sched_rt_runtime = 950000;
781
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200782static inline u64 global_rt_period(void)
783{
784 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
785}
786
787static inline u64 global_rt_runtime(void)
788{
789 if (sysctl_sched_rt_period < 0)
790 return RUNTIME_INF;
791
792 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
793}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100794
Ingo Molnar27ec4402008-02-28 21:00:21 +0100795static const unsigned long long time_sync_thresh = 100000;
796
797static DEFINE_PER_CPU(unsigned long long, time_offset);
798static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
799
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100800/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100801 * Global lock which we take every now and then to synchronize
802 * the CPUs time. This method is not warp-safe, but it's good
803 * enough to synchronize slowly diverging time sources and thus
804 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200805 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100806static DEFINE_SPINLOCK(time_sync_lock);
807static unsigned long long prev_global_time;
808
809static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
810{
811 unsigned long flags;
812
813 spin_lock_irqsave(&time_sync_lock, flags);
814
815 if (time < prev_global_time) {
816 per_cpu(time_offset, cpu) += prev_global_time - time;
817 time = prev_global_time;
818 } else {
819 prev_global_time = time;
820 }
821
822 spin_unlock_irqrestore(&time_sync_lock, flags);
823
824 return time;
825}
826
827static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200828{
Ingo Molnare436d802007-07-19 21:28:35 +0200829 unsigned long long now;
830 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200831 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200832
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100833 /*
834 * Only call sched_clock() if the scheduler has already been
835 * initialized (some code might call cpu_clock() very early):
836 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100837 if (unlikely(!scheduler_running))
838 return 0;
839
840 local_irq_save(flags);
841 rq = cpu_rq(cpu);
842 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200843 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200844 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200845
846 return now;
847}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100848
849/*
850 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
851 * clock constructed from sched_clock():
852 */
853unsigned long long cpu_clock(int cpu)
854{
855 unsigned long long prev_cpu_time, time, delta_time;
856
857 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
858 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
859 delta_time = time-prev_cpu_time;
860
861 if (unlikely(delta_time > time_sync_thresh))
862 time = __sync_cpu_clock(time, cpu);
863
864 return time;
865}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200866EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700869# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700871#ifndef finish_arch_switch
872# define finish_arch_switch(prev) do { } while (0)
873#endif
874
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100875static inline int task_current(struct rq *rq, struct task_struct *p)
876{
877 return rq->curr == p;
878}
879
Nick Piggin4866cde2005-06-25 14:57:23 -0700880#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700881static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700882{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100883 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700884}
885
Ingo Molnar70b97a72006-07-03 00:25:42 -0700886static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700887{
888}
889
Ingo Molnar70b97a72006-07-03 00:25:42 -0700890static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700891{
Ingo Molnarda04c032005-09-13 11:17:59 +0200892#ifdef CONFIG_DEBUG_SPINLOCK
893 /* this is a valid case when another task releases the spinlock */
894 rq->lock.owner = current;
895#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700896 /*
897 * If we are tracking spinlock dependencies then we have to
898 * fix up the runqueue lock - which gets 'carried over' from
899 * prev into current:
900 */
901 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
902
Nick Piggin4866cde2005-06-25 14:57:23 -0700903 spin_unlock_irq(&rq->lock);
904}
905
906#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700907static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700908{
909#ifdef CONFIG_SMP
910 return p->oncpu;
911#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100912 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700913#endif
914}
915
Ingo Molnar70b97a72006-07-03 00:25:42 -0700916static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700917{
918#ifdef CONFIG_SMP
919 /*
920 * We can optimise this out completely for !SMP, because the
921 * SMP rebalancing from interrupt is the only thing that cares
922 * here.
923 */
924 next->oncpu = 1;
925#endif
926#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
927 spin_unlock_irq(&rq->lock);
928#else
929 spin_unlock(&rq->lock);
930#endif
931}
932
Ingo Molnar70b97a72006-07-03 00:25:42 -0700933static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700934{
935#ifdef CONFIG_SMP
936 /*
937 * After ->oncpu is cleared, the task can be moved to a different CPU.
938 * We must ensure this doesn't happen until the switch is completely
939 * finished.
940 */
941 smp_wmb();
942 prev->oncpu = 0;
943#endif
944#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
945 local_irq_enable();
946#endif
947}
948#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700951 * __task_rq_lock - lock the runqueue a given task resides on.
952 * Must be called interrupts disabled.
953 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700954static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700955 __acquires(rq->lock)
956{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200957 for (;;) {
958 struct rq *rq = task_rq(p);
959 spin_lock(&rq->lock);
960 if (likely(rq == task_rq(p)))
961 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700962 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700963 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700964}
965
966/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100968 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * explicitly disabling preemption.
970 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700971static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 __acquires(rq->lock)
973{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700974 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Andi Kleen3a5c3592007-10-15 17:00:14 +0200976 for (;;) {
977 local_irq_save(*flags);
978 rq = task_rq(p);
979 spin_lock(&rq->lock);
980 if (likely(rq == task_rq(p)))
981 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Alexey Dobriyana9957442007-10-15 17:00:13 +0200986static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700987 __releases(rq->lock)
988{
989 spin_unlock(&rq->lock);
990}
991
Ingo Molnar70b97a72006-07-03 00:25:42 -0700992static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 __releases(rq->lock)
994{
995 spin_unlock_irqrestore(&rq->lock, *flags);
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800999 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001001static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 __acquires(rq->lock)
1003{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001004 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 local_irq_disable();
1007 rq = this_rq();
1008 spin_lock(&rq->lock);
1009
1010 return rq;
1011}
1012
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001013/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001014 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001015 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001016void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001017{
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001018 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001019
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001020 spin_lock(&rq->lock);
1021 __update_rq_clock(rq);
1022 spin_unlock(&rq->lock);
1023 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001024}
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001025EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
1026
1027/*
1028 * We just idled delta nanoseconds (called with irqs disabled):
1029 */
1030void sched_clock_idle_wakeup_event(u64 delta_ns)
1031{
1032 struct rq *rq = cpu_rq(smp_processor_id());
1033 u64 now = sched_clock();
1034
1035 rq->idle_clock += delta_ns;
1036 /*
1037 * Override the previous timestamp and ignore all
1038 * sched_clock() deltas that occured while we idled,
1039 * and use the PM-provided delta_ns to advance the
1040 * rq clock:
1041 */
1042 spin_lock(&rq->lock);
1043 rq->prev_clock_raw = now;
1044 rq->clock += delta_ns;
1045 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +01001046 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001047}
1048EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001049
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001050static void __resched_task(struct task_struct *p, int tif_bit);
1051
1052static inline void resched_task(struct task_struct *p)
1053{
1054 __resched_task(p, TIF_NEED_RESCHED);
1055}
1056
1057#ifdef CONFIG_SCHED_HRTICK
1058/*
1059 * Use HR-timers to deliver accurate preemption points.
1060 *
1061 * Its all a bit involved since we cannot program an hrt while holding the
1062 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1063 * reschedule event.
1064 *
1065 * When we get rescheduled we reprogram the hrtick_timer outside of the
1066 * rq->lock.
1067 */
1068static inline void resched_hrt(struct task_struct *p)
1069{
1070 __resched_task(p, TIF_HRTICK_RESCHED);
1071}
1072
1073static inline void resched_rq(struct rq *rq)
1074{
1075 unsigned long flags;
1076
1077 spin_lock_irqsave(&rq->lock, flags);
1078 resched_task(rq->curr);
1079 spin_unlock_irqrestore(&rq->lock, flags);
1080}
1081
1082enum {
1083 HRTICK_SET, /* re-programm hrtick_timer */
1084 HRTICK_RESET, /* not a new slice */
1085};
1086
1087/*
1088 * Use hrtick when:
1089 * - enabled by features
1090 * - hrtimer is actually high res
1091 */
1092static inline int hrtick_enabled(struct rq *rq)
1093{
1094 if (!sched_feat(HRTICK))
1095 return 0;
1096 return hrtimer_is_hres_active(&rq->hrtick_timer);
1097}
1098
1099/*
1100 * Called to set the hrtick timer state.
1101 *
1102 * called with rq->lock held and irqs disabled
1103 */
1104static void hrtick_start(struct rq *rq, u64 delay, int reset)
1105{
1106 assert_spin_locked(&rq->lock);
1107
1108 /*
1109 * preempt at: now + delay
1110 */
1111 rq->hrtick_expire =
1112 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1113 /*
1114 * indicate we need to program the timer
1115 */
1116 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1117 if (reset)
1118 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1119
1120 /*
1121 * New slices are called from the schedule path and don't need a
1122 * forced reschedule.
1123 */
1124 if (reset)
1125 resched_hrt(rq->curr);
1126}
1127
1128static void hrtick_clear(struct rq *rq)
1129{
1130 if (hrtimer_active(&rq->hrtick_timer))
1131 hrtimer_cancel(&rq->hrtick_timer);
1132}
1133
1134/*
1135 * Update the timer from the possible pending state.
1136 */
1137static void hrtick_set(struct rq *rq)
1138{
1139 ktime_t time;
1140 int set, reset;
1141 unsigned long flags;
1142
1143 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1144
1145 spin_lock_irqsave(&rq->lock, flags);
1146 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1147 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1148 time = rq->hrtick_expire;
1149 clear_thread_flag(TIF_HRTICK_RESCHED);
1150 spin_unlock_irqrestore(&rq->lock, flags);
1151
1152 if (set) {
1153 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1154 if (reset && !hrtimer_active(&rq->hrtick_timer))
1155 resched_rq(rq);
1156 } else
1157 hrtick_clear(rq);
1158}
1159
1160/*
1161 * High-resolution timer tick.
1162 * Runs from hardirq context with interrupts disabled.
1163 */
1164static enum hrtimer_restart hrtick(struct hrtimer *timer)
1165{
1166 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1167
1168 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1169
1170 spin_lock(&rq->lock);
1171 __update_rq_clock(rq);
1172 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1173 spin_unlock(&rq->lock);
1174
1175 return HRTIMER_NORESTART;
1176}
1177
1178static inline void init_rq_hrtick(struct rq *rq)
1179{
1180 rq->hrtick_flags = 0;
1181 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1182 rq->hrtick_timer.function = hrtick;
1183 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1184}
1185
1186void hrtick_resched(void)
1187{
1188 struct rq *rq;
1189 unsigned long flags;
1190
1191 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1192 return;
1193
1194 local_irq_save(flags);
1195 rq = cpu_rq(smp_processor_id());
1196 hrtick_set(rq);
1197 local_irq_restore(flags);
1198}
1199#else
1200static inline void hrtick_clear(struct rq *rq)
1201{
1202}
1203
1204static inline void hrtick_set(struct rq *rq)
1205{
1206}
1207
1208static inline void init_rq_hrtick(struct rq *rq)
1209{
1210}
1211
1212void hrtick_resched(void)
1213{
1214}
1215#endif
1216
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001217/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001218 * resched_task - mark a task 'to be rescheduled now'.
1219 *
1220 * On UP this means the setting of the need_resched flag, on SMP it
1221 * might also involve a cross-CPU call to trigger the scheduler on
1222 * the target CPU.
1223 */
1224#ifdef CONFIG_SMP
1225
1226#ifndef tsk_is_polling
1227#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1228#endif
1229
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001230static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001231{
1232 int cpu;
1233
1234 assert_spin_locked(&task_rq(p)->lock);
1235
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001236 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001237 return;
1238
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001239 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001240
1241 cpu = task_cpu(p);
1242 if (cpu == smp_processor_id())
1243 return;
1244
1245 /* NEED_RESCHED must be visible before we test polling */
1246 smp_mb();
1247 if (!tsk_is_polling(p))
1248 smp_send_reschedule(cpu);
1249}
1250
1251static void resched_cpu(int cpu)
1252{
1253 struct rq *rq = cpu_rq(cpu);
1254 unsigned long flags;
1255
1256 if (!spin_trylock_irqsave(&rq->lock, flags))
1257 return;
1258 resched_task(cpu_curr(cpu));
1259 spin_unlock_irqrestore(&rq->lock, flags);
1260}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001261
1262#ifdef CONFIG_NO_HZ
1263/*
1264 * When add_timer_on() enqueues a timer into the timer wheel of an
1265 * idle CPU then this timer might expire before the next timer event
1266 * which is scheduled to wake up that CPU. In case of a completely
1267 * idle system the next event might even be infinite time into the
1268 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1269 * leaves the inner idle loop so the newly added timer is taken into
1270 * account when the CPU goes back to idle and evaluates the timer
1271 * wheel for the next timer event.
1272 */
1273void wake_up_idle_cpu(int cpu)
1274{
1275 struct rq *rq = cpu_rq(cpu);
1276
1277 if (cpu == smp_processor_id())
1278 return;
1279
1280 /*
1281 * This is safe, as this function is called with the timer
1282 * wheel base lock of (cpu) held. When the CPU is on the way
1283 * to idle and has not yet set rq->curr to idle then it will
1284 * be serialized on the timer wheel base lock and take the new
1285 * timer into account automatically.
1286 */
1287 if (rq->curr != rq->idle)
1288 return;
1289
1290 /*
1291 * We can set TIF_RESCHED on the idle task of the other CPU
1292 * lockless. The worst case is that the other CPU runs the
1293 * idle task through an additional NOOP schedule()
1294 */
1295 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1296
1297 /* NEED_RESCHED must be visible before we test polling */
1298 smp_mb();
1299 if (!tsk_is_polling(rq->idle))
1300 smp_send_reschedule(cpu);
1301}
1302#endif
1303
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001304#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001305static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001306{
1307 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001308 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001309}
1310#endif
1311
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001312#if BITS_PER_LONG == 32
1313# define WMULT_CONST (~0UL)
1314#else
1315# define WMULT_CONST (1UL << 32)
1316#endif
1317
1318#define WMULT_SHIFT 32
1319
Ingo Molnar194081e2007-08-09 11:16:51 +02001320/*
1321 * Shift right and round:
1322 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001323#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001324
Peter Zijlstra8f1bc382008-04-19 19:45:00 +02001325/*
1326 * delta *= weight / lw
1327 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001328static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001329calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1330 struct load_weight *lw)
1331{
1332 u64 tmp;
1333
1334 if (unlikely(!lw->inv_weight))
Ingo Molnar27d11722008-03-14 22:20:01 +01001335 lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001336
1337 tmp = (u64)delta_exec * weight;
1338 /*
1339 * Check whether we'd overflow the 64-bit multiplication:
1340 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001341 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001342 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001343 WMULT_SHIFT/2);
1344 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001345 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001346
Ingo Molnarecf691d2007-08-02 17:41:40 +02001347 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001348}
1349
Ingo Molnar10919852007-10-15 17:00:04 +02001350static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001351{
1352 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001353 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001354}
1355
Ingo Molnar10919852007-10-15 17:00:04 +02001356static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001357{
1358 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001359 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001360}
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001363 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1364 * of tasks with abnormal "nice" values across CPUs the contribution that
1365 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001366 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001367 * scaled version of the new time slice allocation that they receive on time
1368 * slice expiry etc.
1369 */
1370
Ingo Molnardd41f592007-07-09 18:51:59 +02001371#define WEIGHT_IDLEPRIO 2
1372#define WMULT_IDLEPRIO (1 << 31)
1373
1374/*
1375 * Nice levels are multiplicative, with a gentle 10% change for every
1376 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1377 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1378 * that remained on nice 0.
1379 *
1380 * The "10% effect" is relative and cumulative: from _any_ nice level,
1381 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee62007-07-16 09:46:30 +02001382 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1383 * If a task goes up by ~10% and another task goes down by ~10% then
1384 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001385 */
1386static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001387 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1388 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1389 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1390 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1391 /* 0 */ 1024, 820, 655, 526, 423,
1392 /* 5 */ 335, 272, 215, 172, 137,
1393 /* 10 */ 110, 87, 70, 56, 45,
1394 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001395};
1396
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001397/*
1398 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1399 *
1400 * In cases where the weight does not change often, we can use the
1401 * precalculated inverse to speed up arithmetics by turning divisions
1402 * into multiplications:
1403 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001404static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001405 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1406 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1407 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1408 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1409 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1410 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1411 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1412 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001413};
Peter Williams2dd73a42006-06-27 02:54:34 -07001414
Ingo Molnardd41f592007-07-09 18:51:59 +02001415static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1416
1417/*
1418 * runqueue iterator, to support SMP load-balancing between different
1419 * scheduling classes, without having to expose their internal data
1420 * structures to the load-balancing proper:
1421 */
1422struct rq_iterator {
1423 void *arg;
1424 struct task_struct *(*start)(void *);
1425 struct task_struct *(*next)(void *);
1426};
1427
Peter Williamse1d14842007-10-24 18:23:51 +02001428#ifdef CONFIG_SMP
1429static unsigned long
1430balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1431 unsigned long max_load_move, struct sched_domain *sd,
1432 enum cpu_idle_type idle, int *all_pinned,
1433 int *this_best_prio, struct rq_iterator *iterator);
1434
1435static int
1436iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1437 struct sched_domain *sd, enum cpu_idle_type idle,
1438 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001439#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001440
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001441#ifdef CONFIG_CGROUP_CPUACCT
1442static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1443#else
1444static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1445#endif
1446
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001447static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1448{
1449 update_load_add(&rq->load, load);
1450}
1451
1452static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1453{
1454 update_load_sub(&rq->load, load);
1455}
1456
Gregory Haskinse7693a32008-01-25 21:08:09 +01001457#ifdef CONFIG_SMP
1458static unsigned long source_load(int cpu, int type);
1459static unsigned long target_load(int cpu, int type);
1460static unsigned long cpu_avg_load_per_task(int cpu);
1461static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001462
1463#ifdef CONFIG_FAIR_GROUP_SCHED
1464
1465/*
1466 * Group load balancing.
1467 *
1468 * We calculate a few balance domain wide aggregate numbers; load and weight.
1469 * Given the pictures below, and assuming each item has equal weight:
1470 *
1471 * root 1 - thread
1472 * / | \ A - group
1473 * A 1 B
1474 * /|\ / \
1475 * C 2 D 3 4
1476 * | |
1477 * 5 6
1478 *
1479 * load:
1480 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1481 * which equals 1/9-th of the total load.
1482 *
1483 * shares:
1484 * The weight of this group on the selected cpus.
1485 *
1486 * rq_weight:
1487 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1488 * B would get 2.
1489 *
1490 * task_weight:
1491 * Part of the rq_weight contributed by tasks; all groups except B would
1492 * get 1, B gets 2.
1493 */
1494
1495static inline struct aggregate_struct *
1496aggregate(struct task_group *tg, struct sched_domain *sd)
1497{
1498 return &tg->cfs_rq[sd->first_cpu]->aggregate;
1499}
1500
1501typedef void (*aggregate_func)(struct task_group *, struct sched_domain *);
1502
1503/*
1504 * Iterate the full tree, calling @down when first entering a node and @up when
1505 * leaving it for the final time.
1506 */
1507static
1508void aggregate_walk_tree(aggregate_func down, aggregate_func up,
1509 struct sched_domain *sd)
1510{
1511 struct task_group *parent, *child;
1512
1513 rcu_read_lock();
1514 parent = &root_task_group;
1515down:
1516 (*down)(parent, sd);
1517 list_for_each_entry_rcu(child, &parent->children, siblings) {
1518 parent = child;
1519 goto down;
1520
1521up:
1522 continue;
1523 }
1524 (*up)(parent, sd);
1525
1526 child = parent;
1527 parent = parent->parent;
1528 if (parent)
1529 goto up;
1530 rcu_read_unlock();
1531}
1532
1533/*
1534 * Calculate the aggregate runqueue weight.
1535 */
1536static
1537void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd)
1538{
1539 unsigned long rq_weight = 0;
1540 unsigned long task_weight = 0;
1541 int i;
1542
1543 for_each_cpu_mask(i, sd->span) {
1544 rq_weight += tg->cfs_rq[i]->load.weight;
1545 task_weight += tg->cfs_rq[i]->task_weight;
1546 }
1547
1548 aggregate(tg, sd)->rq_weight = rq_weight;
1549 aggregate(tg, sd)->task_weight = task_weight;
1550}
1551
1552/*
1553 * Redistribute tg->shares amongst all tg->cfs_rq[]s.
1554 */
1555static void __aggregate_redistribute_shares(struct task_group *tg)
1556{
1557 int i, max_cpu = smp_processor_id();
1558 unsigned long rq_weight = 0;
1559 unsigned long shares, max_shares = 0, shares_rem = tg->shares;
1560
1561 for_each_possible_cpu(i)
1562 rq_weight += tg->cfs_rq[i]->load.weight;
1563
1564 for_each_possible_cpu(i) {
1565 /*
1566 * divide shares proportional to the rq_weights.
1567 */
1568 shares = tg->shares * tg->cfs_rq[i]->load.weight;
1569 shares /= rq_weight + 1;
1570
1571 tg->cfs_rq[i]->shares = shares;
1572
1573 if (shares > max_shares) {
1574 max_shares = shares;
1575 max_cpu = i;
1576 }
1577 shares_rem -= shares;
1578 }
1579
1580 /*
1581 * Ensure it all adds up to tg->shares; we can loose a few
1582 * due to rounding down when computing the per-cpu shares.
1583 */
1584 if (shares_rem)
1585 tg->cfs_rq[max_cpu]->shares += shares_rem;
1586}
1587
1588/*
1589 * Compute the weight of this group on the given cpus.
1590 */
1591static
1592void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd)
1593{
1594 unsigned long shares = 0;
1595 int i;
1596
1597again:
1598 for_each_cpu_mask(i, sd->span)
1599 shares += tg->cfs_rq[i]->shares;
1600
1601 /*
1602 * When the span doesn't have any shares assigned, but does have
1603 * tasks to run do a machine wide rebalance (should be rare).
1604 */
1605 if (unlikely(!shares && aggregate(tg, sd)->rq_weight)) {
1606 __aggregate_redistribute_shares(tg);
1607 goto again;
1608 }
1609
1610 aggregate(tg, sd)->shares = shares;
1611}
1612
1613/*
1614 * Compute the load fraction assigned to this group, relies on the aggregate
1615 * weight and this group's parent's load, i.e. top-down.
1616 */
1617static
1618void aggregate_group_load(struct task_group *tg, struct sched_domain *sd)
1619{
1620 unsigned long load;
1621
1622 if (!tg->parent) {
1623 int i;
1624
1625 load = 0;
1626 for_each_cpu_mask(i, sd->span)
1627 load += cpu_rq(i)->load.weight;
1628
1629 } else {
1630 load = aggregate(tg->parent, sd)->load;
1631
1632 /*
1633 * shares is our weight in the parent's rq so
1634 * shares/parent->rq_weight gives our fraction of the load
1635 */
1636 load *= aggregate(tg, sd)->shares;
1637 load /= aggregate(tg->parent, sd)->rq_weight + 1;
1638 }
1639
1640 aggregate(tg, sd)->load = load;
1641}
1642
1643static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1644
1645/*
1646 * Calculate and set the cpu's group shares.
1647 */
1648static void
1649__update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,
1650 int tcpu)
1651{
1652 int boost = 0;
1653 unsigned long shares;
1654 unsigned long rq_weight;
1655
1656 if (!tg->se[tcpu])
1657 return;
1658
1659 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1660
1661 /*
1662 * If there are currently no tasks on the cpu pretend there is one of
1663 * average load so that when a new task gets to run here it will not
1664 * get delayed by group starvation.
1665 */
1666 if (!rq_weight) {
1667 boost = 1;
1668 rq_weight = NICE_0_LOAD;
1669 }
1670
1671 /*
1672 * \Sum shares * rq_weight
1673 * shares = -----------------------
1674 * \Sum rq_weight
1675 *
1676 */
1677 shares = aggregate(tg, sd)->shares * rq_weight;
1678 shares /= aggregate(tg, sd)->rq_weight + 1;
1679
1680 /*
1681 * record the actual number of shares, not the boosted amount.
1682 */
1683 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1684
1685 if (shares < MIN_SHARES)
1686 shares = MIN_SHARES;
1687
1688 __set_se_shares(tg->se[tcpu], shares);
1689}
1690
1691/*
1692 * Re-adjust the weights on the cpu the task came from and on the cpu the
1693 * task went to.
1694 */
1695static void
1696__move_group_shares(struct task_group *tg, struct sched_domain *sd,
1697 int scpu, int dcpu)
1698{
1699 unsigned long shares;
1700
1701 shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1702
1703 __update_group_shares_cpu(tg, sd, scpu);
1704 __update_group_shares_cpu(tg, sd, dcpu);
1705
1706 /*
1707 * ensure we never loose shares due to rounding errors in the
1708 * above redistribution.
1709 */
1710 shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1711 if (shares)
1712 tg->cfs_rq[dcpu]->shares += shares;
1713}
1714
1715/*
1716 * Because changing a group's shares changes the weight of the super-group
1717 * we need to walk up the tree and change all shares until we hit the root.
1718 */
1719static void
1720move_group_shares(struct task_group *tg, struct sched_domain *sd,
1721 int scpu, int dcpu)
1722{
1723 while (tg) {
1724 __move_group_shares(tg, sd, scpu, dcpu);
1725 tg = tg->parent;
1726 }
1727}
1728
1729static
1730void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd)
1731{
1732 unsigned long shares = aggregate(tg, sd)->shares;
1733 int i;
1734
1735 for_each_cpu_mask(i, sd->span) {
1736 struct rq *rq = cpu_rq(i);
1737 unsigned long flags;
1738
1739 spin_lock_irqsave(&rq->lock, flags);
1740 __update_group_shares_cpu(tg, sd, i);
1741 spin_unlock_irqrestore(&rq->lock, flags);
1742 }
1743
1744 aggregate_group_shares(tg, sd);
1745
1746 /*
1747 * ensure we never loose shares due to rounding errors in the
1748 * above redistribution.
1749 */
1750 shares -= aggregate(tg, sd)->shares;
1751 if (shares) {
1752 tg->cfs_rq[sd->first_cpu]->shares += shares;
1753 aggregate(tg, sd)->shares += shares;
1754 }
1755}
1756
1757/*
1758 * Calculate the accumulative weight and recursive load of each task group
1759 * while walking down the tree.
1760 */
1761static
1762void aggregate_get_down(struct task_group *tg, struct sched_domain *sd)
1763{
1764 aggregate_group_weight(tg, sd);
1765 aggregate_group_shares(tg, sd);
1766 aggregate_group_load(tg, sd);
1767}
1768
1769/*
1770 * Rebalance the cpu shares while walking back up the tree.
1771 */
1772static
1773void aggregate_get_up(struct task_group *tg, struct sched_domain *sd)
1774{
1775 aggregate_group_set_shares(tg, sd);
1776}
1777
1778static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1779
1780static void __init init_aggregate(void)
1781{
1782 int i;
1783
1784 for_each_possible_cpu(i)
1785 spin_lock_init(&per_cpu(aggregate_lock, i));
1786}
1787
1788static int get_aggregate(struct sched_domain *sd)
1789{
1790 if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu)))
1791 return 0;
1792
1793 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd);
1794 return 1;
1795}
1796
1797static void put_aggregate(struct sched_domain *sd)
1798{
1799 spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu));
1800}
1801
1802static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1803{
1804 cfs_rq->shares = shares;
1805}
1806
1807#else
1808
1809static inline void init_aggregate(void)
1810{
1811}
1812
1813static inline int get_aggregate(struct sched_domain *sd)
1814{
1815 return 0;
1816}
1817
1818static inline void put_aggregate(struct sched_domain *sd)
1819{
1820}
1821#endif
1822
1823#else /* CONFIG_SMP */
1824
1825#ifdef CONFIG_FAIR_GROUP_SCHED
1826static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1827{
1828}
1829#endif
1830
Gregory Haskinse7693a32008-01-25 21:08:09 +01001831#endif /* CONFIG_SMP */
1832
Ingo Molnardd41f592007-07-09 18:51:59 +02001833#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001834#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001835#include "sched_fair.c"
1836#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001837#ifdef CONFIG_SCHED_DEBUG
1838# include "sched_debug.c"
1839#endif
1840
1841#define sched_class_highest (&rt_sched_class)
1842
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001843static void inc_nr_running(struct rq *rq)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001844{
1845 rq->nr_running++;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001846}
1847
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001848static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001849{
1850 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001851}
1852
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001853static void set_load_weight(struct task_struct *p)
1854{
1855 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001856 p->se.load.weight = prio_to_weight[0] * 2;
1857 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1858 return;
1859 }
1860
1861 /*
1862 * SCHED_IDLE tasks get minimal weight:
1863 */
1864 if (p->policy == SCHED_IDLE) {
1865 p->se.load.weight = WEIGHT_IDLEPRIO;
1866 p->se.load.inv_weight = WMULT_IDLEPRIO;
1867 return;
1868 }
1869
1870 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1871 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001872}
1873
Ingo Molnar8159f872007-08-09 11:16:49 +02001874static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001875{
1876 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001877 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001878 p->se.on_rq = 1;
1879}
1880
Ingo Molnar69be72c2007-08-09 11:16:49 +02001881static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001882{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001883 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001884 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001885}
1886
1887/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001888 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001889 */
Ingo Molnar14531182007-07-09 18:51:59 +02001890static inline int __normal_prio(struct task_struct *p)
1891{
Ingo Molnardd41f592007-07-09 18:51:59 +02001892 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001893}
1894
1895/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001896 * Calculate the expected normal priority: i.e. priority
1897 * without taking RT-inheritance into account. Might be
1898 * boosted by interactivity modifiers. Changes upon fork,
1899 * setprio syscalls, and whenever the interactivity
1900 * estimator recalculates.
1901 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001902static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001903{
1904 int prio;
1905
Ingo Molnare05606d2007-07-09 18:51:59 +02001906 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001907 prio = MAX_RT_PRIO-1 - p->rt_priority;
1908 else
1909 prio = __normal_prio(p);
1910 return prio;
1911}
1912
1913/*
1914 * Calculate the current priority, i.e. the priority
1915 * taken into account by the scheduler. This value might
1916 * be boosted by RT tasks, or might be boosted by
1917 * interactivity modifiers. Will be RT if the task got
1918 * RT-boosted. If not then it returns p->normal_prio.
1919 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001920static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001921{
1922 p->normal_prio = normal_prio(p);
1923 /*
1924 * If we are RT tasks or we were boosted to RT priority,
1925 * keep the priority unchanged. Otherwise, update priority
1926 * to the normal priority:
1927 */
1928 if (!rt_prio(p->prio))
1929 return p->normal_prio;
1930 return p->prio;
1931}
1932
1933/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001934 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001936static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001938 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001939 rq->nr_uninterruptible--;
1940
Ingo Molnar8159f872007-08-09 11:16:49 +02001941 enqueue_task(rq, p, wakeup);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001942 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
1945/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 * deactivate_task - remove a task from the runqueue.
1947 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001948static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001950 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001951 rq->nr_uninterruptible++;
1952
Ingo Molnar69be72c2007-08-09 11:16:49 +02001953 dequeue_task(rq, p, sleep);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001954 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957/**
1958 * task_curr - is this task currently executing on a CPU?
1959 * @p: the task in question.
1960 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001961inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 return cpu_curr(task_cpu(p)) == p;
1964}
1965
Peter Williams2dd73a42006-06-27 02:54:34 -07001966/* Used instead of source_load when we know the type == 0 */
1967unsigned long weighted_cpuload(const int cpu)
1968{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001969 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001970}
1971
1972static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1973{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001974 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001975#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001976 /*
1977 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1978 * successfuly executed on another CPU. We must ensure that updates of
1979 * per-task data have been completed by this moment.
1980 */
1981 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001982 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001983#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001984}
1985
Steven Rostedtcb469842008-01-25 21:08:22 +01001986static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1987 const struct sched_class *prev_class,
1988 int oldprio, int running)
1989{
1990 if (prev_class != p->sched_class) {
1991 if (prev_class->switched_from)
1992 prev_class->switched_from(rq, p, running);
1993 p->sched_class->switched_to(rq, p, running);
1994 } else
1995 p->sched_class->prio_changed(rq, p, oldprio, running);
1996}
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001999
Ingo Molnarcc367732007-10-15 17:00:18 +02002000/*
2001 * Is this task likely cache-hot:
2002 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002003static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002004task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2005{
2006 s64 delta;
2007
Ingo Molnarf540a602008-03-15 17:10:34 +01002008 /*
2009 * Buddy candidates are cache hot:
2010 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01002011 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01002012 return 1;
2013
Ingo Molnarcc367732007-10-15 17:00:18 +02002014 if (p->sched_class != &fair_sched_class)
2015 return 0;
2016
Ingo Molnar6bc16652007-10-15 17:00:18 +02002017 if (sysctl_sched_migration_cost == -1)
2018 return 1;
2019 if (sysctl_sched_migration_cost == 0)
2020 return 0;
2021
Ingo Molnarcc367732007-10-15 17:00:18 +02002022 delta = now - p->se.exec_start;
2023
2024 return delta < (s64)sysctl_sched_migration_cost;
2025}
2026
2027
Ingo Molnardd41f592007-07-09 18:51:59 +02002028void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002029{
Ingo Molnardd41f592007-07-09 18:51:59 +02002030 int old_cpu = task_cpu(p);
2031 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002032 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
2033 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02002034 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002035
2036 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002037
2038#ifdef CONFIG_SCHEDSTATS
2039 if (p->se.wait_start)
2040 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002041 if (p->se.sleep_start)
2042 p->se.sleep_start -= clock_offset;
2043 if (p->se.block_start)
2044 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02002045 if (old_cpu != new_cpu) {
2046 schedstat_inc(p, se.nr_migrations);
2047 if (task_hot(p, old_rq->clock, NULL))
2048 schedstat_inc(p, se.nr_forced2_migrations);
2049 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002050#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002051 p->se.vruntime -= old_cfsrq->min_vruntime -
2052 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02002053
2054 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002055}
2056
Ingo Molnar70b97a72006-07-03 00:25:42 -07002057struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Ingo Molnar36c8b582006-07-03 00:25:41 -07002060 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 int dest_cpu;
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002064};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066/*
2067 * The task's runqueue lock must be held.
2068 * Returns true if you have to wait for migration thread.
2069 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002070static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002071migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002073 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 /*
2076 * If the task is not on a runqueue (and not running), then
2077 * it is sufficient to simply update the task's cpu field.
2078 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002079 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 set_task_cpu(p, dest_cpu);
2081 return 0;
2082 }
2083
2084 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 req->task = p;
2086 req->dest_cpu = dest_cpu;
2087 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return 1;
2090}
2091
2092/*
2093 * wait_task_inactive - wait for a thread to unschedule.
2094 *
2095 * The caller must ensure that the task *will* unschedule sometime soon,
2096 * else this function might spin for a *long* time. This function can't
2097 * be called with interrupts off, or it may introduce deadlock with
2098 * smp_call_function() if an IPI is sent by the same process we are
2099 * waiting to become inactive.
2100 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002101void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
2103 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002104 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002105 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Andi Kleen3a5c3592007-10-15 17:00:14 +02002107 for (;;) {
2108 /*
2109 * We do the initial early heuristics without holding
2110 * any task-queue locks at all. We'll only try to get
2111 * the runqueue lock when things look like they will
2112 * work out!
2113 */
2114 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002115
Andi Kleen3a5c3592007-10-15 17:00:14 +02002116 /*
2117 * If the task is actively running on another CPU
2118 * still, just relax and busy-wait without holding
2119 * any locks.
2120 *
2121 * NOTE! Since we don't hold any locks, it's not
2122 * even sure that "rq" stays as the right runqueue!
2123 * But we don't care, since "task_running()" will
2124 * return false if the runqueue has changed and p
2125 * is actually now running somewhere else!
2126 */
2127 while (task_running(rq, p))
2128 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002129
Andi Kleen3a5c3592007-10-15 17:00:14 +02002130 /*
2131 * Ok, time to look more closely! We need the rq
2132 * lock now, to be *sure*. If we're wrong, we'll
2133 * just go back and repeat.
2134 */
2135 rq = task_rq_lock(p, &flags);
2136 running = task_running(rq, p);
2137 on_rq = p->se.on_rq;
2138 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002139
Andi Kleen3a5c3592007-10-15 17:00:14 +02002140 /*
2141 * Was it really running after all now that we
2142 * checked with the proper locks actually held?
2143 *
2144 * Oops. Go back and try again..
2145 */
2146 if (unlikely(running)) {
2147 cpu_relax();
2148 continue;
2149 }
2150
2151 /*
2152 * It's not enough that it's not actively running,
2153 * it must be off the runqueue _entirely_, and not
2154 * preempted!
2155 *
2156 * So if it wa still runnable (but just not actively
2157 * running right now), it's preempted, and we should
2158 * yield - it could be a while.
2159 */
2160 if (unlikely(on_rq)) {
2161 schedule_timeout_uninterruptible(1);
2162 continue;
2163 }
2164
2165 /*
2166 * Ahh, all good. It wasn't running, and it wasn't
2167 * runnable, which means that it will never become
2168 * running in the future either. We're all done!
2169 */
2170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
2174/***
2175 * kick_process - kick a running thread to enter/exit the kernel
2176 * @p: the to-be-kicked thread
2177 *
2178 * Cause a process which is running on another CPU to enter
2179 * kernel-mode, without any delay. (to get signals handled.)
2180 *
2181 * NOTE: this function doesnt have to take the runqueue lock,
2182 * because all it wants to ensure is that the remote task enters
2183 * the kernel. If the IPI races and the task has been migrated
2184 * to another CPU then no harm is done and the purpose has been
2185 * achieved as well.
2186 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002187void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
2189 int cpu;
2190
2191 preempt_disable();
2192 cpu = task_cpu(p);
2193 if ((cpu != smp_processor_id()) && task_curr(p))
2194 smp_send_reschedule(cpu);
2195 preempt_enable();
2196}
2197
2198/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002199 * Return a low guess at the load of a migration-source cpu weighted
2200 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 *
2202 * We want to under-estimate the load of migration sources, to
2203 * balance conservatively.
2204 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002205static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002206{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002207 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002208 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002209
Peter Williams2dd73a42006-06-27 02:54:34 -07002210 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002211 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002212
Ingo Molnardd41f592007-07-09 18:51:59 +02002213 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
2216/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002217 * Return a high guess at the load of a migration-target cpu weighted
2218 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002220static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002221{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002222 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002223 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002224
Peter Williams2dd73a42006-06-27 02:54:34 -07002225 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002226 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002227
Ingo Molnardd41f592007-07-09 18:51:59 +02002228 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002229}
2230
2231/*
2232 * Return the average load per task on the cpu's run queue
2233 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002234static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07002235{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002236 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002237 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07002238 unsigned long n = rq->nr_running;
2239
Ingo Molnardd41f592007-07-09 18:51:59 +02002240 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
Nick Piggin147cbb42005-06-25 14:57:19 -07002243/*
2244 * find_idlest_group finds and returns the least busy CPU group within the
2245 * domain.
2246 */
2247static struct sched_group *
2248find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2249{
2250 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2251 unsigned long min_load = ULONG_MAX, this_load = 0;
2252 int load_idx = sd->forkexec_idx;
2253 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2254
2255 do {
2256 unsigned long load, avg_load;
2257 int local_group;
2258 int i;
2259
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002260 /* Skip over this group if it has no CPUs allowed */
2261 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002262 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002263
Nick Piggin147cbb42005-06-25 14:57:19 -07002264 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002265
2266 /* Tally up the load of all CPUs in the group */
2267 avg_load = 0;
2268
2269 for_each_cpu_mask(i, group->cpumask) {
2270 /* Bias balancing toward cpus of our domain */
2271 if (local_group)
2272 load = source_load(i, load_idx);
2273 else
2274 load = target_load(i, load_idx);
2275
2276 avg_load += load;
2277 }
2278
2279 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002280 avg_load = sg_div_cpu_power(group,
2281 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002282
2283 if (local_group) {
2284 this_load = avg_load;
2285 this = group;
2286 } else if (avg_load < min_load) {
2287 min_load = avg_load;
2288 idlest = group;
2289 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002290 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002291
2292 if (!idlest || 100*this_load < imbalance*min_load)
2293 return NULL;
2294 return idlest;
2295}
2296
2297/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002298 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002299 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002300static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002301find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2302 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002303{
2304 unsigned long load, min_load = ULONG_MAX;
2305 int idlest = -1;
2306 int i;
2307
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002308 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002309 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002310
Mike Travis7c16ec52008-04-04 18:11:11 -07002311 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002312 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002313
2314 if (load < min_load || (load == min_load && i == this_cpu)) {
2315 min_load = load;
2316 idlest = i;
2317 }
2318 }
2319
2320 return idlest;
2321}
2322
Nick Piggin476d1392005-06-25 14:57:29 -07002323/*
2324 * sched_balance_self: balance the current task (running on cpu) in domains
2325 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2326 * SD_BALANCE_EXEC.
2327 *
2328 * Balance, ie. select the least loaded group.
2329 *
2330 * Returns the target CPU number, or the same CPU if no balancing is needed.
2331 *
2332 * preempt must be disabled.
2333 */
2334static int sched_balance_self(int cpu, int flag)
2335{
2336 struct task_struct *t = current;
2337 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002338
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002339 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002340 /*
2341 * If power savings logic is enabled for a domain, stop there.
2342 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002343 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2344 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002345 if (tmp->flags & flag)
2346 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002347 }
Nick Piggin476d1392005-06-25 14:57:29 -07002348
2349 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002350 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002351 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002352 int new_cpu, weight;
2353
2354 if (!(sd->flags & flag)) {
2355 sd = sd->child;
2356 continue;
2357 }
Nick Piggin476d1392005-06-25 14:57:29 -07002358
2359 span = sd->span;
2360 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002361 if (!group) {
2362 sd = sd->child;
2363 continue;
2364 }
Nick Piggin476d1392005-06-25 14:57:29 -07002365
Mike Travis7c16ec52008-04-04 18:11:11 -07002366 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002367 if (new_cpu == -1 || new_cpu == cpu) {
2368 /* Now try balancing at a lower domain level of cpu */
2369 sd = sd->child;
2370 continue;
2371 }
Nick Piggin476d1392005-06-25 14:57:29 -07002372
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002373 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002374 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002375 sd = NULL;
2376 weight = cpus_weight(span);
2377 for_each_domain(cpu, tmp) {
2378 if (weight <= cpus_weight(tmp->span))
2379 break;
2380 if (tmp->flags & flag)
2381 sd = tmp;
2382 }
2383 /* while loop will break here if sd == NULL */
2384 }
2385
2386 return cpu;
2387}
2388
2389#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391/***
2392 * try_to_wake_up - wake up a thread
2393 * @p: the to-be-woken-up thread
2394 * @state: the mask of task states that can be woken
2395 * @sync: do a synchronous wakeup?
2396 *
2397 * Put it on the run-queue if it's not already there. The "current"
2398 * thread is always on the run-queue (except when the actual
2399 * re-schedule is in progress), and as such you're allowed to do
2400 * the simpler "current->state = TASK_RUNNING" to mark yourself
2401 * runnable without the overhead of this.
2402 *
2403 * returns failure only if the task is already active.
2404 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002405static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Ingo Molnarcc367732007-10-15 17:00:18 +02002407 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 unsigned long flags;
2409 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002410 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Ingo Molnarb85d0662008-03-16 20:03:22 +01002412 if (!sched_feat(SYNC_WAKEUPS))
2413 sync = 0;
2414
Linus Torvalds04e2f172008-02-23 18:05:03 -08002415 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 rq = task_rq_lock(p, &flags);
2417 old_state = p->state;
2418 if (!(old_state & state))
2419 goto out;
2420
Ingo Molnardd41f592007-07-09 18:51:59 +02002421 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 goto out_running;
2423
2424 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002425 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 this_cpu = smp_processor_id();
2427
2428#ifdef CONFIG_SMP
2429 if (unlikely(task_running(rq, p)))
2430 goto out_activate;
2431
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002432 cpu = p->sched_class->select_task_rq(p, sync);
2433 if (cpu != orig_cpu) {
2434 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 task_rq_unlock(rq, &flags);
2436 /* might preempt at this point */
2437 rq = task_rq_lock(p, &flags);
2438 old_state = p->state;
2439 if (!(old_state & state))
2440 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002441 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 goto out_running;
2443
2444 this_cpu = smp_processor_id();
2445 cpu = task_cpu(p);
2446 }
2447
Gregory Haskinse7693a32008-01-25 21:08:09 +01002448#ifdef CONFIG_SCHEDSTATS
2449 schedstat_inc(rq, ttwu_count);
2450 if (cpu == this_cpu)
2451 schedstat_inc(rq, ttwu_local);
2452 else {
2453 struct sched_domain *sd;
2454 for_each_domain(this_cpu, sd) {
2455 if (cpu_isset(cpu, sd->span)) {
2456 schedstat_inc(sd, ttwu_wake_remote);
2457 break;
2458 }
2459 }
2460 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002461#endif
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463out_activate:
2464#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002465 schedstat_inc(p, se.nr_wakeups);
2466 if (sync)
2467 schedstat_inc(p, se.nr_wakeups_sync);
2468 if (orig_cpu != cpu)
2469 schedstat_inc(p, se.nr_wakeups_migrate);
2470 if (cpu == this_cpu)
2471 schedstat_inc(p, se.nr_wakeups_local);
2472 else
2473 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002474 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002475 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 success = 1;
2477
2478out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002479 check_preempt_curr(rq, p);
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002482#ifdef CONFIG_SMP
2483 if (p->sched_class->task_wake_up)
2484 p->sched_class->task_wake_up(rq, p);
2485#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486out:
2487 task_rq_unlock(rq, &flags);
2488
2489 return success;
2490}
2491
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002492int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002494 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496EXPORT_SYMBOL(wake_up_process);
2497
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002498int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
2500 return try_to_wake_up(p, state, 0);
2501}
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503/*
2504 * Perform scheduler related setup for a newly forked process p.
2505 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002506 *
2507 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002509static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
Ingo Molnardd41f592007-07-09 18:51:59 +02002511 p->se.exec_start = 0;
2512 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002513 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002514 p->se.last_wakeup = 0;
2515 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002516
2517#ifdef CONFIG_SCHEDSTATS
2518 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002519 p->se.sum_sleep_runtime = 0;
2520 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002521 p->se.block_start = 0;
2522 p->se.sleep_max = 0;
2523 p->se.block_max = 0;
2524 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002525 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002526 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002527#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002528
Peter Zijlstrafa717062008-01-25 21:08:27 +01002529 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002530 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002531 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002532
Avi Kivitye107be32007-07-26 13:40:43 +02002533#ifdef CONFIG_PREEMPT_NOTIFIERS
2534 INIT_HLIST_HEAD(&p->preempt_notifiers);
2535#endif
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 /*
2538 * We mark the process as running here, but have not actually
2539 * inserted it onto the runqueue yet. This guarantees that
2540 * nobody will actually run it, and a signal or other external
2541 * event cannot wake it up and insert it on the runqueue either.
2542 */
2543 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002544}
2545
2546/*
2547 * fork()/clone()-time setup:
2548 */
2549void sched_fork(struct task_struct *p, int clone_flags)
2550{
2551 int cpu = get_cpu();
2552
2553 __sched_fork(p);
2554
2555#ifdef CONFIG_SMP
2556 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2557#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002558 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002559
2560 /*
2561 * Make sure we do not leak PI boosting priority to the child:
2562 */
2563 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002564 if (!rt_prio(p->prio))
2565 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002566
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002567#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002568 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002569 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002571#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002572 p->oncpu = 0;
2573#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002575 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002576 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002578 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
2581/*
2582 * wake_up_new_task - wake up a newly created task for the first time.
2583 *
2584 * This function will do some initial scheduler statistics housekeeping
2585 * that must be done for every newly created context, then puts the task
2586 * on the runqueue and wakes it.
2587 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002588void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
2590 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002591 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002595 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 p->prio = effective_prio(p);
2598
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002599 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002600 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002603 * Let the scheduling class do new task startup
2604 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002606 p->sched_class->task_new(rq, p);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002607 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002609 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002610#ifdef CONFIG_SMP
2611 if (p->sched_class->task_wake_up)
2612 p->sched_class->task_wake_up(rq, p);
2613#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002614 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
Avi Kivitye107be32007-07-26 13:40:43 +02002617#ifdef CONFIG_PREEMPT_NOTIFIERS
2618
2619/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002620 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2621 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002622 */
2623void preempt_notifier_register(struct preempt_notifier *notifier)
2624{
2625 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2626}
2627EXPORT_SYMBOL_GPL(preempt_notifier_register);
2628
2629/**
2630 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002631 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002632 *
2633 * This is safe to call from within a preemption notifier.
2634 */
2635void preempt_notifier_unregister(struct preempt_notifier *notifier)
2636{
2637 hlist_del(&notifier->link);
2638}
2639EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2640
2641static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2642{
2643 struct preempt_notifier *notifier;
2644 struct hlist_node *node;
2645
2646 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2647 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2648}
2649
2650static void
2651fire_sched_out_preempt_notifiers(struct task_struct *curr,
2652 struct task_struct *next)
2653{
2654 struct preempt_notifier *notifier;
2655 struct hlist_node *node;
2656
2657 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2658 notifier->ops->sched_out(notifier, next);
2659}
2660
2661#else
2662
2663static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2664{
2665}
2666
2667static void
2668fire_sched_out_preempt_notifiers(struct task_struct *curr,
2669 struct task_struct *next)
2670{
2671}
2672
2673#endif
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002676 * prepare_task_switch - prepare to switch tasks
2677 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002678 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002679 * @next: the task we are going to switch to.
2680 *
2681 * This is called with the rq lock held and interrupts off. It must
2682 * be paired with a subsequent finish_task_switch after the context
2683 * switch.
2684 *
2685 * prepare_task_switch sets up locking and calls architecture specific
2686 * hooks.
2687 */
Avi Kivitye107be32007-07-26 13:40:43 +02002688static inline void
2689prepare_task_switch(struct rq *rq, struct task_struct *prev,
2690 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002691{
Avi Kivitye107be32007-07-26 13:40:43 +02002692 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002693 prepare_lock_switch(rq, next);
2694 prepare_arch_switch(next);
2695}
2696
2697/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002699 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 * @prev: the thread we just switched away from.
2701 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002702 * finish_task_switch must be called after the context switch, paired
2703 * with a prepare_task_switch call before the context switch.
2704 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2705 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 *
2707 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002708 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 * with the lock held can cause deadlocks; see schedule() for
2710 * details.)
2711 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002712static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 __releases(rq->lock)
2714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002716 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 rq->prev_mm = NULL;
2719
2720 /*
2721 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002722 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002723 * schedule one last time. The schedule call will never return, and
2724 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002725 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 * still held, otherwise prev could be scheduled on another cpu, die
2727 * there before we look at prev->state, and then the reference would
2728 * be dropped twice.
2729 * Manfred Spraul <manfred@colorfullife.com>
2730 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002731 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002732 finish_arch_switch(prev);
2733 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002734#ifdef CONFIG_SMP
2735 if (current->sched_class->post_schedule)
2736 current->sched_class->post_schedule(rq);
2737#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002738
Avi Kivitye107be32007-07-26 13:40:43 +02002739 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 if (mm)
2741 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002742 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002743 /*
2744 * Remove function-return probe instances associated with this
2745 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002746 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002747 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
2752/**
2753 * schedule_tail - first thing a freshly forked thread must call.
2754 * @prev: the thread we just switched away from.
2755 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002756asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 __releases(rq->lock)
2758{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002759 struct rq *rq = this_rq();
2760
Nick Piggin4866cde2005-06-25 14:57:23 -07002761 finish_task_switch(rq, prev);
2762#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2763 /* In this case, finish_task_switch does not reenable preemption */
2764 preempt_enable();
2765#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002767 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
2770/*
2771 * context_switch - switch to the new MM and the new
2772 * thread's register state.
2773 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002774static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002775context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002776 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Ingo Molnardd41f592007-07-09 18:51:59 +02002778 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Avi Kivitye107be32007-07-26 13:40:43 +02002780 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002781 mm = next->mm;
2782 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002783 /*
2784 * For paravirt, this is coupled with an exit in switch_to to
2785 * combine the page table reload and the switch backend into
2786 * one hypercall.
2787 */
2788 arch_enter_lazy_cpu_mode();
2789
Ingo Molnardd41f592007-07-09 18:51:59 +02002790 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 next->active_mm = oldmm;
2792 atomic_inc(&oldmm->mm_count);
2793 enter_lazy_tlb(oldmm, next);
2794 } else
2795 switch_mm(oldmm, mm, next);
2796
Ingo Molnardd41f592007-07-09 18:51:59 +02002797 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 rq->prev_mm = oldmm;
2800 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002801 /*
2802 * Since the runqueue lock will be released by the next
2803 * task (which is an invalid locking op but in the case
2804 * of the scheduler it's an obvious special-case), so we
2805 * do an early lockdep release here:
2806 */
2807#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002808 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002809#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 /* Here we just switch the register state and the stack. */
2812 switch_to(prev, next, prev);
2813
Ingo Molnardd41f592007-07-09 18:51:59 +02002814 barrier();
2815 /*
2816 * this_rq must be evaluated again because prev may have moved
2817 * CPUs since it called schedule(), thus the 'rq' on its stack
2818 * frame will be invalid.
2819 */
2820 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822
2823/*
2824 * nr_running, nr_uninterruptible and nr_context_switches:
2825 *
2826 * externally visible scheduler statistics: current number of runnable
2827 * threads, current number of uninterruptible-sleeping threads, total
2828 * number of context switches performed since bootup.
2829 */
2830unsigned long nr_running(void)
2831{
2832 unsigned long i, sum = 0;
2833
2834 for_each_online_cpu(i)
2835 sum += cpu_rq(i)->nr_running;
2836
2837 return sum;
2838}
2839
2840unsigned long nr_uninterruptible(void)
2841{
2842 unsigned long i, sum = 0;
2843
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002844 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 sum += cpu_rq(i)->nr_uninterruptible;
2846
2847 /*
2848 * Since we read the counters lockless, it might be slightly
2849 * inaccurate. Do not allow it to go below zero though:
2850 */
2851 if (unlikely((long)sum < 0))
2852 sum = 0;
2853
2854 return sum;
2855}
2856
2857unsigned long long nr_context_switches(void)
2858{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002859 int i;
2860 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002862 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 sum += cpu_rq(i)->nr_switches;
2864
2865 return sum;
2866}
2867
2868unsigned long nr_iowait(void)
2869{
2870 unsigned long i, sum = 0;
2871
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002872 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2874
2875 return sum;
2876}
2877
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002878unsigned long nr_active(void)
2879{
2880 unsigned long i, running = 0, uninterruptible = 0;
2881
2882 for_each_online_cpu(i) {
2883 running += cpu_rq(i)->nr_running;
2884 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2885 }
2886
2887 if (unlikely((long)uninterruptible < 0))
2888 uninterruptible = 0;
2889
2890 return running + uninterruptible;
2891}
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002894 * Update rq->cpu_load[] statistics. This function is usually called every
2895 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002896 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002897static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002898{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002899 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002900 int i, scale;
2901
2902 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002903
2904 /* Update our load: */
2905 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2906 unsigned long old_load, new_load;
2907
2908 /* scale is effectively 1 << i now, and >> i divides by scale */
2909
2910 old_load = this_rq->cpu_load[i];
2911 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002912 /*
2913 * Round up the averaging division if load is increasing. This
2914 * prevents us from getting stuck on 9 if the load is 10, for
2915 * example.
2916 */
2917 if (new_load > old_load)
2918 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002919 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2920 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002921}
2922
Ingo Molnardd41f592007-07-09 18:51:59 +02002923#ifdef CONFIG_SMP
2924
Ingo Molnar48f24c42006-07-03 00:25:40 -07002925/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 * double_rq_lock - safely lock two runqueues
2927 *
2928 * Note this does not disable interrupts like task_rq_lock,
2929 * you need to do so manually before calling.
2930 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002931static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 __acquires(rq1->lock)
2933 __acquires(rq2->lock)
2934{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002935 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (rq1 == rq2) {
2937 spin_lock(&rq1->lock);
2938 __acquire(rq2->lock); /* Fake it out ;) */
2939 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002940 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 spin_lock(&rq1->lock);
2942 spin_lock(&rq2->lock);
2943 } else {
2944 spin_lock(&rq2->lock);
2945 spin_lock(&rq1->lock);
2946 }
2947 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002948 update_rq_clock(rq1);
2949 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951
2952/*
2953 * double_rq_unlock - safely unlock two runqueues
2954 *
2955 * Note this does not restore interrupts like task_rq_unlock,
2956 * you need to do so manually after calling.
2957 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002958static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 __releases(rq1->lock)
2960 __releases(rq2->lock)
2961{
2962 spin_unlock(&rq1->lock);
2963 if (rq1 != rq2)
2964 spin_unlock(&rq2->lock);
2965 else
2966 __release(rq2->lock);
2967}
2968
2969/*
2970 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2971 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002972static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 __releases(this_rq->lock)
2974 __acquires(busiest->lock)
2975 __acquires(this_rq->lock)
2976{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002977 int ret = 0;
2978
Kirill Korotaev054b9102006-12-10 02:20:11 -08002979 if (unlikely(!irqs_disabled())) {
2980 /* printk() doesn't work good under rq->lock */
2981 spin_unlock(&this_rq->lock);
2982 BUG_ON(1);
2983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002985 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 spin_unlock(&this_rq->lock);
2987 spin_lock(&busiest->lock);
2988 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002989 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 } else
2991 spin_lock(&busiest->lock);
2992 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002993 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994}
2995
2996/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 * If dest_cpu is allowed for this process, migrate the task to it.
2998 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002999 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 * the cpu_allowed mask is restored.
3001 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003002static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003004 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003006 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
3008 rq = task_rq_lock(p, &flags);
3009 if (!cpu_isset(dest_cpu, p->cpus_allowed)
3010 || unlikely(cpu_is_offline(dest_cpu)))
3011 goto out;
3012
3013 /* force the process onto the specified CPU */
3014 if (migrate_task(p, dest_cpu, &req)) {
3015 /* Need to wait for migration thread (might exit: take ref). */
3016 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 get_task_struct(mt);
3019 task_rq_unlock(rq, &flags);
3020 wake_up_process(mt);
3021 put_task_struct(mt);
3022 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003023
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return;
3025 }
3026out:
3027 task_rq_unlock(rq, &flags);
3028}
3029
3030/*
Nick Piggin476d1392005-06-25 14:57:29 -07003031 * sched_exec - execve() is a valuable balancing opportunity, because at
3032 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 */
3034void sched_exec(void)
3035{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003037 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003039 if (new_cpu != this_cpu)
3040 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
3043/*
3044 * pull_task - move a task from a remote runqueue to the local runqueue.
3045 * Both runqueues must be locked.
3046 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003047static void pull_task(struct rq *src_rq, struct task_struct *p,
3048 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003050 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003052 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 /*
3054 * Note that idle threads have a prio of MAX_PRIO, for this test
3055 * to be always true for them.
3056 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003057 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058}
3059
3060/*
3061 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3062 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003063static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003064int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003065 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003066 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
3068 /*
3069 * We do not migrate tasks that are:
3070 * 1) running (obviously), or
3071 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3072 * 3) are cache-hot on their current CPU.
3073 */
Ingo Molnarcc367732007-10-15 17:00:18 +02003074 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3075 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003077 }
Nick Piggin81026792005-06-25 14:57:07 -07003078 *all_pinned = 0;
3079
Ingo Molnarcc367732007-10-15 17:00:18 +02003080 if (task_running(rq, p)) {
3081 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003082 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Ingo Molnarda84d962007-10-15 17:00:18 +02003085 /*
3086 * Aggressive migration if:
3087 * 1) task is cache cold, or
3088 * 2) too many balance attempts have failed.
3089 */
3090
Ingo Molnar6bc16652007-10-15 17:00:18 +02003091 if (!task_hot(p, rq->clock, sd) ||
3092 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003093#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02003094 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003095 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003096 schedstat_inc(p, se.nr_forced_migrations);
3097 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003098#endif
3099 return 1;
3100 }
3101
Ingo Molnarcc367732007-10-15 17:00:18 +02003102 if (task_hot(p, rq->clock, sd)) {
3103 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003104 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return 1;
3107}
3108
Peter Williamse1d14842007-10-24 18:23:51 +02003109static unsigned long
3110balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3111 unsigned long max_load_move, struct sched_domain *sd,
3112 enum cpu_idle_type idle, int *all_pinned,
3113 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003114{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003115 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02003116 struct task_struct *p;
3117 long rem_load_move = max_load_move;
3118
Peter Williamse1d14842007-10-24 18:23:51 +02003119 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003120 goto out;
3121
3122 pinned = 1;
3123
3124 /*
3125 * Start the load-balancing iterator:
3126 */
3127 p = iterator->start(iterator->arg);
3128next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003129 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003130 goto out;
3131 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003132 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02003133 * skip a task if it will be the highest priority task (i.e. smallest
3134 * prio value) on its new queue regardless of its load weight
3135 */
3136 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
3137 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003138 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003139 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003140 p = iterator->next(iterator->arg);
3141 goto next;
3142 }
3143
3144 pull_task(busiest, p, this_rq, this_cpu);
3145 pulled++;
3146 rem_load_move -= p->se.load.weight;
3147
3148 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003149 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003150 */
Peter Williamse1d14842007-10-24 18:23:51 +02003151 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003152 if (p->prio < *this_best_prio)
3153 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003154 p = iterator->next(iterator->arg);
3155 goto next;
3156 }
3157out:
3158 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003159 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003160 * so we can safely collect pull_task() stats here rather than
3161 * inside pull_task().
3162 */
3163 schedstat_add(sd, lb_gained[idle], pulled);
3164
3165 if (all_pinned)
3166 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003167
3168 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003169}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171/*
Peter Williams43010652007-08-09 11:16:46 +02003172 * move_tasks tries to move up to max_load_move weighted load from busiest to
3173 * this_rq, as part of a balancing operation within domain "sd".
3174 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 *
3176 * Called with both runqueues locked.
3177 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003178static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003179 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003180 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003181 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003183 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003184 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003185 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Ingo Molnardd41f592007-07-09 18:51:59 +02003187 do {
Peter Williams43010652007-08-09 11:16:46 +02003188 total_load_moved +=
3189 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003190 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003191 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003192 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003193 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Peter Williams43010652007-08-09 11:16:46 +02003195 return total_load_moved > 0;
3196}
3197
Peter Williamse1d14842007-10-24 18:23:51 +02003198static int
3199iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3200 struct sched_domain *sd, enum cpu_idle_type idle,
3201 struct rq_iterator *iterator)
3202{
3203 struct task_struct *p = iterator->start(iterator->arg);
3204 int pinned = 0;
3205
3206 while (p) {
3207 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3208 pull_task(busiest, p, this_rq, this_cpu);
3209 /*
3210 * Right now, this is only the second place pull_task()
3211 * is called, so we can safely collect pull_task()
3212 * stats here rather than inside pull_task().
3213 */
3214 schedstat_inc(sd, lb_gained[idle]);
3215
3216 return 1;
3217 }
3218 p = iterator->next(iterator->arg);
3219 }
3220
3221 return 0;
3222}
3223
Peter Williams43010652007-08-09 11:16:46 +02003224/*
3225 * move_one_task tries to move exactly one task from busiest to this_rq, as
3226 * part of active balancing operations within "domain".
3227 * Returns 1 if successful and 0 otherwise.
3228 *
3229 * Called with both runqueues locked.
3230 */
3231static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3232 struct sched_domain *sd, enum cpu_idle_type idle)
3233{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003234 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003235
3236 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003237 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003238 return 1;
3239
3240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
3243/*
3244 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003245 * domain. It calculates and returns the amount of weighted load which
3246 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 */
3248static struct sched_group *
3249find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003250 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003251 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
3253 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3254 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003255 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003256 unsigned long busiest_load_per_task, busiest_nr_running;
3257 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003258 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003259#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3260 int power_savings_balance = 1;
3261 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3262 unsigned long min_nr_running = ULONG_MAX;
3263 struct sched_group *group_min = NULL, *group_leader = NULL;
3264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003267 busiest_load_per_task = busiest_nr_running = 0;
3268 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003269 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003270 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003271 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003272 load_idx = sd->newidle_idx;
3273 else
3274 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
3276 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003277 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 int local_group;
3279 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003280 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003281 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003282 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284 local_group = cpu_isset(this_cpu, group->cpumask);
3285
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003286 if (local_group)
3287 balance_cpu = first_cpu(group->cpumask);
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003290 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02003291 max_cpu_load = 0;
3292 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003295 struct rq *rq;
3296
3297 if (!cpu_isset(i, *cpus))
3298 continue;
3299
3300 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003301
Suresh Siddha9439aab2007-07-19 21:28:35 +02003302 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003303 *sd_idle = 0;
3304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003306 if (local_group) {
3307 if (idle_cpu(i) && !first_idle_cpu) {
3308 first_idle_cpu = 1;
3309 balance_cpu = i;
3310 }
3311
Nick Piggina2000572006-02-10 01:51:02 -08003312 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003313 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003314 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003315 if (load > max_cpu_load)
3316 max_cpu_load = load;
3317 if (min_cpu_load > load)
3318 min_cpu_load = load;
3319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
3321 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003322 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003323 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
3325
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003326 /*
3327 * First idle cpu or the first cpu(busiest) in this sched group
3328 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003329 * domains. In the newly idle case, we will allow all the cpu's
3330 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003331 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003332 if (idle != CPU_NEWLY_IDLE && local_group &&
3333 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003334 *balance = 0;
3335 goto ret;
3336 }
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003339 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
3341 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003342 avg_load = sg_div_cpu_power(group,
3343 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Ken Chen908a7c12007-10-17 16:55:11 +02003345 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3346 __group_imb = 1;
3347
Eric Dumazet5517d862007-05-08 00:32:57 -07003348 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 if (local_group) {
3351 this_load = avg_load;
3352 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003353 this_nr_running = sum_nr_running;
3354 this_load_per_task = sum_weighted_load;
3355 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003356 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 max_load = avg_load;
3358 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003359 busiest_nr_running = sum_nr_running;
3360 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003361 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003363
3364#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3365 /*
3366 * Busy processors will not participate in power savings
3367 * balance.
3368 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003369 if (idle == CPU_NOT_IDLE ||
3370 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3371 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003372
3373 /*
3374 * If the local group is idle or completely loaded
3375 * no need to do power savings balance at this domain
3376 */
3377 if (local_group && (this_nr_running >= group_capacity ||
3378 !this_nr_running))
3379 power_savings_balance = 0;
3380
Ingo Molnardd41f592007-07-09 18:51:59 +02003381 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003382 * If a group is already running at full capacity or idle,
3383 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003384 */
3385 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003386 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003387 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003388
Ingo Molnardd41f592007-07-09 18:51:59 +02003389 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003390 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003391 * This is the group from where we need to pick up the load
3392 * for saving power
3393 */
3394 if ((sum_nr_running < min_nr_running) ||
3395 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003396 first_cpu(group->cpumask) <
3397 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003398 group_min = group;
3399 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003400 min_load_per_task = sum_weighted_load /
3401 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003402 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003403
Ingo Molnardd41f592007-07-09 18:51:59 +02003404 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003405 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003406 * capacity but still has some space to pick up some load
3407 * from other group and save more power
3408 */
3409 if (sum_nr_running <= group_capacity - 1) {
3410 if (sum_nr_running > leader_nr_running ||
3411 (sum_nr_running == leader_nr_running &&
3412 first_cpu(group->cpumask) >
3413 first_cpu(group_leader->cpumask))) {
3414 group_leader = group;
3415 leader_nr_running = sum_nr_running;
3416 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003417 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003418group_next:
3419#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 group = group->next;
3421 } while (group != sd->groups);
3422
Peter Williams2dd73a42006-06-27 02:54:34 -07003423 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 goto out_balanced;
3425
3426 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3427
3428 if (this_load >= avg_load ||
3429 100*max_load <= sd->imbalance_pct*this_load)
3430 goto out_balanced;
3431
Peter Williams2dd73a42006-06-27 02:54:34 -07003432 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003433 if (group_imb)
3434 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 /*
3437 * We're trying to get all the cpus to the average_load, so we don't
3438 * want to push ourselves above the average load, nor do we wish to
3439 * reduce the max loaded cpu below the average load, as either of these
3440 * actions would just result in more rebalancing later, and ping-pong
3441 * tasks around. Thus we look for the minimum possible imbalance.
3442 * Negative imbalances (*we* are more loaded than anyone else) will
3443 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003444 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 * appear as very large values with unsigned longs.
3446 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003447 if (max_load <= busiest_load_per_task)
3448 goto out_balanced;
3449
3450 /*
3451 * In the presence of smp nice balancing, certain scenarios can have
3452 * max load less than avg load(as we skip the groups at or below
3453 * its cpu_power, while calculating max_load..)
3454 */
3455 if (max_load < avg_load) {
3456 *imbalance = 0;
3457 goto small_imbalance;
3458 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003459
3460 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003461 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003464 *imbalance = min(max_pull * busiest->__cpu_power,
3465 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 / SCHED_LOAD_SCALE;
3467
Peter Williams2dd73a42006-06-27 02:54:34 -07003468 /*
3469 * if *imbalance is less than the average load per runnable task
3470 * there is no gaurantee that any tasks will be moved so we'll have
3471 * a think about bumping its value to force at least one task to be
3472 * moved
3473 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003474 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003475 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003476 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
Peter Williams2dd73a42006-06-27 02:54:34 -07003478small_imbalance:
3479 pwr_move = pwr_now = 0;
3480 imbn = 2;
3481 if (this_nr_running) {
3482 this_load_per_task /= this_nr_running;
3483 if (busiest_load_per_task > this_load_per_task)
3484 imbn = 1;
3485 } else
3486 this_load_per_task = SCHED_LOAD_SCALE;
3487
Ingo Molnardd41f592007-07-09 18:51:59 +02003488 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3489 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003490 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 return busiest;
3492 }
3493
3494 /*
3495 * OK, we don't have enough imbalance to justify moving tasks,
3496 * however we may be able to increase total CPU power used by
3497 * moving them.
3498 */
3499
Eric Dumazet5517d862007-05-08 00:32:57 -07003500 pwr_now += busiest->__cpu_power *
3501 min(busiest_load_per_task, max_load);
3502 pwr_now += this->__cpu_power *
3503 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 pwr_now /= SCHED_LOAD_SCALE;
3505
3506 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003507 tmp = sg_div_cpu_power(busiest,
3508 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003510 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003511 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
3513 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003514 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003515 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003516 tmp = sg_div_cpu_power(this,
3517 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003519 tmp = sg_div_cpu_power(this,
3520 busiest_load_per_task * SCHED_LOAD_SCALE);
3521 pwr_move += this->__cpu_power *
3522 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 pwr_move /= SCHED_LOAD_SCALE;
3524
3525 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003526 if (pwr_move > pwr_now)
3527 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 }
3529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return busiest;
3531
3532out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003533#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003534 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003535 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003537 if (this == group_leader && group_leader != group_min) {
3538 *imbalance = min_load_per_task;
3539 return group_min;
3540 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003541#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003542ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 *imbalance = 0;
3544 return NULL;
3545}
3546
3547/*
3548 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3549 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003550static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003551find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003552 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003554 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003555 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 int i;
3557
3558 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003559 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003560
3561 if (!cpu_isset(i, *cpus))
3562 continue;
3563
Ingo Molnar48f24c42006-07-03 00:25:40 -07003564 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003565 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Ingo Molnardd41f592007-07-09 18:51:59 +02003567 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003568 continue;
3569
Ingo Molnardd41f592007-07-09 18:51:59 +02003570 if (wl > max_load) {
3571 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003572 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 }
3574 }
3575
3576 return busiest;
3577}
3578
3579/*
Nick Piggin77391d72005-06-25 14:57:30 -07003580 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3581 * so long as it is large enough.
3582 */
3583#define MAX_PINNED_INTERVAL 512
3584
3585/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3587 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003589static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003590 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003591 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
Peter Williams43010652007-08-09 11:16:46 +02003593 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003596 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003597 unsigned long flags;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003598 int unlock_aggregate;
Nick Piggin5969fe02005-09-10 00:26:19 -07003599
Mike Travis7c16ec52008-04-04 18:11:11 -07003600 cpus_setall(*cpus);
3601
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003602 unlock_aggregate = get_aggregate(sd);
3603
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003604 /*
3605 * When power savings policy is enabled for the parent domain, idle
3606 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003607 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003608 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003609 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003610 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003611 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003612 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Ingo Molnar2d723762007-10-15 17:00:12 +02003614 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003616redo:
3617 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003618 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003619
Chen, Kenneth W06066712006-12-10 02:20:35 -08003620 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003621 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 if (!group) {
3624 schedstat_inc(sd, lb_nobusyg[idle]);
3625 goto out_balanced;
3626 }
3627
Mike Travis7c16ec52008-04-04 18:11:11 -07003628 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 if (!busiest) {
3630 schedstat_inc(sd, lb_nobusyq[idle]);
3631 goto out_balanced;
3632 }
3633
Nick Piggindb935db2005-06-25 14:57:11 -07003634 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
3636 schedstat_add(sd, lb_imbalance[idle], imbalance);
3637
Peter Williams43010652007-08-09 11:16:46 +02003638 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 if (busiest->nr_running > 1) {
3640 /*
3641 * Attempt to move tasks. If find_busiest_group has found
3642 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003643 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 * correctly treated as an imbalance.
3645 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003646 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003647 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003648 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003649 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003650 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003651 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003652
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003653 /*
3654 * some other cpu did the load balance for us.
3655 */
Peter Williams43010652007-08-09 11:16:46 +02003656 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003657 resched_cpu(this_cpu);
3658
Nick Piggin81026792005-06-25 14:57:07 -07003659 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003660 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003661 cpu_clear(cpu_of(busiest), *cpus);
3662 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003663 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003664 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
Nick Piggin81026792005-06-25 14:57:07 -07003667
Peter Williams43010652007-08-09 11:16:46 +02003668 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 schedstat_inc(sd, lb_failed[idle]);
3670 sd->nr_balance_failed++;
3671
3672 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003674 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003675
3676 /* don't kick the migration_thread, if the curr
3677 * task on busiest cpu can't be moved to this_cpu
3678 */
3679 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003680 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003681 all_pinned = 1;
3682 goto out_one_pinned;
3683 }
3684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 if (!busiest->active_balance) {
3686 busiest->active_balance = 1;
3687 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003688 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003690 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003691 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 wake_up_process(busiest->migration_thread);
3693
3694 /*
3695 * We've kicked active balancing, reset the failure
3696 * counter.
3697 */
Nick Piggin39507452005-06-25 14:57:09 -07003698 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 }
Nick Piggin81026792005-06-25 14:57:07 -07003700 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 sd->nr_balance_failed = 0;
3702
Nick Piggin81026792005-06-25 14:57:07 -07003703 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 /* We were unbalanced, so reset the balancing interval */
3705 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003706 } else {
3707 /*
3708 * If we've begun active balancing, start to back off. This
3709 * case may not be covered by the all_pinned logic if there
3710 * is only 1 task on the busy runqueue (because we don't call
3711 * move_tasks).
3712 */
3713 if (sd->balance_interval < sd->max_interval)
3714 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 }
3716
Peter Williams43010652007-08-09 11:16:46 +02003717 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003718 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003719 ld_moved = -1;
3720
3721 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
3723out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 schedstat_inc(sd, lb_balanced[idle]);
3725
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003726 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003727
3728out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003730 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3731 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 sd->balance_interval *= 2;
3733
Ingo Molnar48f24c42006-07-03 00:25:40 -07003734 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003735 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003736 ld_moved = -1;
3737 else
3738 ld_moved = 0;
3739out:
3740 if (unlock_aggregate)
3741 put_aggregate(sd);
3742 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743}
3744
3745/*
3746 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3747 * tasks if there is an imbalance.
3748 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003749 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 * this_rq is locked.
3751 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003752static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003753load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3754 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
3756 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003757 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003759 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003760 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003761 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003762
3763 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003764
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003765 /*
3766 * When power savings policy is enabled for the parent domain, idle
3767 * sibling can pick up load irrespective of busy siblings. In this case,
3768 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003769 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003770 */
3771 if (sd->flags & SD_SHARE_CPUPOWER &&
3772 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003773 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Ingo Molnar2d723762007-10-15 17:00:12 +02003775 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003776redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003777 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003778 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003780 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003781 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 }
3783
Mike Travis7c16ec52008-04-04 18:11:11 -07003784 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003785 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003786 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003787 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 }
3789
Nick Piggindb935db2005-06-25 14:57:11 -07003790 BUG_ON(busiest == this_rq);
3791
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003792 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003793
Peter Williams43010652007-08-09 11:16:46 +02003794 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003795 if (busiest->nr_running > 1) {
3796 /* Attempt to move tasks */
3797 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003798 /* this_rq->clock is already updated */
3799 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003800 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003801 imbalance, sd, CPU_NEWLY_IDLE,
3802 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003803 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003804
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003805 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003806 cpu_clear(cpu_of(busiest), *cpus);
3807 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003808 goto redo;
3809 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003810 }
3811
Peter Williams43010652007-08-09 11:16:46 +02003812 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003813 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003814 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3815 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003816 return -1;
3817 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003818 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Peter Williams43010652007-08-09 11:16:46 +02003820 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003821
3822out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003823 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003824 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003825 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003826 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003827 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003828
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830}
3831
3832/*
3833 * idle_balance is called by schedule() if this_cpu is about to become
3834 * idle. Attempts to pull tasks from other CPUs.
3835 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003836static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837{
3838 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003839 int pulled_task = -1;
3840 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003841 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
3843 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003844 unsigned long interval;
3845
3846 if (!(sd->flags & SD_LOAD_BALANCE))
3847 continue;
3848
3849 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003850 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003851 pulled_task = load_balance_newidle(this_cpu, this_rq,
3852 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003853
3854 interval = msecs_to_jiffies(sd->balance_interval);
3855 if (time_after(next_balance, sd->last_balance + interval))
3856 next_balance = sd->last_balance + interval;
3857 if (pulled_task)
3858 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003860 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003861 /*
3862 * We are going idle. next_balance may be set based on
3863 * a busy processor. So reset next_balance.
3864 */
3865 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867}
3868
3869/*
3870 * active_load_balance is run by migration threads. It pushes running tasks
3871 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3872 * running on each physical CPU where possible, and avoids physical /
3873 * logical imbalances.
3874 *
3875 * Called with busiest_rq locked.
3876 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003877static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878{
Nick Piggin39507452005-06-25 14:57:09 -07003879 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003880 struct sched_domain *sd;
3881 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003882
Ingo Molnar48f24c42006-07-03 00:25:40 -07003883 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003884 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003885 return;
3886
3887 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
3889 /*
Nick Piggin39507452005-06-25 14:57:09 -07003890 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003891 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003892 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 */
Nick Piggin39507452005-06-25 14:57:09 -07003894 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Nick Piggin39507452005-06-25 14:57:09 -07003896 /* move a task from busiest_rq to target_rq */
3897 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003898 update_rq_clock(busiest_rq);
3899 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Nick Piggin39507452005-06-25 14:57:09 -07003901 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003902 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003903 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003904 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003905 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Ingo Molnar48f24c42006-07-03 00:25:40 -07003908 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003909 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Peter Williams43010652007-08-09 11:16:46 +02003911 if (move_one_task(target_rq, target_cpu, busiest_rq,
3912 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003913 schedstat_inc(sd, alb_pushed);
3914 else
3915 schedstat_inc(sd, alb_failed);
3916 }
Nick Piggin39507452005-06-25 14:57:09 -07003917 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918}
3919
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003920#ifdef CONFIG_NO_HZ
3921static struct {
3922 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003923 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003924} nohz ____cacheline_aligned = {
3925 .load_balancer = ATOMIC_INIT(-1),
3926 .cpu_mask = CPU_MASK_NONE,
3927};
3928
Christoph Lameter7835b982006-12-10 02:20:22 -08003929/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003930 * This routine will try to nominate the ilb (idle load balancing)
3931 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3932 * load balancing on behalf of all those cpus. If all the cpus in the system
3933 * go into this tickless mode, then there will be no ilb owner (as there is
3934 * no need for one) and all the cpus will sleep till the next wakeup event
3935 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003936 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003937 * For the ilb owner, tick is not stopped. And this tick will be used
3938 * for idle load balancing. ilb owner will still be part of
3939 * nohz.cpu_mask..
3940 *
3941 * While stopping the tick, this cpu will become the ilb owner if there
3942 * is no other owner. And will be the owner till that cpu becomes busy
3943 * or if all cpus in the system stop their ticks at which point
3944 * there is no need for ilb owner.
3945 *
3946 * When the ilb owner becomes busy, it nominates another owner, during the
3947 * next busy scheduler_tick()
3948 */
3949int select_nohz_load_balancer(int stop_tick)
3950{
3951 int cpu = smp_processor_id();
3952
3953 if (stop_tick) {
3954 cpu_set(cpu, nohz.cpu_mask);
3955 cpu_rq(cpu)->in_nohz_recently = 1;
3956
3957 /*
3958 * If we are going offline and still the leader, give up!
3959 */
3960 if (cpu_is_offline(cpu) &&
3961 atomic_read(&nohz.load_balancer) == cpu) {
3962 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3963 BUG();
3964 return 0;
3965 }
3966
3967 /* time for ilb owner also to sleep */
3968 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3969 if (atomic_read(&nohz.load_balancer) == cpu)
3970 atomic_set(&nohz.load_balancer, -1);
3971 return 0;
3972 }
3973
3974 if (atomic_read(&nohz.load_balancer) == -1) {
3975 /* make me the ilb owner */
3976 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3977 return 1;
3978 } else if (atomic_read(&nohz.load_balancer) == cpu)
3979 return 1;
3980 } else {
3981 if (!cpu_isset(cpu, nohz.cpu_mask))
3982 return 0;
3983
3984 cpu_clear(cpu, nohz.cpu_mask);
3985
3986 if (atomic_read(&nohz.load_balancer) == cpu)
3987 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3988 BUG();
3989 }
3990 return 0;
3991}
3992#endif
3993
3994static DEFINE_SPINLOCK(balancing);
3995
3996/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003997 * It checks each scheduling domain to see if it is due to be balanced,
3998 * and initiates a balancing operation if so.
3999 *
4000 * Balancing parameters are set up in arch_init_sched_domains.
4001 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004002static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08004003{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004004 int balance = 1;
4005 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004006 unsigned long interval;
4007 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004008 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004009 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004010 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07004011 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004013 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 if (!(sd->flags & SD_LOAD_BALANCE))
4015 continue;
4016
4017 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004018 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 interval *= sd->busy_factor;
4020
4021 /* scale ms to jiffies */
4022 interval = msecs_to_jiffies(interval);
4023 if (unlikely(!interval))
4024 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004025 if (interval > HZ*NR_CPUS/10)
4026 interval = HZ*NR_CPUS/10;
4027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
Christoph Lameter08c183f2006-12-10 02:20:29 -08004029 if (sd->flags & SD_SERIALIZE) {
4030 if (!spin_trylock(&balancing))
4031 goto out;
4032 }
4033
Christoph Lameterc9819f42006-12-10 02:20:25 -08004034 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07004035 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004036 /*
4037 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004038 * longer idle, or one of our SMT siblings is
4039 * not idle.
4040 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004041 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004043 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08004045 if (sd->flags & SD_SERIALIZE)
4046 spin_unlock(&balancing);
4047out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004048 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004049 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004050 update_next_balance = 1;
4051 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004052
4053 /*
4054 * Stop the load balance at this level. There is another
4055 * CPU in our sched group which is doing load balancing more
4056 * actively.
4057 */
4058 if (!balance)
4059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004061
4062 /*
4063 * next_balance will be updated only when there is a need.
4064 * When the cpu is attached to null domain for ex, it will not be
4065 * updated.
4066 */
4067 if (likely(update_next_balance))
4068 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004069}
4070
4071/*
4072 * run_rebalance_domains is triggered when needed from the scheduler tick.
4073 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4074 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4075 */
4076static void run_rebalance_domains(struct softirq_action *h)
4077{
Ingo Molnardd41f592007-07-09 18:51:59 +02004078 int this_cpu = smp_processor_id();
4079 struct rq *this_rq = cpu_rq(this_cpu);
4080 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4081 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004082
Ingo Molnardd41f592007-07-09 18:51:59 +02004083 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004084
4085#ifdef CONFIG_NO_HZ
4086 /*
4087 * If this cpu is the owner for idle load balancing, then do the
4088 * balancing on behalf of the other idle cpus whose ticks are
4089 * stopped.
4090 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004091 if (this_rq->idle_at_tick &&
4092 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004093 cpumask_t cpus = nohz.cpu_mask;
4094 struct rq *rq;
4095 int balance_cpu;
4096
Ingo Molnardd41f592007-07-09 18:51:59 +02004097 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004098 for_each_cpu_mask(balance_cpu, cpus) {
4099 /*
4100 * If this cpu gets work to do, stop the load balancing
4101 * work being done for other cpus. Next load
4102 * balancing owner will pick it up.
4103 */
4104 if (need_resched())
4105 break;
4106
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004107 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004108
4109 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004110 if (time_after(this_rq->next_balance, rq->next_balance))
4111 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004112 }
4113 }
4114#endif
4115}
4116
4117/*
4118 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4119 *
4120 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4121 * idle load balancing owner or decide to stop the periodic load balancing,
4122 * if the whole system is idle.
4123 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004124static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004125{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004126#ifdef CONFIG_NO_HZ
4127 /*
4128 * If we were in the nohz mode recently and busy at the current
4129 * scheduler tick, then check if we need to nominate new idle
4130 * load balancer.
4131 */
4132 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4133 rq->in_nohz_recently = 0;
4134
4135 if (atomic_read(&nohz.load_balancer) == cpu) {
4136 cpu_clear(cpu, nohz.cpu_mask);
4137 atomic_set(&nohz.load_balancer, -1);
4138 }
4139
4140 if (atomic_read(&nohz.load_balancer) == -1) {
4141 /*
4142 * simple selection for now: Nominate the
4143 * first cpu in the nohz list to be the next
4144 * ilb owner.
4145 *
4146 * TBD: Traverse the sched domains and nominate
4147 * the nearest cpu in the nohz.cpu_mask.
4148 */
4149 int ilb = first_cpu(nohz.cpu_mask);
4150
Mike Travis434d53b2008-04-04 18:11:04 -07004151 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004152 resched_cpu(ilb);
4153 }
4154 }
4155
4156 /*
4157 * If this cpu is idle and doing idle load balancing for all the
4158 * cpus with ticks stopped, is it time for that to stop?
4159 */
4160 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4161 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4162 resched_cpu(cpu);
4163 return;
4164 }
4165
4166 /*
4167 * If this cpu is idle and the idle load balancing is done by
4168 * someone else, then no need raise the SCHED_SOFTIRQ
4169 */
4170 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4171 cpu_isset(cpu, nohz.cpu_mask))
4172 return;
4173#endif
4174 if (time_after_eq(jiffies, rq->next_balance))
4175 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176}
Ingo Molnardd41f592007-07-09 18:51:59 +02004177
4178#else /* CONFIG_SMP */
4179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180/*
4181 * on UP we do not need to balance between CPUs:
4182 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004183static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
4185}
Ingo Molnardd41f592007-07-09 18:51:59 +02004186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187#endif
4188
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189DEFINE_PER_CPU(struct kernel_stat, kstat);
4190
4191EXPORT_PER_CPU_SYMBOL(kstat);
4192
4193/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004194 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4195 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004197unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004200 u64 ns, delta_exec;
4201 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004202
Ingo Molnar41b86e92007-07-09 18:51:58 +02004203 rq = task_rq_lock(p, &flags);
4204 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004205 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004206 update_rq_clock(rq);
4207 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004208 if ((s64)delta_exec > 0)
4209 ns += delta_exec;
4210 }
4211 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004212
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 return ns;
4214}
4215
4216/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 * Account user cpu time to a process.
4218 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 * @cputime: the cpu time spent in user space since the last update
4220 */
4221void account_user_time(struct task_struct *p, cputime_t cputime)
4222{
4223 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4224 cputime64_t tmp;
4225
4226 p->utime = cputime_add(p->utime, cputime);
4227
4228 /* Add user time to cpustat. */
4229 tmp = cputime_to_cputime64(cputime);
4230 if (TASK_NICE(p) > 0)
4231 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4232 else
4233 cpustat->user = cputime64_add(cpustat->user, tmp);
4234}
4235
4236/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004237 * Account guest cpu time to a process.
4238 * @p: the process that the cpu time gets accounted to
4239 * @cputime: the cpu time spent in virtual machine since the last update
4240 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004241static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004242{
4243 cputime64_t tmp;
4244 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4245
4246 tmp = cputime_to_cputime64(cputime);
4247
4248 p->utime = cputime_add(p->utime, cputime);
4249 p->gtime = cputime_add(p->gtime, cputime);
4250
4251 cpustat->user = cputime64_add(cpustat->user, tmp);
4252 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4253}
4254
4255/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004256 * Account scaled user cpu time to a process.
4257 * @p: the process that the cpu time gets accounted to
4258 * @cputime: the cpu time spent in user space since the last update
4259 */
4260void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4261{
4262 p->utimescaled = cputime_add(p->utimescaled, cputime);
4263}
4264
4265/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 * Account system cpu time to a process.
4267 * @p: the process that the cpu time gets accounted to
4268 * @hardirq_offset: the offset to subtract from hardirq_count()
4269 * @cputime: the cpu time spent in kernel space since the last update
4270 */
4271void account_system_time(struct task_struct *p, int hardirq_offset,
4272 cputime_t cputime)
4273{
4274 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004275 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 cputime64_t tmp;
4277
Christian Borntraeger97783852007-11-15 20:57:39 +01004278 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
4279 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02004280
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 p->stime = cputime_add(p->stime, cputime);
4282
4283 /* Add system time to cpustat. */
4284 tmp = cputime_to_cputime64(cputime);
4285 if (hardirq_count() - hardirq_offset)
4286 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4287 else if (softirq_count())
4288 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004289 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004291 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4293 else
4294 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4295 /* Account for system time used */
4296 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298
4299/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004300 * Account scaled system cpu time to a process.
4301 * @p: the process that the cpu time gets accounted to
4302 * @hardirq_offset: the offset to subtract from hardirq_count()
4303 * @cputime: the cpu time spent in kernel space since the last update
4304 */
4305void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4306{
4307 p->stimescaled = cputime_add(p->stimescaled, cputime);
4308}
4309
4310/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 * Account for involuntary wait time.
4312 * @p: the process from which the cpu time has been stolen
4313 * @steal: the cpu time spent in involuntary wait
4314 */
4315void account_steal_time(struct task_struct *p, cputime_t steal)
4316{
4317 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4318 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004319 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
4321 if (p == rq->idle) {
4322 p->stime = cputime_add(p->stime, steal);
4323 if (atomic_read(&rq->nr_iowait) > 0)
4324 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4325 else
4326 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004327 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4329}
4330
Christoph Lameter7835b982006-12-10 02:20:22 -08004331/*
4332 * This function gets called by the timer code, with HZ frequency.
4333 * We call it with interrupts disabled.
4334 *
4335 * It also gets called by the fork code, when changing the parent's
4336 * timeslices.
4337 */
4338void scheduler_tick(void)
4339{
Christoph Lameter7835b982006-12-10 02:20:22 -08004340 int cpu = smp_processor_id();
4341 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004342 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02004343 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08004344
Ingo Molnardd41f592007-07-09 18:51:59 +02004345 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02004346 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02004347 /*
4348 * Let rq->clock advance by at least TICK_NSEC:
4349 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004350 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02004351 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004352 rq->clock_underflows++;
4353 }
Ingo Molnar529c7722007-08-10 23:05:11 +02004354 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02004355 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004356 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004357 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004358 spin_unlock(&rq->lock);
4359
Christoph Lametere418e1c2006-12-10 02:20:23 -08004360#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004361 rq->idle_at_tick = idle_cpu(cpu);
4362 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004363#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364}
4365
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
4367
Srinivasa Ds43627582008-02-23 15:24:04 -08004368void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369{
4370 /*
4371 * Underflow?
4372 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004373 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4374 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 preempt_count() += val;
4376 /*
4377 * Spinlock count overflowing soon?
4378 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004379 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4380 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381}
4382EXPORT_SYMBOL(add_preempt_count);
4383
Srinivasa Ds43627582008-02-23 15:24:04 -08004384void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
4386 /*
4387 * Underflow?
4388 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004389 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4390 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 /*
4392 * Is the spinlock portion underflowing?
4393 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004394 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4395 !(preempt_count() & PREEMPT_MASK)))
4396 return;
4397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 preempt_count() -= val;
4399}
4400EXPORT_SYMBOL(sub_preempt_count);
4401
4402#endif
4403
4404/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004405 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004407static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
Satyam Sharma838225b2007-10-24 18:23:50 +02004409 struct pt_regs *regs = get_irq_regs();
4410
4411 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4412 prev->comm, prev->pid, preempt_count());
4413
Ingo Molnardd41f592007-07-09 18:51:59 +02004414 debug_show_held_locks(prev);
4415 if (irqs_disabled())
4416 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004417
4418 if (regs)
4419 show_regs(regs);
4420 else
4421 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004422}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Ingo Molnardd41f592007-07-09 18:51:59 +02004424/*
4425 * Various schedule()-time debugging checks and statistics:
4426 */
4427static inline void schedule_debug(struct task_struct *prev)
4428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004430 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 * schedule() atomically, we ignore that path for now.
4432 * Otherwise, whine if we are scheduling when we should not be.
4433 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004434 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4435 __schedule_bug(prev);
4436
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4438
Ingo Molnar2d723762007-10-15 17:00:12 +02004439 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004440#ifdef CONFIG_SCHEDSTATS
4441 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004442 schedstat_inc(this_rq(), bkl_count);
4443 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004444 }
4445#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004446}
4447
4448/*
4449 * Pick up the highest-prio task:
4450 */
4451static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004452pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004453{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004454 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004455 struct task_struct *p;
4456
4457 /*
4458 * Optimization: we know that if all tasks are in
4459 * the fair class we can call that function directly:
4460 */
4461 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004462 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004463 if (likely(p))
4464 return p;
4465 }
4466
4467 class = sched_class_highest;
4468 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004469 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004470 if (p)
4471 return p;
4472 /*
4473 * Will never be NULL as the idle class always
4474 * returns a non-NULL p:
4475 */
4476 class = class->next;
4477 }
4478}
4479
4480/*
4481 * schedule() is the main scheduler function.
4482 */
4483asmlinkage void __sched schedule(void)
4484{
4485 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004486 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004487 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004488 int cpu;
4489
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490need_resched:
4491 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004492 cpu = smp_processor_id();
4493 rq = cpu_rq(cpu);
4494 rcu_qsctr_inc(cpu);
4495 prev = rq->curr;
4496 switch_count = &prev->nivcsw;
4497
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 release_kernel_lock(prev);
4499need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Ingo Molnardd41f592007-07-09 18:51:59 +02004501 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004503 hrtick_clear(rq);
4504
Ingo Molnar1e819952007-10-15 17:00:13 +02004505 /*
4506 * Do the rq-clock update outside the rq lock:
4507 */
4508 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004509 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004510 spin_lock(&rq->lock);
4511 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Ingo Molnardd41f592007-07-09 18:51:59 +02004513 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4514 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004515 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004516 prev->state = TASK_RUNNING;
4517 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004518 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004519 }
4520 switch_count = &prev->nvcsw;
4521 }
4522
Steven Rostedt9a897c52008-01-25 21:08:22 +01004523#ifdef CONFIG_SMP
4524 if (prev->sched_class->pre_schedule)
4525 prev->sched_class->pre_schedule(rq, prev);
4526#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004527
Ingo Molnardd41f592007-07-09 18:51:59 +02004528 if (unlikely(!rq->nr_running))
4529 idle_balance(cpu, rq);
4530
Ingo Molnar31ee5292007-08-09 11:16:49 +02004531 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004532 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533
4534 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02004535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 rq->nr_switches++;
4538 rq->curr = next;
4539 ++*switch_count;
4540
Ingo Molnardd41f592007-07-09 18:51:59 +02004541 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004542 /*
4543 * the context switch might have flipped the stack from under
4544 * us, hence refresh the local variables.
4545 */
4546 cpu = smp_processor_id();
4547 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 } else
4549 spin_unlock_irq(&rq->lock);
4550
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004551 hrtick_set(rq);
4552
4553 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004555
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 preempt_enable_no_resched();
4557 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4558 goto need_resched;
4559}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560EXPORT_SYMBOL(schedule);
4561
4562#ifdef CONFIG_PREEMPT
4563/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004564 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004565 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 * occur there and call schedule directly.
4567 */
4568asmlinkage void __sched preempt_schedule(void)
4569{
4570 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 struct task_struct *task = current;
4572 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004573
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 /*
4575 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004576 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004578 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 return;
4580
Andi Kleen3a5c3592007-10-15 17:00:14 +02004581 do {
4582 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Andi Kleen3a5c3592007-10-15 17:00:14 +02004584 /*
4585 * We keep the big kernel semaphore locked, but we
4586 * clear ->lock_depth so that schedule() doesnt
4587 * auto-release the semaphore:
4588 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004589 saved_lock_depth = task->lock_depth;
4590 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004591 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004592 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004593 sub_preempt_count(PREEMPT_ACTIVE);
4594
4595 /*
4596 * Check again in case we missed a preemption opportunity
4597 * between schedule and now.
4598 */
4599 barrier();
4600 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602EXPORT_SYMBOL(preempt_schedule);
4603
4604/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004605 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 * off of irq context.
4607 * Note, that this is called and return with irqs disabled. This will
4608 * protect us against recursive calling from irq.
4609 */
4610asmlinkage void __sched preempt_schedule_irq(void)
4611{
4612 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 struct task_struct *task = current;
4614 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004615
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004616 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 BUG_ON(ti->preempt_count || !irqs_disabled());
4618
Andi Kleen3a5c3592007-10-15 17:00:14 +02004619 do {
4620 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
Andi Kleen3a5c3592007-10-15 17:00:14 +02004622 /*
4623 * We keep the big kernel semaphore locked, but we
4624 * clear ->lock_depth so that schedule() doesnt
4625 * auto-release the semaphore:
4626 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004627 saved_lock_depth = task->lock_depth;
4628 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004629 local_irq_enable();
4630 schedule();
4631 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004632 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004633 sub_preempt_count(PREEMPT_ACTIVE);
4634
4635 /*
4636 * Check again in case we missed a preemption opportunity
4637 * between schedule and now.
4638 */
4639 barrier();
4640 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
4643#endif /* CONFIG_PREEMPT */
4644
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004645int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4646 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004648 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650EXPORT_SYMBOL(default_wake_function);
4651
4652/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004653 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4654 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 * number) then we wake all the non-exclusive tasks and one exclusive task.
4656 *
4657 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004658 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4660 */
4661static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4662 int nr_exclusive, int sync, void *key)
4663{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004664 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004666 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004667 unsigned flags = curr->flags;
4668
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004670 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 break;
4672 }
4673}
4674
4675/**
4676 * __wake_up - wake up threads blocked on a waitqueue.
4677 * @q: the waitqueue
4678 * @mode: which threads
4679 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004680 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004682void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004683 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684{
4685 unsigned long flags;
4686
4687 spin_lock_irqsave(&q->lock, flags);
4688 __wake_up_common(q, mode, nr_exclusive, 0, key);
4689 spin_unlock_irqrestore(&q->lock, flags);
4690}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691EXPORT_SYMBOL(__wake_up);
4692
4693/*
4694 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4695 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004696void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
4698 __wake_up_common(q, mode, 1, 0, NULL);
4699}
4700
4701/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004702 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 * @q: the waitqueue
4704 * @mode: which threads
4705 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4706 *
4707 * The sync wakeup differs that the waker knows that it will schedule
4708 * away soon, so while the target thread will be woken up, it will not
4709 * be migrated to another CPU - ie. the two threads are 'synchronized'
4710 * with each other. This can prevent needless bouncing between CPUs.
4711 *
4712 * On UP it can prevent extra preemption.
4713 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004714void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004715__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716{
4717 unsigned long flags;
4718 int sync = 1;
4719
4720 if (unlikely(!q))
4721 return;
4722
4723 if (unlikely(!nr_exclusive))
4724 sync = 0;
4725
4726 spin_lock_irqsave(&q->lock, flags);
4727 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4728 spin_unlock_irqrestore(&q->lock, flags);
4729}
4730EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4731
Ingo Molnarb15136e2007-10-24 18:23:48 +02004732void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733{
4734 unsigned long flags;
4735
4736 spin_lock_irqsave(&x->wait.lock, flags);
4737 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004738 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 spin_unlock_irqrestore(&x->wait.lock, flags);
4740}
4741EXPORT_SYMBOL(complete);
4742
Ingo Molnarb15136e2007-10-24 18:23:48 +02004743void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744{
4745 unsigned long flags;
4746
4747 spin_lock_irqsave(&x->wait.lock, flags);
4748 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004749 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 spin_unlock_irqrestore(&x->wait.lock, flags);
4751}
4752EXPORT_SYMBOL(complete_all);
4753
Andi Kleen8cbbe862007-10-15 17:00:14 +02004754static inline long __sched
4755do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 if (!x->done) {
4758 DECLARE_WAITQUEUE(wait, current);
4759
4760 wait.flags |= WQ_FLAG_EXCLUSIVE;
4761 __add_wait_queue_tail(&x->wait, &wait);
4762 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004763 if ((state == TASK_INTERRUPTIBLE &&
4764 signal_pending(current)) ||
4765 (state == TASK_KILLABLE &&
4766 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004767 __remove_wait_queue(&x->wait, &wait);
4768 return -ERESTARTSYS;
4769 }
4770 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004772 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004774 if (!timeout) {
4775 __remove_wait_queue(&x->wait, &wait);
4776 return timeout;
4777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 } while (!x->done);
4779 __remove_wait_queue(&x->wait, &wait);
4780 }
4781 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004782 return timeout;
4783}
4784
4785static long __sched
4786wait_for_common(struct completion *x, long timeout, int state)
4787{
4788 might_sleep();
4789
4790 spin_lock_irq(&x->wait.lock);
4791 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004793 return timeout;
4794}
4795
Ingo Molnarb15136e2007-10-24 18:23:48 +02004796void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004797{
4798 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799}
4800EXPORT_SYMBOL(wait_for_completion);
4801
Ingo Molnarb15136e2007-10-24 18:23:48 +02004802unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4804{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004805 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806}
4807EXPORT_SYMBOL(wait_for_completion_timeout);
4808
Andi Kleen8cbbe862007-10-15 17:00:14 +02004809int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810{
Andi Kleen51e97992007-10-18 21:32:55 +02004811 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4812 if (t == -ERESTARTSYS)
4813 return t;
4814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815}
4816EXPORT_SYMBOL(wait_for_completion_interruptible);
4817
Ingo Molnarb15136e2007-10-24 18:23:48 +02004818unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819wait_for_completion_interruptible_timeout(struct completion *x,
4820 unsigned long timeout)
4821{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004822 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4825
Matthew Wilcox009e5772007-12-06 12:29:54 -05004826int __sched wait_for_completion_killable(struct completion *x)
4827{
4828 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4829 if (t == -ERESTARTSYS)
4830 return t;
4831 return 0;
4832}
4833EXPORT_SYMBOL(wait_for_completion_killable);
4834
Andi Kleen8cbbe862007-10-15 17:00:14 +02004835static long __sched
4836sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004837{
4838 unsigned long flags;
4839 wait_queue_t wait;
4840
4841 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Andi Kleen8cbbe862007-10-15 17:00:14 +02004843 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Andi Kleen8cbbe862007-10-15 17:00:14 +02004845 spin_lock_irqsave(&q->lock, flags);
4846 __add_wait_queue(q, &wait);
4847 spin_unlock(&q->lock);
4848 timeout = schedule_timeout(timeout);
4849 spin_lock_irq(&q->lock);
4850 __remove_wait_queue(q, &wait);
4851 spin_unlock_irqrestore(&q->lock, flags);
4852
4853 return timeout;
4854}
4855
4856void __sched interruptible_sleep_on(wait_queue_head_t *q)
4857{
4858 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860EXPORT_SYMBOL(interruptible_sleep_on);
4861
Ingo Molnar0fec1712007-07-09 18:52:01 +02004862long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004863interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004865 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4868
Ingo Molnar0fec1712007-07-09 18:52:01 +02004869void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004871 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873EXPORT_SYMBOL(sleep_on);
4874
Ingo Molnar0fec1712007-07-09 18:52:01 +02004875long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004877 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879EXPORT_SYMBOL(sleep_on_timeout);
4880
Ingo Molnarb29739f2006-06-27 02:54:51 -07004881#ifdef CONFIG_RT_MUTEXES
4882
4883/*
4884 * rt_mutex_setprio - set the current priority of a task
4885 * @p: task
4886 * @prio: prio value (kernel-internal form)
4887 *
4888 * This function changes the 'effective' priority of a task. It does
4889 * not touch ->normal_prio like __setscheduler().
4890 *
4891 * Used by the rt_mutex code to implement priority inheritance logic.
4892 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004893void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004894{
4895 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004896 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004897 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004898 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004899
4900 BUG_ON(prio < 0 || prio > MAX_PRIO);
4901
4902 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004903 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004904
Andrew Mortond5f9f942007-05-08 20:27:06 -07004905 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004906 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004907 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004908 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004909 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004910 if (running)
4911 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004912
4913 if (rt_prio(prio))
4914 p->sched_class = &rt_sched_class;
4915 else
4916 p->sched_class = &fair_sched_class;
4917
Ingo Molnarb29739f2006-06-27 02:54:51 -07004918 p->prio = prio;
4919
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004920 if (running)
4921 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004922 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004923 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004924
4925 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004926 }
4927 task_rq_unlock(rq, &flags);
4928}
4929
4930#endif
4931
Ingo Molnar36c8b582006-07-03 00:25:41 -07004932void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933{
Ingo Molnardd41f592007-07-09 18:51:59 +02004934 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004936 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
4938 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4939 return;
4940 /*
4941 * We have to be careful, if called from sys_setpriority(),
4942 * the task might be in the middle of scheduling on another CPU.
4943 */
4944 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004945 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 /*
4947 * The RT priorities are set via sched_setscheduler(), but we still
4948 * allow the 'normal' nice value to be set - but as expected
4949 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004950 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004952 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 p->static_prio = NICE_TO_PRIO(nice);
4954 goto out_unlock;
4955 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004956 on_rq = p->se.on_rq;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02004957 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004958 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004961 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004962 old_prio = p->prio;
4963 p->prio = effective_prio(p);
4964 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
Ingo Molnardd41f592007-07-09 18:51:59 +02004966 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004967 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004969 * If the task increased its priority or is running and
4970 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004972 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 resched_task(rq->curr);
4974 }
4975out_unlock:
4976 task_rq_unlock(rq, &flags);
4977}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978EXPORT_SYMBOL(set_user_nice);
4979
Matt Mackalle43379f2005-05-01 08:59:00 -07004980/*
4981 * can_nice - check if a task can reduce its nice value
4982 * @p: task
4983 * @nice: nice value
4984 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004985int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004986{
Matt Mackall024f4742005-08-18 11:24:19 -07004987 /* convert nice value [19,-20] to rlimit style value [1,40] */
4988 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004989
Matt Mackalle43379f2005-05-01 08:59:00 -07004990 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4991 capable(CAP_SYS_NICE));
4992}
4993
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994#ifdef __ARCH_WANT_SYS_NICE
4995
4996/*
4997 * sys_nice - change the priority of the current process.
4998 * @increment: priority increment
4999 *
5000 * sys_setpriority is a more generic, but much slower function that
5001 * does similar things.
5002 */
5003asmlinkage long sys_nice(int increment)
5004{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005005 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
5007 /*
5008 * Setpriority might change our priority at the same moment.
5009 * We don't have to worry. Conceptually one call occurs first
5010 * and we have a single winner.
5011 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005012 if (increment < -40)
5013 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 if (increment > 40)
5015 increment = 40;
5016
5017 nice = PRIO_TO_NICE(current->static_prio) + increment;
5018 if (nice < -20)
5019 nice = -20;
5020 if (nice > 19)
5021 nice = 19;
5022
Matt Mackalle43379f2005-05-01 08:59:00 -07005023 if (increment < 0 && !can_nice(current, nice))
5024 return -EPERM;
5025
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 retval = security_task_setnice(current, nice);
5027 if (retval)
5028 return retval;
5029
5030 set_user_nice(current, nice);
5031 return 0;
5032}
5033
5034#endif
5035
5036/**
5037 * task_prio - return the priority value of a given task.
5038 * @p: the task in question.
5039 *
5040 * This is the priority value as seen by users in /proc.
5041 * RT tasks are offset by -200. Normal tasks are centered
5042 * around 0, value goes from -16 to +15.
5043 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005044int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045{
5046 return p->prio - MAX_RT_PRIO;
5047}
5048
5049/**
5050 * task_nice - return the nice value of a given task.
5051 * @p: the task in question.
5052 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005053int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054{
5055 return TASK_NICE(p);
5056}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005057EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058
5059/**
5060 * idle_cpu - is a given cpu idle currently?
5061 * @cpu: the processor in question.
5062 */
5063int idle_cpu(int cpu)
5064{
5065 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5066}
5067
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068/**
5069 * idle_task - return the idle task for a given cpu.
5070 * @cpu: the processor in question.
5071 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005072struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073{
5074 return cpu_rq(cpu)->idle;
5075}
5076
5077/**
5078 * find_process_by_pid - find a process with a matching PID value.
5079 * @pid: the pid in question.
5080 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005081static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005083 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084}
5085
5086/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005087static void
5088__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089{
Ingo Molnardd41f592007-07-09 18:51:59 +02005090 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005091
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005093 switch (p->policy) {
5094 case SCHED_NORMAL:
5095 case SCHED_BATCH:
5096 case SCHED_IDLE:
5097 p->sched_class = &fair_sched_class;
5098 break;
5099 case SCHED_FIFO:
5100 case SCHED_RR:
5101 p->sched_class = &rt_sched_class;
5102 break;
5103 }
5104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005106 p->normal_prio = normal_prio(p);
5107 /* we are holding p->pi_lock already */
5108 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005109 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110}
5111
5112/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005113 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 * @p: the task in question.
5115 * @policy: new policy.
5116 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005117 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005118 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005120int sched_setscheduler(struct task_struct *p, int policy,
5121 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005123 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005125 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005126 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
Steven Rostedt66e53932006-06-27 02:54:44 -07005128 /* may grab non-irq protected spin_locks */
5129 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130recheck:
5131 /* double check policy once rq lock held */
5132 if (policy < 0)
5133 policy = oldpolicy = p->policy;
5134 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005135 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5136 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005137 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 /*
5139 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005140 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5141 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 */
5143 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005144 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005145 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005147 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 return -EINVAL;
5149
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005150 /*
5151 * Allow unprivileged RT tasks to decrease priority:
5152 */
5153 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005154 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005155 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005156
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005157 if (!lock_task_sighand(p, &flags))
5158 return -ESRCH;
5159 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5160 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005161
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005162 /* can't set/change the rt policy */
5163 if (policy != p->policy && !rlim_rtprio)
5164 return -EPERM;
5165
5166 /* can't increase priority */
5167 if (param->sched_priority > p->rt_priority &&
5168 param->sched_priority > rlim_rtprio)
5169 return -EPERM;
5170 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005171 /*
5172 * Like positive nice levels, dont allow tasks to
5173 * move out of SCHED_IDLE either:
5174 */
5175 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5176 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005177
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005178 /* can't change other user's priorities */
5179 if ((current->euid != p->euid) &&
5180 (current->euid != p->uid))
5181 return -EPERM;
5182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005184#ifdef CONFIG_RT_GROUP_SCHED
5185 /*
5186 * Do not allow realtime tasks into groups that have no runtime
5187 * assigned.
5188 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02005189 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005190 return -EPERM;
5191#endif
5192
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 retval = security_task_setscheduler(p, policy, param);
5194 if (retval)
5195 return retval;
5196 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005197 * make sure no PI-waiters arrive (or leave) while we are
5198 * changing the priority of the task:
5199 */
5200 spin_lock_irqsave(&p->pi_lock, flags);
5201 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 * To be able to change p->policy safely, the apropriate
5203 * runqueue lock must be held.
5204 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005205 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 /* recheck policy now with rq lock held */
5207 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5208 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005209 __task_rq_unlock(rq);
5210 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 goto recheck;
5212 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005213 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005214 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005215 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005216 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005217 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005218 if (running)
5219 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005220
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005222 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005223
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005224 if (running)
5225 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005226 if (on_rq) {
5227 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005228
5229 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005231 __task_rq_unlock(rq);
5232 spin_unlock_irqrestore(&p->pi_lock, flags);
5233
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005234 rt_mutex_adjust_pi(p);
5235
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 return 0;
5237}
5238EXPORT_SYMBOL_GPL(sched_setscheduler);
5239
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005240static int
5241do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 struct sched_param lparam;
5244 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005245 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
5247 if (!param || pid < 0)
5248 return -EINVAL;
5249 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5250 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005251
5252 rcu_read_lock();
5253 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005255 if (p != NULL)
5256 retval = sched_setscheduler(p, policy, &lparam);
5257 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005258
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 return retval;
5260}
5261
5262/**
5263 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5264 * @pid: the pid in question.
5265 * @policy: new policy.
5266 * @param: structure containing the new RT priority.
5267 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005268asmlinkage long
5269sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270{
Jason Baronc21761f2006-01-18 17:43:03 -08005271 /* negative values for policy are not valid */
5272 if (policy < 0)
5273 return -EINVAL;
5274
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 return do_sched_setscheduler(pid, policy, param);
5276}
5277
5278/**
5279 * sys_sched_setparam - set/change the RT priority of a thread
5280 * @pid: the pid in question.
5281 * @param: structure containing the new RT priority.
5282 */
5283asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5284{
5285 return do_sched_setscheduler(pid, -1, param);
5286}
5287
5288/**
5289 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5290 * @pid: the pid in question.
5291 */
5292asmlinkage long sys_sched_getscheduler(pid_t pid)
5293{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005294 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005295 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
5297 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005298 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
5300 retval = -ESRCH;
5301 read_lock(&tasklist_lock);
5302 p = find_process_by_pid(pid);
5303 if (p) {
5304 retval = security_task_getscheduler(p);
5305 if (!retval)
5306 retval = p->policy;
5307 }
5308 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 return retval;
5310}
5311
5312/**
5313 * sys_sched_getscheduler - get the RT priority of a thread
5314 * @pid: the pid in question.
5315 * @param: structure containing the RT priority.
5316 */
5317asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5318{
5319 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005320 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005321 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322
5323 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005324 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
5326 read_lock(&tasklist_lock);
5327 p = find_process_by_pid(pid);
5328 retval = -ESRCH;
5329 if (!p)
5330 goto out_unlock;
5331
5332 retval = security_task_getscheduler(p);
5333 if (retval)
5334 goto out_unlock;
5335
5336 lp.sched_priority = p->rt_priority;
5337 read_unlock(&tasklist_lock);
5338
5339 /*
5340 * This one might sleep, we cannot do it with a spinlock held ...
5341 */
5342 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5343
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 return retval;
5345
5346out_unlock:
5347 read_unlock(&tasklist_lock);
5348 return retval;
5349}
5350
Mike Travisb53e9212008-04-04 18:11:08 -07005351long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005354 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005355 struct task_struct *p;
5356 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005358 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 read_lock(&tasklist_lock);
5360
5361 p = find_process_by_pid(pid);
5362 if (!p) {
5363 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005364 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 return -ESRCH;
5366 }
5367
5368 /*
5369 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005370 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 * usage count and then drop tasklist_lock.
5372 */
5373 get_task_struct(p);
5374 read_unlock(&tasklist_lock);
5375
5376 retval = -EPERM;
5377 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5378 !capable(CAP_SYS_NICE))
5379 goto out_unlock;
5380
David Quigleye7834f82006-06-23 02:03:59 -07005381 retval = security_task_setscheduler(p, 0, NULL);
5382 if (retval)
5383 goto out_unlock;
5384
Mike Travisf9a86fc2008-04-04 18:11:07 -07005385 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005387 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005388 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
Paul Menage8707d8b2007-10-18 23:40:22 -07005390 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005391 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005392 if (!cpus_subset(new_mask, cpus_allowed)) {
5393 /*
5394 * We must have raced with a concurrent cpuset
5395 * update. Just reset the cpus_allowed to the
5396 * cpuset's cpus_allowed
5397 */
5398 new_mask = cpus_allowed;
5399 goto again;
5400 }
5401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402out_unlock:
5403 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005404 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 return retval;
5406}
5407
5408static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5409 cpumask_t *new_mask)
5410{
5411 if (len < sizeof(cpumask_t)) {
5412 memset(new_mask, 0, sizeof(cpumask_t));
5413 } else if (len > sizeof(cpumask_t)) {
5414 len = sizeof(cpumask_t);
5415 }
5416 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5417}
5418
5419/**
5420 * sys_sched_setaffinity - set the cpu affinity of a process
5421 * @pid: pid of the process
5422 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5423 * @user_mask_ptr: user-space pointer to the new cpu mask
5424 */
5425asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5426 unsigned long __user *user_mask_ptr)
5427{
5428 cpumask_t new_mask;
5429 int retval;
5430
5431 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5432 if (retval)
5433 return retval;
5434
Mike Travisb53e9212008-04-04 18:11:08 -07005435 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436}
5437
5438/*
5439 * Represents all cpu's present in the system
5440 * In systems capable of hotplug, this map could dynamically grow
5441 * as new cpu's are detected in the system via any platform specific
5442 * method, such as ACPI for e.g.
5443 */
5444
Andi Kleen4cef0c62006-01-11 22:44:57 +01005445cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446EXPORT_SYMBOL(cpu_present_map);
5447
5448#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005449cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005450EXPORT_SYMBOL(cpu_online_map);
5451
Andi Kleen4cef0c62006-01-11 22:44:57 +01005452cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005453EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454#endif
5455
5456long sched_getaffinity(pid_t pid, cpumask_t *mask)
5457{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005458 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005461 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 read_lock(&tasklist_lock);
5463
5464 retval = -ESRCH;
5465 p = find_process_by_pid(pid);
5466 if (!p)
5467 goto out_unlock;
5468
David Quigleye7834f82006-06-23 02:03:59 -07005469 retval = security_task_getscheduler(p);
5470 if (retval)
5471 goto out_unlock;
5472
Jack Steiner2f7016d2006-02-01 03:05:18 -08005473 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
5475out_unlock:
5476 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005477 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
Ulrich Drepper9531b622007-08-09 11:16:46 +02005479 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480}
5481
5482/**
5483 * sys_sched_getaffinity - get the cpu affinity of a process
5484 * @pid: pid of the process
5485 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5486 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5487 */
5488asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5489 unsigned long __user *user_mask_ptr)
5490{
5491 int ret;
5492 cpumask_t mask;
5493
5494 if (len < sizeof(cpumask_t))
5495 return -EINVAL;
5496
5497 ret = sched_getaffinity(pid, &mask);
5498 if (ret < 0)
5499 return ret;
5500
5501 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5502 return -EFAULT;
5503
5504 return sizeof(cpumask_t);
5505}
5506
5507/**
5508 * sys_sched_yield - yield the current processor to other threads.
5509 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005510 * This function yields the current CPU to other tasks. If there are no
5511 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 */
5513asmlinkage long sys_sched_yield(void)
5514{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005515 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
Ingo Molnar2d723762007-10-15 17:00:12 +02005517 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005518 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519
5520 /*
5521 * Since we are going to call schedule() anyway, there's
5522 * no need to preempt or enable interrupts:
5523 */
5524 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005525 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 _raw_spin_unlock(&rq->lock);
5527 preempt_enable_no_resched();
5528
5529 schedule();
5530
5531 return 0;
5532}
5533
Andrew Mortone7b38402006-06-30 01:56:00 -07005534static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005536#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5537 __might_sleep(__FILE__, __LINE__);
5538#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005539 /*
5540 * The BKS might be reacquired before we have dropped
5541 * PREEMPT_ACTIVE, which could trigger a second
5542 * cond_resched() call.
5543 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 do {
5545 add_preempt_count(PREEMPT_ACTIVE);
5546 schedule();
5547 sub_preempt_count(PREEMPT_ACTIVE);
5548 } while (need_resched());
5549}
5550
Herbert Xu02b67cc32008-01-25 21:08:28 +01005551#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5552int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553{
Ingo Molnar94142322006-12-29 16:48:13 -08005554 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5555 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 __cond_resched();
5557 return 1;
5558 }
5559 return 0;
5560}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005561EXPORT_SYMBOL(_cond_resched);
5562#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
5564/*
5565 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5566 * call schedule, and on return reacquire the lock.
5567 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005568 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 * operations here to prevent schedule() from being called twice (once via
5570 * spin_unlock(), once by hand).
5571 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005572int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573{
Nick Piggin95c354f2008-01-30 13:31:20 +01005574 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005575 int ret = 0;
5576
Nick Piggin95c354f2008-01-30 13:31:20 +01005577 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005579 if (resched && need_resched())
5580 __cond_resched();
5581 else
5582 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005583 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005586 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588EXPORT_SYMBOL(cond_resched_lock);
5589
5590int __sched cond_resched_softirq(void)
5591{
5592 BUG_ON(!in_softirq());
5593
Ingo Molnar94142322006-12-29 16:48:13 -08005594 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005595 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 __cond_resched();
5597 local_bh_disable();
5598 return 1;
5599 }
5600 return 0;
5601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602EXPORT_SYMBOL(cond_resched_softirq);
5603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604/**
5605 * yield - yield the current processor to other threads.
5606 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005607 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 * thread runnable and calls sys_sched_yield().
5609 */
5610void __sched yield(void)
5611{
5612 set_current_state(TASK_RUNNING);
5613 sys_sched_yield();
5614}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615EXPORT_SYMBOL(yield);
5616
5617/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005618 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 * that process accounting knows that this is a task in IO wait state.
5620 *
5621 * But don't do that if it is a deliberate, throttling IO wait (this task
5622 * has set its backing_dev_info: the queue against which it should throttle)
5623 */
5624void __sched io_schedule(void)
5625{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005626 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005628 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 atomic_inc(&rq->nr_iowait);
5630 schedule();
5631 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005632 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634EXPORT_SYMBOL(io_schedule);
5635
5636long __sched io_schedule_timeout(long timeout)
5637{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005638 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 long ret;
5640
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005641 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 atomic_inc(&rq->nr_iowait);
5643 ret = schedule_timeout(timeout);
5644 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005645 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 return ret;
5647}
5648
5649/**
5650 * sys_sched_get_priority_max - return maximum RT priority.
5651 * @policy: scheduling class.
5652 *
5653 * this syscall returns the maximum rt_priority that can be used
5654 * by a given scheduling class.
5655 */
5656asmlinkage long sys_sched_get_priority_max(int policy)
5657{
5658 int ret = -EINVAL;
5659
5660 switch (policy) {
5661 case SCHED_FIFO:
5662 case SCHED_RR:
5663 ret = MAX_USER_RT_PRIO-1;
5664 break;
5665 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005666 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005667 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 ret = 0;
5669 break;
5670 }
5671 return ret;
5672}
5673
5674/**
5675 * sys_sched_get_priority_min - return minimum RT priority.
5676 * @policy: scheduling class.
5677 *
5678 * this syscall returns the minimum rt_priority that can be used
5679 * by a given scheduling class.
5680 */
5681asmlinkage long sys_sched_get_priority_min(int policy)
5682{
5683 int ret = -EINVAL;
5684
5685 switch (policy) {
5686 case SCHED_FIFO:
5687 case SCHED_RR:
5688 ret = 1;
5689 break;
5690 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005691 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005692 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 ret = 0;
5694 }
5695 return ret;
5696}
5697
5698/**
5699 * sys_sched_rr_get_interval - return the default timeslice of a process.
5700 * @pid: pid of the process.
5701 * @interval: userspace pointer to the timeslice value.
5702 *
5703 * this syscall writes the default timeslice value of a given process
5704 * into the user-space timespec buffer. A value of '0' means infinity.
5705 */
5706asmlinkage
5707long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5708{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005709 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005710 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005711 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713
5714 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005715 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716
5717 retval = -ESRCH;
5718 read_lock(&tasklist_lock);
5719 p = find_process_by_pid(pid);
5720 if (!p)
5721 goto out_unlock;
5722
5723 retval = security_task_getscheduler(p);
5724 if (retval)
5725 goto out_unlock;
5726
Ingo Molnar77034932007-12-04 17:04:39 +01005727 /*
5728 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5729 * tasks that are on an otherwise idle runqueue:
5730 */
5731 time_slice = 0;
5732 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005733 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005734 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005735 struct sched_entity *se = &p->se;
5736 unsigned long flags;
5737 struct rq *rq;
5738
5739 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005740 if (rq->cfs.load.weight)
5741 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005742 task_rq_unlock(rq, &flags);
5743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005745 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005748
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749out_unlock:
5750 read_unlock(&tasklist_lock);
5751 return retval;
5752}
5753
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005754static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005755
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005756void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005759 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005762 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005763 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005764#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005766 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005768 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769#else
5770 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005771 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005773 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774#endif
5775#ifdef CONFIG_DEBUG_STACK_USAGE
5776 {
Al Viro10ebffd2005-11-13 16:06:56 -08005777 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 while (!*n)
5779 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005780 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 }
5782#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005783 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005784 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005786 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787}
5788
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005789void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005791 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
Ingo Molnar4bd77322007-07-11 21:21:47 +02005793#if BITS_PER_LONG == 32
5794 printk(KERN_INFO
5795 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005797 printk(KERN_INFO
5798 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799#endif
5800 read_lock(&tasklist_lock);
5801 do_each_thread(g, p) {
5802 /*
5803 * reset the NMI-timeout, listing all files on a slow
5804 * console might take alot of time:
5805 */
5806 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005807 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005808 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 } while_each_thread(g, p);
5810
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005811 touch_all_softlockup_watchdogs();
5812
Ingo Molnardd41f592007-07-09 18:51:59 +02005813#ifdef CONFIG_SCHED_DEBUG
5814 sysrq_sched_debug_show();
5815#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005817 /*
5818 * Only show locks if all tasks are dumped:
5819 */
5820 if (state_filter == -1)
5821 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822}
5823
Ingo Molnar1df21052007-07-09 18:51:58 +02005824void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5825{
Ingo Molnardd41f592007-07-09 18:51:59 +02005826 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005827}
5828
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005829/**
5830 * init_idle - set up an idle thread for a given CPU
5831 * @idle: task in question
5832 * @cpu: cpu the idle task belongs to
5833 *
5834 * NOTE: this function does not set the idle thread's NEED_RESCHED
5835 * flag, to make booting more robust.
5836 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005837void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005839 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 unsigned long flags;
5841
Ingo Molnardd41f592007-07-09 18:51:59 +02005842 __sched_fork(idle);
5843 idle->se.exec_start = sched_clock();
5844
Ingo Molnarb29739f2006-06-27 02:54:51 -07005845 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005847 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
5849 spin_lock_irqsave(&rq->lock, flags);
5850 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005851#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5852 idle->oncpu = 1;
5853#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 spin_unlock_irqrestore(&rq->lock, flags);
5855
5856 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08005857 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005858
Ingo Molnardd41f592007-07-09 18:51:59 +02005859 /*
5860 * The idle tasks have their own, simple scheduling class:
5861 */
5862 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863}
5864
5865/*
5866 * In a system that switches off the HZ timer nohz_cpu_mask
5867 * indicates which cpus entered this state. This is used
5868 * in the rcu update to wait only for active cpus. For system
5869 * which do not switch off the HZ timer nohz_cpu_mask should
5870 * always be CPU_MASK_NONE.
5871 */
5872cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5873
Ingo Molnar19978ca2007-11-09 22:39:38 +01005874/*
5875 * Increase the granularity value when there are more CPUs,
5876 * because with more CPUs the 'effective latency' as visible
5877 * to users decreases. But the relationship is not linear,
5878 * so pick a second-best guess by going with the log2 of the
5879 * number of CPUs.
5880 *
5881 * This idea comes from the SD scheduler of Con Kolivas:
5882 */
5883static inline void sched_init_granularity(void)
5884{
5885 unsigned int factor = 1 + ilog2(num_online_cpus());
5886 const unsigned long limit = 200000000;
5887
5888 sysctl_sched_min_granularity *= factor;
5889 if (sysctl_sched_min_granularity > limit)
5890 sysctl_sched_min_granularity = limit;
5891
5892 sysctl_sched_latency *= factor;
5893 if (sysctl_sched_latency > limit)
5894 sysctl_sched_latency = limit;
5895
5896 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005897}
5898
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899#ifdef CONFIG_SMP
5900/*
5901 * This is how migration works:
5902 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005903 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 * runqueue and wake up that CPU's migration thread.
5905 * 2) we down() the locked semaphore => thread blocks.
5906 * 3) migration thread wakes up (implicitly it forces the migrated
5907 * thread off the CPU)
5908 * 4) it gets the migration request and checks whether the migrated
5909 * task is still in the wrong runqueue.
5910 * 5) if it's in the wrong runqueue then the migration thread removes
5911 * it and puts it into the right queue.
5912 * 6) migration thread up()s the semaphore.
5913 * 7) we wake up and the migration is done.
5914 */
5915
5916/*
5917 * Change a given task's CPU affinity. Migrate the thread to a
5918 * proper CPU and schedule it away if the CPU it's executing on
5919 * is removed from the allowed bitmask.
5920 *
5921 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005922 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 * call is not atomic; no spinlocks may be held.
5924 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005925int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005927 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005929 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005930 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005933 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 ret = -EINVAL;
5935 goto out;
5936 }
5937
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005938 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005939 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005940 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005941 p->cpus_allowed = *new_mask;
5942 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005943 }
5944
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005946 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 goto out;
5948
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005949 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 /* Need help from migration thread: drop lock and wait. */
5951 task_rq_unlock(rq, &flags);
5952 wake_up_process(rq->migration_thread);
5953 wait_for_completion(&req.done);
5954 tlb_migrate_finish(p->mm);
5955 return 0;
5956 }
5957out:
5958 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005959
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 return ret;
5961}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005962EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
5964/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005965 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 * this because either it can't run here any more (set_cpus_allowed()
5967 * away from this CPU, or CPU going down), or because we're
5968 * attempting to rebalance this task on exec (sched_exec).
5969 *
5970 * So we race with normal scheduler movements, but that's OK, as long
5971 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005972 *
5973 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005975static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005977 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005978 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
5980 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005981 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
5983 rq_src = cpu_rq(src_cpu);
5984 rq_dest = cpu_rq(dest_cpu);
5985
5986 double_rq_lock(rq_src, rq_dest);
5987 /* Already moved. */
5988 if (task_cpu(p) != src_cpu)
5989 goto out;
5990 /* Affinity changed (again). */
5991 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5992 goto out;
5993
Ingo Molnardd41f592007-07-09 18:51:59 +02005994 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005995 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005996 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005997
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005999 if (on_rq) {
6000 activate_task(rq_dest, p, 0);
6001 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07006003 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004out:
6005 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006006 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007}
6008
6009/*
6010 * migration_thread - this is a highprio system thread that performs
6011 * thread migration by bumping thread off CPU then 'pushing' onto
6012 * another runqueue.
6013 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006014static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006017 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
6019 rq = cpu_rq(cpu);
6020 BUG_ON(rq->migration_thread != current);
6021
6022 set_current_state(TASK_INTERRUPTIBLE);
6023 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006024 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 spin_lock_irq(&rq->lock);
6028
6029 if (cpu_is_offline(cpu)) {
6030 spin_unlock_irq(&rq->lock);
6031 goto wait_to_die;
6032 }
6033
6034 if (rq->active_balance) {
6035 active_load_balance(rq, cpu);
6036 rq->active_balance = 0;
6037 }
6038
6039 head = &rq->migration_queue;
6040
6041 if (list_empty(head)) {
6042 spin_unlock_irq(&rq->lock);
6043 schedule();
6044 set_current_state(TASK_INTERRUPTIBLE);
6045 continue;
6046 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006047 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 list_del_init(head->next);
6049
Nick Piggin674311d2005-06-25 14:57:27 -07006050 spin_unlock(&rq->lock);
6051 __migrate_task(req->task, cpu, req->dest_cpu);
6052 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
6054 complete(&req->done);
6055 }
6056 __set_current_state(TASK_RUNNING);
6057 return 0;
6058
6059wait_to_die:
6060 /* Wait for kthread_stop */
6061 set_current_state(TASK_INTERRUPTIBLE);
6062 while (!kthread_should_stop()) {
6063 schedule();
6064 set_current_state(TASK_INTERRUPTIBLE);
6065 }
6066 __set_current_state(TASK_RUNNING);
6067 return 0;
6068}
6069
6070#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006071
6072static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6073{
6074 int ret;
6075
6076 local_irq_disable();
6077 ret = __migrate_task(p, src_cpu, dest_cpu);
6078 local_irq_enable();
6079 return ret;
6080}
6081
Kirill Korotaev054b9102006-12-10 02:20:11 -08006082/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006083 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006084 * NOTE: interrupts should be disabled by the caller
6085 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006086static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006088 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006090 struct rq *rq;
6091 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
Andi Kleen3a5c3592007-10-15 17:00:14 +02006093 do {
6094 /* On same node? */
6095 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6096 cpus_and(mask, mask, p->cpus_allowed);
6097 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098
Andi Kleen3a5c3592007-10-15 17:00:14 +02006099 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006100 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006101 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
Andi Kleen3a5c3592007-10-15 17:00:14 +02006103 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006104 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006105 cpumask_t cpus_allowed;
6106
6107 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006108 /*
6109 * Try to stay on the same cpuset, where the
6110 * current cpuset may be a subset of all cpus.
6111 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006112 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006113 * called within calls to cpuset_lock/cpuset_unlock.
6114 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006115 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006116 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006117 dest_cpu = any_online_cpu(p->cpus_allowed);
6118 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
Andi Kleen3a5c3592007-10-15 17:00:14 +02006120 /*
6121 * Don't tell them about moving exiting tasks or
6122 * kernel threads (both mm NULL), since they never
6123 * leave kernel.
6124 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006125 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006126 printk(KERN_INFO "process %d (%s) no "
6127 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006128 task_pid_nr(p), p->comm, dead_cpu);
6129 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006130 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006131 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132}
6133
6134/*
6135 * While a dead CPU has no uninterruptible tasks queued at this point,
6136 * it might still have a nonzero ->nr_uninterruptible counter, because
6137 * for performance reasons the counter is not stricly tracking tasks to
6138 * their home CPUs. So we just add the counter to another CPU's counter,
6139 * to keep the global sum constant after CPU-down:
6140 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006141static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142{
Mike Travis7c16ec52008-04-04 18:11:11 -07006143 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 unsigned long flags;
6145
6146 local_irq_save(flags);
6147 double_rq_lock(rq_src, rq_dest);
6148 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6149 rq_src->nr_uninterruptible = 0;
6150 double_rq_unlock(rq_src, rq_dest);
6151 local_irq_restore(flags);
6152}
6153
6154/* Run through task list and migrate tasks from the dead cpu. */
6155static void migrate_live_tasks(int src_cpu)
6156{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006157 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006159 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
Ingo Molnar48f24c42006-07-03 00:25:40 -07006161 do_each_thread(t, p) {
6162 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 continue;
6164
Ingo Molnar48f24c42006-07-03 00:25:40 -07006165 if (task_cpu(p) == src_cpu)
6166 move_task_off_dead_cpu(src_cpu, p);
6167 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006169 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170}
6171
Ingo Molnardd41f592007-07-09 18:51:59 +02006172/*
6173 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006174 * It does so by boosting its priority to highest possible.
6175 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 */
6177void sched_idle_next(void)
6178{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006179 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006180 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 struct task_struct *p = rq->idle;
6182 unsigned long flags;
6183
6184 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006185 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
Ingo Molnar48f24c42006-07-03 00:25:40 -07006187 /*
6188 * Strictly not necessary since rest of the CPUs are stopped by now
6189 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 */
6191 spin_lock_irqsave(&rq->lock, flags);
6192
Ingo Molnardd41f592007-07-09 18:51:59 +02006193 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006194
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006195 update_rq_clock(rq);
6196 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197
6198 spin_unlock_irqrestore(&rq->lock, flags);
6199}
6200
Ingo Molnar48f24c42006-07-03 00:25:40 -07006201/*
6202 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 * offline.
6204 */
6205void idle_task_exit(void)
6206{
6207 struct mm_struct *mm = current->active_mm;
6208
6209 BUG_ON(cpu_online(smp_processor_id()));
6210
6211 if (mm != &init_mm)
6212 switch_mm(mm, &init_mm, current);
6213 mmdrop(mm);
6214}
6215
Kirill Korotaev054b9102006-12-10 02:20:11 -08006216/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006217static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006219 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
6221 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006222 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
6224 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006225 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
Ingo Molnar48f24c42006-07-03 00:25:40 -07006227 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
6229 /*
6230 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006231 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 * fine.
6233 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006234 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006235 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006236 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
Ingo Molnar48f24c42006-07-03 00:25:40 -07006238 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239}
6240
6241/* release_task() removes task from tasklist, so we won't find dead tasks. */
6242static void migrate_dead_tasks(unsigned int dead_cpu)
6243{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006244 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006245 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
Ingo Molnardd41f592007-07-09 18:51:59 +02006247 for ( ; ; ) {
6248 if (!rq->nr_running)
6249 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006250 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006251 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006252 if (!next)
6253 break;
6254 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006255
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 }
6257}
6258#endif /* CONFIG_HOTPLUG_CPU */
6259
Nick Piggine692ab52007-07-26 13:40:43 +02006260#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6261
6262static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006263 {
6264 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006265 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006266 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006267 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006268};
6269
6270static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006271 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006272 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006273 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006274 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006275 .child = sd_ctl_dir,
6276 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006277 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006278};
6279
6280static struct ctl_table *sd_alloc_ctl_entry(int n)
6281{
6282 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006283 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006284
Nick Piggine692ab52007-07-26 13:40:43 +02006285 return entry;
6286}
6287
Milton Miller6382bc92007-10-15 17:00:19 +02006288static void sd_free_ctl_entry(struct ctl_table **tablep)
6289{
Milton Millercd7900762007-10-17 16:55:11 +02006290 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006291
Milton Millercd7900762007-10-17 16:55:11 +02006292 /*
6293 * In the intermediate directories, both the child directory and
6294 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006295 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006296 * static strings and all have proc handlers.
6297 */
6298 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006299 if (entry->child)
6300 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006301 if (entry->proc_handler == NULL)
6302 kfree(entry->procname);
6303 }
Milton Miller6382bc92007-10-15 17:00:19 +02006304
6305 kfree(*tablep);
6306 *tablep = NULL;
6307}
6308
Nick Piggine692ab52007-07-26 13:40:43 +02006309static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006310set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006311 const char *procname, void *data, int maxlen,
6312 mode_t mode, proc_handler *proc_handler)
6313{
Nick Piggine692ab52007-07-26 13:40:43 +02006314 entry->procname = procname;
6315 entry->data = data;
6316 entry->maxlen = maxlen;
6317 entry->mode = mode;
6318 entry->proc_handler = proc_handler;
6319}
6320
6321static struct ctl_table *
6322sd_alloc_ctl_domain_table(struct sched_domain *sd)
6323{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006324 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006325
Milton Millerad1cdc12007-10-15 17:00:19 +02006326 if (table == NULL)
6327 return NULL;
6328
Alexey Dobriyane0361852007-08-09 11:16:46 +02006329 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006330 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006331 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006332 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006333 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006334 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006335 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006336 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006337 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006338 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006339 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006340 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006341 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006342 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006343 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006344 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006345 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006346 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006347 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006348 &sd->cache_nice_tries,
6349 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006350 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006351 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006352 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006353
6354 return table;
6355}
6356
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006357static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006358{
6359 struct ctl_table *entry, *table;
6360 struct sched_domain *sd;
6361 int domain_num = 0, i;
6362 char buf[32];
6363
6364 for_each_domain(cpu, sd)
6365 domain_num++;
6366 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006367 if (table == NULL)
6368 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006369
6370 i = 0;
6371 for_each_domain(cpu, sd) {
6372 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006373 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006374 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006375 entry->child = sd_alloc_ctl_domain_table(sd);
6376 entry++;
6377 i++;
6378 }
6379 return table;
6380}
6381
6382static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006383static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006384{
6385 int i, cpu_num = num_online_cpus();
6386 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6387 char buf[32];
6388
Milton Miller73785472007-10-24 18:23:48 +02006389 WARN_ON(sd_ctl_dir[0].child);
6390 sd_ctl_dir[0].child = entry;
6391
Milton Millerad1cdc12007-10-15 17:00:19 +02006392 if (entry == NULL)
6393 return;
6394
Milton Miller97b6ea72007-10-15 17:00:19 +02006395 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006396 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006397 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006398 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006399 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006400 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006401 }
Milton Miller73785472007-10-24 18:23:48 +02006402
6403 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006404 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6405}
Milton Miller6382bc92007-10-15 17:00:19 +02006406
Milton Miller73785472007-10-24 18:23:48 +02006407/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006408static void unregister_sched_domain_sysctl(void)
6409{
Milton Miller73785472007-10-24 18:23:48 +02006410 if (sd_sysctl_header)
6411 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006412 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006413 if (sd_ctl_dir[0].child)
6414 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006415}
Nick Piggine692ab52007-07-26 13:40:43 +02006416#else
Milton Miller6382bc92007-10-15 17:00:19 +02006417static void register_sched_domain_sysctl(void)
6418{
6419}
6420static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006421{
6422}
6423#endif
6424
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425/*
6426 * migration_call - callback that gets triggered when a CPU is added.
6427 * Here we can start up the necessary migration thread for the new CPU.
6428 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006429static int __cpuinit
6430migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006433 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006435 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436
6437 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006438
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006440 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006441 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 if (IS_ERR(p))
6443 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 kthread_bind(p, cpu);
6445 /* Must be high prio: stop_machine expects to yield to it. */
6446 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006447 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 task_rq_unlock(rq, &flags);
6449 cpu_rq(cpu)->migration_thread = p;
6450 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006451
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006453 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006454 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006456
6457 /* Update our root-domain */
6458 rq = cpu_rq(cpu);
6459 spin_lock_irqsave(&rq->lock, flags);
6460 if (rq->rd) {
6461 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6462 cpu_set(cpu, rq->rd->online);
6463 }
6464 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006466
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467#ifdef CONFIG_HOTPLUG_CPU
6468 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006469 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006470 if (!cpu_rq(cpu)->migration_thread)
6471 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006472 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006473 kthread_bind(cpu_rq(cpu)->migration_thread,
6474 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 kthread_stop(cpu_rq(cpu)->migration_thread);
6476 cpu_rq(cpu)->migration_thread = NULL;
6477 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006478
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006480 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006481 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 migrate_live_tasks(cpu);
6483 rq = cpu_rq(cpu);
6484 kthread_stop(rq->migration_thread);
6485 rq->migration_thread = NULL;
6486 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006487 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006488 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006489 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006491 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6492 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006494 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006495 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 migrate_nr_uninterruptible(rq);
6497 BUG_ON(rq->nr_running != 0);
6498
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006499 /*
6500 * No need to migrate the tasks: it was best-effort if
6501 * they didn't take sched_hotcpu_mutex. Just wake up
6502 * the requestors.
6503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 spin_lock_irq(&rq->lock);
6505 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006506 struct migration_req *req;
6507
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006509 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 list_del_init(&req->list);
6511 complete(&req->done);
6512 }
6513 spin_unlock_irq(&rq->lock);
6514 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006515
Gregory Haskins08f503b2008-03-10 17:59:11 -04006516 case CPU_DYING:
6517 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006518 /* Update our root-domain */
6519 rq = cpu_rq(cpu);
6520 spin_lock_irqsave(&rq->lock, flags);
6521 if (rq->rd) {
6522 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6523 cpu_clear(cpu, rq->rd->online);
6524 }
6525 spin_unlock_irqrestore(&rq->lock, flags);
6526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527#endif
6528 }
6529 return NOTIFY_OK;
6530}
6531
6532/* Register at highest priority so that task migration (migrate_all_tasks)
6533 * happens before everything else.
6534 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006535static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 .notifier_call = migration_call,
6537 .priority = 10
6538};
6539
Adrian Bunke6fe6642007-11-09 22:39:39 +01006540void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541{
6542 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006543 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006544
6545 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006546 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6547 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6549 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550}
6551#endif
6552
6553#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006554
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006555#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006556
Mike Travis7c16ec52008-04-04 18:11:11 -07006557static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6558 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006559{
6560 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006561 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006562
Mike Travis434d53b2008-04-04 18:11:04 -07006563 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006564 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006565
6566 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6567
6568 if (!(sd->flags & SD_LOAD_BALANCE)) {
6569 printk("does not load-balance\n");
6570 if (sd->parent)
6571 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6572 " has parent");
6573 return -1;
6574 }
6575
6576 printk(KERN_CONT "span %s\n", str);
6577
6578 if (!cpu_isset(cpu, sd->span)) {
6579 printk(KERN_ERR "ERROR: domain->span does not contain "
6580 "CPU%d\n", cpu);
6581 }
6582 if (!cpu_isset(cpu, group->cpumask)) {
6583 printk(KERN_ERR "ERROR: domain->groups does not contain"
6584 " CPU%d\n", cpu);
6585 }
6586
6587 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6588 do {
6589 if (!group) {
6590 printk("\n");
6591 printk(KERN_ERR "ERROR: group is NULL\n");
6592 break;
6593 }
6594
6595 if (!group->__cpu_power) {
6596 printk(KERN_CONT "\n");
6597 printk(KERN_ERR "ERROR: domain->cpu_power not "
6598 "set\n");
6599 break;
6600 }
6601
6602 if (!cpus_weight(group->cpumask)) {
6603 printk(KERN_CONT "\n");
6604 printk(KERN_ERR "ERROR: empty group\n");
6605 break;
6606 }
6607
Mike Travis7c16ec52008-04-04 18:11:11 -07006608 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006609 printk(KERN_CONT "\n");
6610 printk(KERN_ERR "ERROR: repeated CPUs\n");
6611 break;
6612 }
6613
Mike Travis7c16ec52008-04-04 18:11:11 -07006614 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006615
Mike Travis434d53b2008-04-04 18:11:04 -07006616 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006617 printk(KERN_CONT " %s", str);
6618
6619 group = group->next;
6620 } while (group != sd->groups);
6621 printk(KERN_CONT "\n");
6622
Mike Travis7c16ec52008-04-04 18:11:11 -07006623 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006624 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6625
Mike Travis7c16ec52008-04-04 18:11:11 -07006626 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006627 printk(KERN_ERR "ERROR: parent span is not a superset "
6628 "of domain->span\n");
6629 return 0;
6630}
6631
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632static void sched_domain_debug(struct sched_domain *sd, int cpu)
6633{
Mike Travis7c16ec52008-04-04 18:11:11 -07006634 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 int level = 0;
6636
Nick Piggin41c7ce92005-06-25 14:57:24 -07006637 if (!sd) {
6638 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6639 return;
6640 }
6641
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6643
Mike Travis7c16ec52008-04-04 18:11:11 -07006644 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6645 if (!groupmask) {
6646 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6647 return;
6648 }
6649
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006650 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006651 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 level++;
6654 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006655 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006656 break;
6657 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006658 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659}
6660#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006661# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662#endif
6663
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006664static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006665{
6666 if (cpus_weight(sd->span) == 1)
6667 return 1;
6668
6669 /* Following flags need at least 2 groups */
6670 if (sd->flags & (SD_LOAD_BALANCE |
6671 SD_BALANCE_NEWIDLE |
6672 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006673 SD_BALANCE_EXEC |
6674 SD_SHARE_CPUPOWER |
6675 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006676 if (sd->groups != sd->groups->next)
6677 return 0;
6678 }
6679
6680 /* Following flags don't use groups */
6681 if (sd->flags & (SD_WAKE_IDLE |
6682 SD_WAKE_AFFINE |
6683 SD_WAKE_BALANCE))
6684 return 0;
6685
6686 return 1;
6687}
6688
Ingo Molnar48f24c42006-07-03 00:25:40 -07006689static int
6690sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006691{
6692 unsigned long cflags = sd->flags, pflags = parent->flags;
6693
6694 if (sd_degenerate(parent))
6695 return 1;
6696
6697 if (!cpus_equal(sd->span, parent->span))
6698 return 0;
6699
6700 /* Does parent contain flags not in child? */
6701 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6702 if (cflags & SD_WAKE_AFFINE)
6703 pflags &= ~SD_WAKE_BALANCE;
6704 /* Flags needing groups don't count if only 1 group in parent */
6705 if (parent->groups == parent->groups->next) {
6706 pflags &= ~(SD_LOAD_BALANCE |
6707 SD_BALANCE_NEWIDLE |
6708 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006709 SD_BALANCE_EXEC |
6710 SD_SHARE_CPUPOWER |
6711 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006712 }
6713 if (~cflags & pflags)
6714 return 0;
6715
6716 return 1;
6717}
6718
Gregory Haskins57d885f2008-01-25 21:08:18 +01006719static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6720{
6721 unsigned long flags;
6722 const struct sched_class *class;
6723
6724 spin_lock_irqsave(&rq->lock, flags);
6725
6726 if (rq->rd) {
6727 struct root_domain *old_rd = rq->rd;
6728
Ingo Molnar0eab9142008-01-25 21:08:19 +01006729 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006730 if (class->leave_domain)
6731 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006732 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006733
Gregory Haskinsdc938522008-01-25 21:08:26 +01006734 cpu_clear(rq->cpu, old_rd->span);
6735 cpu_clear(rq->cpu, old_rd->online);
6736
Gregory Haskins57d885f2008-01-25 21:08:18 +01006737 if (atomic_dec_and_test(&old_rd->refcount))
6738 kfree(old_rd);
6739 }
6740
6741 atomic_inc(&rd->refcount);
6742 rq->rd = rd;
6743
Gregory Haskinsdc938522008-01-25 21:08:26 +01006744 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006745 if (cpu_isset(rq->cpu, cpu_online_map))
6746 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006747
Ingo Molnar0eab9142008-01-25 21:08:19 +01006748 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006749 if (class->join_domain)
6750 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006751 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006752
6753 spin_unlock_irqrestore(&rq->lock, flags);
6754}
6755
Gregory Haskinsdc938522008-01-25 21:08:26 +01006756static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006757{
6758 memset(rd, 0, sizeof(*rd));
6759
Gregory Haskinsdc938522008-01-25 21:08:26 +01006760 cpus_clear(rd->span);
6761 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006762}
6763
6764static void init_defrootdomain(void)
6765{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006766 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006767 atomic_set(&def_root_domain.refcount, 1);
6768}
6769
Gregory Haskinsdc938522008-01-25 21:08:26 +01006770static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006771{
6772 struct root_domain *rd;
6773
6774 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6775 if (!rd)
6776 return NULL;
6777
Gregory Haskinsdc938522008-01-25 21:08:26 +01006778 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006779
6780 return rd;
6781}
6782
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006784 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 * hold the hotplug lock.
6786 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006787static void
6788cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006790 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006791 struct sched_domain *tmp;
6792
6793 /* Remove the sched domains which do not contribute to scheduling. */
6794 for (tmp = sd; tmp; tmp = tmp->parent) {
6795 struct sched_domain *parent = tmp->parent;
6796 if (!parent)
6797 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006798 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006799 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006800 if (parent->parent)
6801 parent->parent->child = tmp;
6802 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006803 }
6804
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006805 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006806 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006807 if (sd)
6808 sd->child = NULL;
6809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810
6811 sched_domain_debug(sd, cpu);
6812
Gregory Haskins57d885f2008-01-25 21:08:18 +01006813 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006814 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815}
6816
6817/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006818static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819
6820/* Setup the mask of cpus configured for isolated domains */
6821static int __init isolated_cpu_setup(char *str)
6822{
6823 int ints[NR_CPUS], i;
6824
6825 str = get_options(str, ARRAY_SIZE(ints), ints);
6826 cpus_clear(cpu_isolated_map);
6827 for (i = 1; i <= ints[0]; i++)
6828 if (ints[i] < NR_CPUS)
6829 cpu_set(ints[i], cpu_isolated_map);
6830 return 1;
6831}
6832
Ingo Molnar8927f492007-10-15 17:00:13 +02006833__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834
6835/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006836 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6837 * to a function which identifies what group(along with sched group) a CPU
6838 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6839 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 *
6841 * init_sched_build_groups will build a circular linked list of the groups
6842 * covered by the given span, and will set each group's ->cpumask correctly,
6843 * and ->cpu_power to 0.
6844 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006845static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006846init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006847 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006848 struct sched_group **sg,
6849 cpumask_t *tmpmask),
6850 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851{
6852 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 int i;
6854
Mike Travis7c16ec52008-04-04 18:11:11 -07006855 cpus_clear(*covered);
6856
6857 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006858 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006859 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 int j;
6861
Mike Travis7c16ec52008-04-04 18:11:11 -07006862 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 continue;
6864
Mike Travis7c16ec52008-04-04 18:11:11 -07006865 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006866 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867
Mike Travis7c16ec52008-04-04 18:11:11 -07006868 for_each_cpu_mask(j, *span) {
6869 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 continue;
6871
Mike Travis7c16ec52008-04-04 18:11:11 -07006872 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 cpu_set(j, sg->cpumask);
6874 }
6875 if (!first)
6876 first = sg;
6877 if (last)
6878 last->next = sg;
6879 last = sg;
6880 }
6881 last->next = first;
6882}
6883
John Hawkes9c1cfda2005-09-06 15:18:14 -07006884#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885
John Hawkes9c1cfda2005-09-06 15:18:14 -07006886#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006887
John Hawkes9c1cfda2005-09-06 15:18:14 -07006888/**
6889 * find_next_best_node - find the next node to include in a sched_domain
6890 * @node: node whose sched_domain we're building
6891 * @used_nodes: nodes already in the sched_domain
6892 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006893 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006894 * finds the closest node not already in the @used_nodes map.
6895 *
6896 * Should use nodemask_t.
6897 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006898static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006899{
6900 int i, n, val, min_val, best_node = 0;
6901
6902 min_val = INT_MAX;
6903
6904 for (i = 0; i < MAX_NUMNODES; i++) {
6905 /* Start at @node */
6906 n = (node + i) % MAX_NUMNODES;
6907
6908 if (!nr_cpus_node(n))
6909 continue;
6910
6911 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006912 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006913 continue;
6914
6915 /* Simple min distance search */
6916 val = node_distance(node, n);
6917
6918 if (val < min_val) {
6919 min_val = val;
6920 best_node = n;
6921 }
6922 }
6923
Mike Travisc5f59f02008-04-04 18:11:10 -07006924 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006925 return best_node;
6926}
6927
6928/**
6929 * sched_domain_node_span - get a cpumask for a node's sched_domain
6930 * @node: node whose cpumask we're constructing
John Hawkes9c1cfda2005-09-06 15:18:14 -07006931 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006932 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006933 * should be one that prevents unnecessary balancing, but also spreads tasks
6934 * out optimally.
6935 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07006936static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006937{
Mike Travisc5f59f02008-04-04 18:11:10 -07006938 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07006939 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006940 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006941
Mike Travis4bdbaad32008-04-15 16:35:52 -07006942 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07006943 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006944
Mike Travis4bdbaad32008-04-15 16:35:52 -07006945 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07006946 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006947
6948 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07006949 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006950
Mike Travisc5f59f02008-04-04 18:11:10 -07006951 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07006952 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006953 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006954}
6955#endif
6956
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006957int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006958
John Hawkes9c1cfda2005-09-06 15:18:14 -07006959/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006960 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962#ifdef CONFIG_SCHED_SMT
6963static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006964static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006965
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006966static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006967cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6968 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006970 if (sg)
6971 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 return cpu;
6973}
6974#endif
6975
Ingo Molnar48f24c42006-07-03 00:25:40 -07006976/*
6977 * multi-core sched-domains:
6978 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006979#ifdef CONFIG_SCHED_MC
6980static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006981static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006982#endif
6983
6984#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006985static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006986cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6987 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006988{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006989 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07006990
6991 *mask = per_cpu(cpu_sibling_map, cpu);
6992 cpus_and(*mask, *mask, *cpu_map);
6993 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006994 if (sg)
6995 *sg = &per_cpu(sched_group_core, group);
6996 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006997}
6998#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006999static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007000cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7001 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007002{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007003 if (sg)
7004 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007005 return cpu;
7006}
7007#endif
7008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007010static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007011
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007012static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007013cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7014 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007016 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007017#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007018 *mask = cpu_coregroup_map(cpu);
7019 cpus_and(*mask, *mask, *cpu_map);
7020 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007021#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007022 *mask = per_cpu(cpu_sibling_map, cpu);
7023 cpus_and(*mask, *mask, *cpu_map);
7024 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007026 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007028 if (sg)
7029 *sg = &per_cpu(sched_group_phys, group);
7030 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031}
7032
7033#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007034/*
7035 * The init_sched_build_groups can't handle what we want to do with node
7036 * groups, so roll our own. Now each node has its own list of groups which
7037 * gets dynamically allocated.
7038 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007040static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007041
7042static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007043static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007044
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007045static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007046 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007048 int group;
7049
Mike Travis7c16ec52008-04-04 18:11:11 -07007050 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7051 cpus_and(*nodemask, *nodemask, *cpu_map);
7052 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007053
7054 if (sg)
7055 *sg = &per_cpu(sched_group_allnodes, group);
7056 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007058
Siddha, Suresh B08069032006-03-27 01:15:23 -08007059static void init_numa_sched_groups_power(struct sched_group *group_head)
7060{
7061 struct sched_group *sg = group_head;
7062 int j;
7063
7064 if (!sg)
7065 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007066 do {
7067 for_each_cpu_mask(j, sg->cpumask) {
7068 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007069
Andi Kleen3a5c3592007-10-15 17:00:14 +02007070 sd = &per_cpu(phys_domains, j);
7071 if (j != first_cpu(sd->groups->cpumask)) {
7072 /*
7073 * Only add "power" once for each
7074 * physical package.
7075 */
7076 continue;
7077 }
7078
7079 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007080 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007081 sg = sg->next;
7082 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084#endif
7085
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007086#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007087/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007088static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007089{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007090 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007091
7092 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007093 struct sched_group **sched_group_nodes
7094 = sched_group_nodes_bycpu[cpu];
7095
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007096 if (!sched_group_nodes)
7097 continue;
7098
7099 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007100 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7101
Mike Travis7c16ec52008-04-04 18:11:11 -07007102 *nodemask = node_to_cpumask(i);
7103 cpus_and(*nodemask, *nodemask, *cpu_map);
7104 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007105 continue;
7106
7107 if (sg == NULL)
7108 continue;
7109 sg = sg->next;
7110next_sg:
7111 oldsg = sg;
7112 sg = sg->next;
7113 kfree(oldsg);
7114 if (oldsg != sched_group_nodes[i])
7115 goto next_sg;
7116 }
7117 kfree(sched_group_nodes);
7118 sched_group_nodes_bycpu[cpu] = NULL;
7119 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007120}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007121#else
Mike Travis7c16ec52008-04-04 18:11:11 -07007122static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007123{
7124}
7125#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007126
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007128 * Initialize sched groups cpu_power.
7129 *
7130 * cpu_power indicates the capacity of sched group, which is used while
7131 * distributing the load between different sched groups in a sched domain.
7132 * Typically cpu_power for all the groups in a sched domain will be same unless
7133 * there are asymmetries in the topology. If there are asymmetries, group
7134 * having more cpu_power will pickup more load compared to the group having
7135 * less cpu_power.
7136 *
7137 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7138 * the maximum number of tasks a group can handle in the presence of other idle
7139 * or lightly loaded groups in the same sched domain.
7140 */
7141static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7142{
7143 struct sched_domain *child;
7144 struct sched_group *group;
7145
7146 WARN_ON(!sd || !sd->groups);
7147
7148 if (cpu != first_cpu(sd->groups->cpumask))
7149 return;
7150
7151 child = sd->child;
7152
Eric Dumazet5517d862007-05-08 00:32:57 -07007153 sd->groups->__cpu_power = 0;
7154
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007155 /*
7156 * For perf policy, if the groups in child domain share resources
7157 * (for example cores sharing some portions of the cache hierarchy
7158 * or SMT), then set this domain groups cpu_power such that each group
7159 * can handle only one task, when there are other idle groups in the
7160 * same sched domain.
7161 */
7162 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7163 (child->flags &
7164 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007165 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007166 return;
7167 }
7168
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007169 /*
7170 * add cpu_power of each child group to this groups cpu_power
7171 */
7172 group = child->groups;
7173 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007174 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007175 group = group->next;
7176 } while (group != child->groups);
7177}
7178
7179/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007180 * Initializers for schedule domains
7181 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7182 */
7183
7184#define SD_INIT(sd, type) sd_init_##type(sd)
7185#define SD_INIT_FUNC(type) \
7186static noinline void sd_init_##type(struct sched_domain *sd) \
7187{ \
7188 memset(sd, 0, sizeof(*sd)); \
7189 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007190 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007191}
7192
7193SD_INIT_FUNC(CPU)
7194#ifdef CONFIG_NUMA
7195 SD_INIT_FUNC(ALLNODES)
7196 SD_INIT_FUNC(NODE)
7197#endif
7198#ifdef CONFIG_SCHED_SMT
7199 SD_INIT_FUNC(SIBLING)
7200#endif
7201#ifdef CONFIG_SCHED_MC
7202 SD_INIT_FUNC(MC)
7203#endif
7204
7205/*
7206 * To minimize stack usage kmalloc room for cpumasks and share the
7207 * space as the usage in build_sched_domains() dictates. Used only
7208 * if the amount of space is significant.
7209 */
7210struct allmasks {
7211 cpumask_t tmpmask; /* make this one first */
7212 union {
7213 cpumask_t nodemask;
7214 cpumask_t this_sibling_map;
7215 cpumask_t this_core_map;
7216 };
7217 cpumask_t send_covered;
7218
7219#ifdef CONFIG_NUMA
7220 cpumask_t domainspan;
7221 cpumask_t covered;
7222 cpumask_t notcovered;
7223#endif
7224};
7225
7226#if NR_CPUS > 128
7227#define SCHED_CPUMASK_ALLOC 1
7228#define SCHED_CPUMASK_FREE(v) kfree(v)
7229#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7230#else
7231#define SCHED_CPUMASK_ALLOC 0
7232#define SCHED_CPUMASK_FREE(v)
7233#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7234#endif
7235
7236#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7237 ((unsigned long)(a) + offsetof(struct allmasks, v))
7238
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007239static int default_relax_domain_level = -1;
7240
7241static int __init setup_relax_domain_level(char *str)
7242{
7243 default_relax_domain_level = simple_strtoul(str, NULL, 0);
7244 return 1;
7245}
7246__setup("relax_domain_level=", setup_relax_domain_level);
7247
7248static void set_domain_attribute(struct sched_domain *sd,
7249 struct sched_domain_attr *attr)
7250{
7251 int request;
7252
7253 if (!attr || attr->relax_domain_level < 0) {
7254 if (default_relax_domain_level < 0)
7255 return;
7256 else
7257 request = default_relax_domain_level;
7258 } else
7259 request = attr->relax_domain_level;
7260 if (request < sd->level) {
7261 /* turn off idle balance on this domain */
7262 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7263 } else {
7264 /* turn on idle balance on this domain */
7265 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7266 }
7267}
7268
Mike Travis7c16ec52008-04-04 18:11:11 -07007269/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007270 * Build sched domains for a given set of cpus and attach the sched domains
7271 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007273static int __build_sched_domains(const cpumask_t *cpu_map,
7274 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275{
7276 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007277 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007278 SCHED_CPUMASK_DECLARE(allmasks);
7279 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007280#ifdef CONFIG_NUMA
7281 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007282 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007283
7284 /*
7285 * Allocate the per-node list of sched groups
7286 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007287 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007288 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007289 if (!sched_group_nodes) {
7290 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007291 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007292 }
John Hawkesd1b55132005-09-06 15:18:14 -07007293#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294
Gregory Haskinsdc938522008-01-25 21:08:26 +01007295 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007296 if (!rd) {
7297 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007298#ifdef CONFIG_NUMA
7299 kfree(sched_group_nodes);
7300#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007301 return -ENOMEM;
7302 }
7303
Mike Travis7c16ec52008-04-04 18:11:11 -07007304#if SCHED_CPUMASK_ALLOC
7305 /* get space for all scratch cpumask variables */
7306 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7307 if (!allmasks) {
7308 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7309 kfree(rd);
7310#ifdef CONFIG_NUMA
7311 kfree(sched_group_nodes);
7312#endif
7313 return -ENOMEM;
7314 }
7315#endif
7316 tmpmask = (cpumask_t *)allmasks;
7317
7318
7319#ifdef CONFIG_NUMA
7320 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7321#endif
7322
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007324 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007326 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007328 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329
Mike Travis7c16ec52008-04-04 18:11:11 -07007330 *nodemask = node_to_cpumask(cpu_to_node(i));
7331 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332
7333#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007334 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007335 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007336 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007337 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007338 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007339 sd->span = *cpu_map;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007340 sd->first_cpu = first_cpu(sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07007341 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007342 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007343 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007344 } else
7345 p = NULL;
7346
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007348 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007349 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007350 sched_domain_node_span(cpu_to_node(i), &sd->span);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007351 sd->first_cpu = first_cpu(sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007352 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007353 if (p)
7354 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007355 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356#endif
7357
7358 p = sd;
7359 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007360 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007361 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007362 sd->span = *nodemask;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007363 sd->first_cpu = first_cpu(sd->span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007365 if (p)
7366 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007367 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007369#ifdef CONFIG_SCHED_MC
7370 p = sd;
7371 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007372 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007373 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007374 sd->span = cpu_coregroup_map(i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007375 sd->first_cpu = first_cpu(sd->span);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007376 cpus_and(sd->span, sd->span, *cpu_map);
7377 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007378 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007379 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007380#endif
7381
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382#ifdef CONFIG_SCHED_SMT
7383 p = sd;
7384 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007385 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007386 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007387 sd->span = per_cpu(cpu_sibling_map, i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007388 sd->first_cpu = first_cpu(sd->span);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007389 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007391 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007392 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393#endif
7394 }
7395
7396#ifdef CONFIG_SCHED_SMT
7397 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007398 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007399 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7400 SCHED_CPUMASK_VAR(send_covered, allmasks);
7401
7402 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7403 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7404 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 continue;
7406
Ingo Molnardd41f592007-07-09 18:51:59 +02007407 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007408 &cpu_to_cpu_group,
7409 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 }
7411#endif
7412
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007413#ifdef CONFIG_SCHED_MC
7414 /* Set up multi-core groups */
7415 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007416 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7417 SCHED_CPUMASK_VAR(send_covered, allmasks);
7418
7419 *this_core_map = cpu_coregroup_map(i);
7420 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7421 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007422 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007423
Ingo Molnardd41f592007-07-09 18:51:59 +02007424 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007425 &cpu_to_core_group,
7426 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007427 }
7428#endif
7429
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430 /* Set up physical groups */
7431 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007432 SCHED_CPUMASK_VAR(nodemask, allmasks);
7433 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434
Mike Travis7c16ec52008-04-04 18:11:11 -07007435 *nodemask = node_to_cpumask(i);
7436 cpus_and(*nodemask, *nodemask, *cpu_map);
7437 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 continue;
7439
Mike Travis7c16ec52008-04-04 18:11:11 -07007440 init_sched_build_groups(nodemask, cpu_map,
7441 &cpu_to_phys_group,
7442 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 }
7444
7445#ifdef CONFIG_NUMA
7446 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007447 if (sd_allnodes) {
7448 SCHED_CPUMASK_VAR(send_covered, allmasks);
7449
7450 init_sched_build_groups(cpu_map, cpu_map,
7451 &cpu_to_allnodes_group,
7452 send_covered, tmpmask);
7453 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007454
7455 for (i = 0; i < MAX_NUMNODES; i++) {
7456 /* Set up node groups */
7457 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007458 SCHED_CPUMASK_VAR(nodemask, allmasks);
7459 SCHED_CPUMASK_VAR(domainspan, allmasks);
7460 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007461 int j;
7462
Mike Travis7c16ec52008-04-04 18:11:11 -07007463 *nodemask = node_to_cpumask(i);
7464 cpus_clear(*covered);
7465
7466 cpus_and(*nodemask, *nodemask, *cpu_map);
7467 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007468 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007469 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007470 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007471
Mike Travis4bdbaad32008-04-15 16:35:52 -07007472 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007473 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007474
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007475 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007476 if (!sg) {
7477 printk(KERN_WARNING "Can not alloc domain group for "
7478 "node %d\n", i);
7479 goto error;
7480 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007481 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007482 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007483 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007484
John Hawkes9c1cfda2005-09-06 15:18:14 -07007485 sd = &per_cpu(node_domains, j);
7486 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007487 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007488 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007489 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007490 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007491 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007492 prev = sg;
7493
7494 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007495 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007496 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007497 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007498
Mike Travis7c16ec52008-04-04 18:11:11 -07007499 cpus_complement(*notcovered, *covered);
7500 cpus_and(*tmpmask, *notcovered, *cpu_map);
7501 cpus_and(*tmpmask, *tmpmask, *domainspan);
7502 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007503 break;
7504
Mike Travis7c16ec52008-04-04 18:11:11 -07007505 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7506 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007507 continue;
7508
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007509 sg = kmalloc_node(sizeof(struct sched_group),
7510 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007511 if (!sg) {
7512 printk(KERN_WARNING
7513 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007514 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007515 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007516 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007517 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007518 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007519 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007520 prev->next = sg;
7521 prev = sg;
7522 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524#endif
7525
7526 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007527#ifdef CONFIG_SCHED_SMT
7528 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007529 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7530
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007531 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007532 }
7533#endif
7534#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007535 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007536 struct sched_domain *sd = &per_cpu(core_domains, i);
7537
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007538 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007539 }
7540#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007542 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007543 struct sched_domain *sd = &per_cpu(phys_domains, i);
7544
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007545 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 }
7547
John Hawkes9c1cfda2005-09-06 15:18:14 -07007548#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007549 for (i = 0; i < MAX_NUMNODES; i++)
7550 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007551
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007552 if (sd_allnodes) {
7553 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007554
Mike Travis7c16ec52008-04-04 18:11:11 -07007555 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7556 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007557 init_numa_sched_groups_power(sg);
7558 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007559#endif
7560
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007562 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563 struct sched_domain *sd;
7564#ifdef CONFIG_SCHED_SMT
7565 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007566#elif defined(CONFIG_SCHED_MC)
7567 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568#else
7569 sd = &per_cpu(phys_domains, i);
7570#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007571 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007573
Mike Travis7c16ec52008-04-04 18:11:11 -07007574 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007575 return 0;
7576
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007577#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007578error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007579 free_sched_groups(cpu_map, tmpmask);
7580 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007581 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007582#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583}
Paul Jackson029190c2007-10-18 23:40:20 -07007584
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007585static int build_sched_domains(const cpumask_t *cpu_map)
7586{
7587 return __build_sched_domains(cpu_map, NULL);
7588}
7589
Paul Jackson029190c2007-10-18 23:40:20 -07007590static cpumask_t *doms_cur; /* current sched domains */
7591static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007592static struct sched_domain_attr *dattr_cur; /* attribues of custom domains
7593 in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007594
7595/*
7596 * Special case: If a kmalloc of a doms_cur partition (array of
7597 * cpumask_t) fails, then fallback to a single sched domain,
7598 * as determined by the single cpumask_t fallback_doms.
7599 */
7600static cpumask_t fallback_doms;
7601
Heiko Carstens22e52b02008-03-12 18:31:59 +01007602void __attribute__((weak)) arch_update_cpu_topology(void)
7603{
7604}
7605
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007606/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007607 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007608 * For now this just excludes isolated cpus, but could be used to
7609 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007610 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007611static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007612{
Milton Miller73785472007-10-24 18:23:48 +02007613 int err;
7614
Heiko Carstens22e52b02008-03-12 18:31:59 +01007615 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007616 ndoms_cur = 1;
7617 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7618 if (!doms_cur)
7619 doms_cur = &fallback_doms;
7620 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007621 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007622 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007623 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007624
7625 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007626}
7627
Mike Travis7c16ec52008-04-04 18:11:11 -07007628static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7629 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630{
Mike Travis7c16ec52008-04-04 18:11:11 -07007631 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007632}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007634/*
7635 * Detach sched domains from a group of cpus specified in cpu_map
7636 * These cpus will now be attached to the NULL domain
7637 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007638static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007639{
Mike Travis7c16ec52008-04-04 18:11:11 -07007640 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007641 int i;
7642
Milton Miller6382bc92007-10-15 17:00:19 +02007643 unregister_sched_domain_sysctl();
7644
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007645 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007646 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007647 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007648 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007649}
7650
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007651/* handle null as "default" */
7652static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7653 struct sched_domain_attr *new, int idx_new)
7654{
7655 struct sched_domain_attr tmp;
7656
7657 /* fast path */
7658 if (!new && !cur)
7659 return 1;
7660
7661 tmp = SD_ATTR_INIT;
7662 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7663 new ? (new + idx_new) : &tmp,
7664 sizeof(struct sched_domain_attr));
7665}
7666
Paul Jackson029190c2007-10-18 23:40:20 -07007667/*
7668 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007669 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007670 * doms_new[] to the current sched domain partitioning, doms_cur[].
7671 * It destroys each deleted domain and builds each new domain.
7672 *
7673 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007674 * The masks don't intersect (don't overlap.) We should setup one
7675 * sched domain for each mask. CPUs not in any of the cpumasks will
7676 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007677 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7678 * it as it is.
7679 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007680 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7681 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007682 * failed the kmalloc call, then it can pass in doms_new == NULL,
7683 * and partition_sched_domains() will fallback to the single partition
7684 * 'fallback_doms'.
7685 *
7686 * Call with hotplug lock held
7687 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007688void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7689 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007690{
7691 int i, j;
7692
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007693 lock_doms_cur();
7694
Milton Miller73785472007-10-24 18:23:48 +02007695 /* always unregister in case we don't destroy any domains */
7696 unregister_sched_domain_sysctl();
7697
Paul Jackson029190c2007-10-18 23:40:20 -07007698 if (doms_new == NULL) {
7699 ndoms_new = 1;
7700 doms_new = &fallback_doms;
7701 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007702 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007703 }
7704
7705 /* Destroy deleted domains */
7706 for (i = 0; i < ndoms_cur; i++) {
7707 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007708 if (cpus_equal(doms_cur[i], doms_new[j])
7709 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007710 goto match1;
7711 }
7712 /* no match - a current sched domain not in new doms_new[] */
7713 detach_destroy_domains(doms_cur + i);
7714match1:
7715 ;
7716 }
7717
7718 /* Build new domains */
7719 for (i = 0; i < ndoms_new; i++) {
7720 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007721 if (cpus_equal(doms_new[i], doms_cur[j])
7722 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007723 goto match2;
7724 }
7725 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007726 __build_sched_domains(doms_new + i,
7727 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007728match2:
7729 ;
7730 }
7731
7732 /* Remember the new sched domains */
7733 if (doms_cur != &fallback_doms)
7734 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007735 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007736 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007737 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007738 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007739
7740 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007741
7742 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007743}
7744
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007745#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007746int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007747{
7748 int err;
7749
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007750 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007751 detach_destroy_domains(&cpu_online_map);
7752 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007753 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007754
7755 return err;
7756}
7757
7758static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7759{
7760 int ret;
7761
7762 if (buf[0] != '0' && buf[0] != '1')
7763 return -EINVAL;
7764
7765 if (smt)
7766 sched_smt_power_savings = (buf[0] == '1');
7767 else
7768 sched_mc_power_savings = (buf[0] == '1');
7769
7770 ret = arch_reinit_sched_domains();
7771
7772 return ret ? ret : count;
7773}
7774
Adrian Bunk6707de002007-08-12 18:08:19 +02007775#ifdef CONFIG_SCHED_MC
7776static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7777{
7778 return sprintf(page, "%u\n", sched_mc_power_savings);
7779}
7780static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7781 const char *buf, size_t count)
7782{
7783 return sched_power_savings_store(buf, count, 0);
7784}
7785static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7786 sched_mc_power_savings_store);
7787#endif
7788
7789#ifdef CONFIG_SCHED_SMT
7790static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7791{
7792 return sprintf(page, "%u\n", sched_smt_power_savings);
7793}
7794static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7795 const char *buf, size_t count)
7796{
7797 return sched_power_savings_store(buf, count, 1);
7798}
7799static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7800 sched_smt_power_savings_store);
7801#endif
7802
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007803int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7804{
7805 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007806
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007807#ifdef CONFIG_SCHED_SMT
7808 if (smt_capable())
7809 err = sysfs_create_file(&cls->kset.kobj,
7810 &attr_sched_smt_power_savings.attr);
7811#endif
7812#ifdef CONFIG_SCHED_MC
7813 if (!err && mc_capable())
7814 err = sysfs_create_file(&cls->kset.kobj,
7815 &attr_sched_mc_power_savings.attr);
7816#endif
7817 return err;
7818}
7819#endif
7820
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007822 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007824 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825 * which will prevent rebalancing while the sched domains are recalculated.
7826 */
7827static int update_sched_domains(struct notifier_block *nfb,
7828 unsigned long action, void *hcpu)
7829{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830 switch (action) {
7831 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007832 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007834 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007835 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836 return NOTIFY_OK;
7837
7838 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007839 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007841 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007843 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007845 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846 /*
7847 * Fall through and re-initialise the domains.
7848 */
7849 break;
7850 default:
7851 return NOTIFY_DONE;
7852 }
7853
7854 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007855 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856
7857 return NOTIFY_OK;
7858}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859
7860void __init sched_init_smp(void)
7861{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007862 cpumask_t non_isolated_cpus;
7863
Mike Travis434d53b2008-04-04 18:11:04 -07007864#if defined(CONFIG_NUMA)
7865 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7866 GFP_KERNEL);
7867 BUG_ON(sched_group_nodes_bycpu == NULL);
7868#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007869 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007870 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007871 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007872 if (cpus_empty(non_isolated_cpus))
7873 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007874 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 /* XXX: Theoretical race here - CPU may be hotplugged now */
7876 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007877
7878 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07007879 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007880 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007881 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882}
7883#else
7884void __init sched_init_smp(void)
7885{
Mike Travis434d53b2008-04-04 18:11:04 -07007886#if defined(CONFIG_NUMA)
7887 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7888 GFP_KERNEL);
7889 BUG_ON(sched_group_nodes_bycpu == NULL);
7890#endif
Ingo Molnar19978ca2007-11-09 22:39:38 +01007891 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892}
7893#endif /* CONFIG_SMP */
7894
7895int in_sched_functions(unsigned long addr)
7896{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897 return in_lock_functions(addr) ||
7898 (addr >= (unsigned long)__sched_text_start
7899 && addr < (unsigned long)__sched_text_end);
7900}
7901
Alexey Dobriyana9957442007-10-15 17:00:13 +02007902static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007903{
7904 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007905 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02007906#ifdef CONFIG_FAIR_GROUP_SCHED
7907 cfs_rq->rq = rq;
7908#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007909 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007910}
7911
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007912static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7913{
7914 struct rt_prio_array *array;
7915 int i;
7916
7917 array = &rt_rq->active;
7918 for (i = 0; i < MAX_RT_PRIO; i++) {
7919 INIT_LIST_HEAD(array->queue + i);
7920 __clear_bit(i, array->bitmap);
7921 }
7922 /* delimiter for bitsearch: */
7923 __set_bit(MAX_RT_PRIO, array->bitmap);
7924
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007925#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007926 rt_rq->highest_prio = MAX_RT_PRIO;
7927#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007928#ifdef CONFIG_SMP
7929 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007930 rt_rq->overloaded = 0;
7931#endif
7932
7933 rt_rq->rt_time = 0;
7934 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007935 rt_rq->rt_runtime = 0;
7936 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007937
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007938#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007939 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007940 rt_rq->rq = rq;
7941#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007942}
7943
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007944#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007945static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7946 struct sched_entity *se, int cpu, int add,
7947 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007948{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007949 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007950 tg->cfs_rq[cpu] = cfs_rq;
7951 init_cfs_rq(cfs_rq, rq);
7952 cfs_rq->tg = tg;
7953 if (add)
7954 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7955
7956 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007957 /* se could be NULL for init_task_group */
7958 if (!se)
7959 return;
7960
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007961 if (!parent)
7962 se->cfs_rq = &rq->cfs;
7963 else
7964 se->cfs_rq = parent->my_q;
7965
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007966 se->my_q = cfs_rq;
7967 se->load.weight = tg->shares;
7968 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007969 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007970}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007971#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007972
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007973#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007974static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
7975 struct sched_rt_entity *rt_se, int cpu, int add,
7976 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007977{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007978 struct rq *rq = cpu_rq(cpu);
7979
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007980 tg->rt_rq[cpu] = rt_rq;
7981 init_rt_rq(rt_rq, rq);
7982 rt_rq->tg = tg;
7983 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007984 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007985 if (add)
7986 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7987
7988 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007989 if (!rt_se)
7990 return;
7991
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007992 if (!parent)
7993 rt_se->rt_rq = &rq->rt;
7994 else
7995 rt_se->rt_rq = parent->my_q;
7996
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007997 rt_se->rt_rq = &rq->rt;
7998 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007999 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008000 INIT_LIST_HEAD(&rt_se->run_list);
8001}
8002#endif
8003
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004void __init sched_init(void)
8005{
Ingo Molnardd41f592007-07-09 18:51:59 +02008006 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008007 unsigned long alloc_size = 0, ptr;
8008
8009#ifdef CONFIG_FAIR_GROUP_SCHED
8010 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8011#endif
8012#ifdef CONFIG_RT_GROUP_SCHED
8013 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8014#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008015#ifdef CONFIG_USER_SCHED
8016 alloc_size *= 2;
8017#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008018 /*
8019 * As sched_init() is called before page_alloc is setup,
8020 * we use alloc_bootmem().
8021 */
8022 if (alloc_size) {
8023 ptr = (unsigned long)alloc_bootmem_low(alloc_size);
8024
8025#ifdef CONFIG_FAIR_GROUP_SCHED
8026 init_task_group.se = (struct sched_entity **)ptr;
8027 ptr += nr_cpu_ids * sizeof(void **);
8028
8029 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8030 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008031
8032#ifdef CONFIG_USER_SCHED
8033 root_task_group.se = (struct sched_entity **)ptr;
8034 ptr += nr_cpu_ids * sizeof(void **);
8035
8036 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8037 ptr += nr_cpu_ids * sizeof(void **);
8038#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008039#endif
8040#ifdef CONFIG_RT_GROUP_SCHED
8041 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8042 ptr += nr_cpu_ids * sizeof(void **);
8043
8044 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008045 ptr += nr_cpu_ids * sizeof(void **);
8046
8047#ifdef CONFIG_USER_SCHED
8048 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8049 ptr += nr_cpu_ids * sizeof(void **);
8050
8051 root_task_group.rt_rq = (struct rt_rq **)ptr;
8052 ptr += nr_cpu_ids * sizeof(void **);
8053#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008054#endif
8055 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008056
Gregory Haskins57d885f2008-01-25 21:08:18 +01008057#ifdef CONFIG_SMP
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008058 init_aggregate();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008059 init_defrootdomain();
8060#endif
8061
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008062 init_rt_bandwidth(&def_rt_bandwidth,
8063 global_rt_period(), global_rt_runtime());
8064
8065#ifdef CONFIG_RT_GROUP_SCHED
8066 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8067 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008068#ifdef CONFIG_USER_SCHED
8069 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8070 global_rt_period(), RUNTIME_INF);
8071#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008072#endif
8073
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008074#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008075 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008076 INIT_LIST_HEAD(&init_task_group.children);
8077
8078#ifdef CONFIG_USER_SCHED
8079 INIT_LIST_HEAD(&root_task_group.children);
8080 init_task_group.parent = &root_task_group;
8081 list_add(&init_task_group.siblings, &root_task_group.children);
8082#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008083#endif
8084
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008085 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008086 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008087
8088 rq = cpu_rq(i);
8089 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07008090 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07008091 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008092 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02008093 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02008094 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008095 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008096#ifdef CONFIG_FAIR_GROUP_SCHED
8097 init_task_group.shares = init_task_group_load;
8098 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008099#ifdef CONFIG_CGROUP_SCHED
8100 /*
8101 * How much cpu bandwidth does init_task_group get?
8102 *
8103 * In case of task-groups formed thr' the cgroup filesystem, it
8104 * gets 100% of the cpu resources in the system. This overall
8105 * system cpu resource is divided among the tasks of
8106 * init_task_group and its child task-groups in a fair manner,
8107 * based on each entity's (task or task-group's) weight
8108 * (se->load.weight).
8109 *
8110 * In other words, if init_task_group has 10 tasks of weight
8111 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8112 * then A0's share of the cpu resource is:
8113 *
8114 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8115 *
8116 * We achieve this by letting init_task_group's tasks sit
8117 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8118 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008119 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008120#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008121 root_task_group.shares = NICE_0_LOAD;
8122 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008123 /*
8124 * In case of task-groups formed thr' the user id of tasks,
8125 * init_task_group represents tasks belonging to root user.
8126 * Hence it forms a sibling of all subsequent groups formed.
8127 * In this case, init_task_group gets only a fraction of overall
8128 * system cpu resource, based on the weight assigned to root
8129 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8130 * by letting tasks of init_task_group sit in a separate cfs_rq
8131 * (init_cfs_rq) and having one entity represent this group of
8132 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8133 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008134 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008135 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008136 &per_cpu(init_sched_entity, i), i, 1,
8137 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008138
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008139#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008140#endif /* CONFIG_FAIR_GROUP_SCHED */
8141
8142 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008143#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008144 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008145#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008146 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008147#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008148 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008149 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008150 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008151 &per_cpu(init_sched_rt_entity, i), i, 1,
8152 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008153#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155
Ingo Molnardd41f592007-07-09 18:51:59 +02008156 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8157 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008159 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008160 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008162 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008164 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165 rq->migration_thread = NULL;
8166 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008167 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008169 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008170 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171 }
8172
Peter Williams2dd73a42006-06-27 02:54:34 -07008173 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008174
Avi Kivitye107be32007-07-26 13:40:43 +02008175#ifdef CONFIG_PREEMPT_NOTIFIERS
8176 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8177#endif
8178
Christoph Lameterc9819f42006-12-10 02:20:25 -08008179#ifdef CONFIG_SMP
8180 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8181#endif
8182
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008183#ifdef CONFIG_RT_MUTEXES
8184 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8185#endif
8186
Linus Torvalds1da177e2005-04-16 15:20:36 -07008187 /*
8188 * The boot idle thread does lazy MMU switching as well:
8189 */
8190 atomic_inc(&init_mm.mm_count);
8191 enter_lazy_tlb(&init_mm, current);
8192
8193 /*
8194 * Make us the idle thread. Technically, schedule() should not be
8195 * called from this thread, however somewhere below it might be,
8196 * but because we are the idle thread, we just pick up running again
8197 * when this runqueue becomes "idle".
8198 */
8199 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008200 /*
8201 * During early bootup we pretend to be a normal task:
8202 */
8203 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008204
8205 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008206}
8207
8208#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8209void __might_sleep(char *file, int line)
8210{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008211#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212 static unsigned long prev_jiffy; /* ratelimiting */
8213
8214 if ((in_atomic() || irqs_disabled()) &&
8215 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8216 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8217 return;
8218 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008219 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008220 " context at %s:%d\n", file, line);
8221 printk("in_atomic():%d, irqs_disabled():%d\n",
8222 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008223 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008224 if (irqs_disabled())
8225 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008226 dump_stack();
8227 }
8228#endif
8229}
8230EXPORT_SYMBOL(__might_sleep);
8231#endif
8232
8233#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008234static void normalize_task(struct rq *rq, struct task_struct *p)
8235{
8236 int on_rq;
8237 update_rq_clock(rq);
8238 on_rq = p->se.on_rq;
8239 if (on_rq)
8240 deactivate_task(rq, p, 0);
8241 __setscheduler(rq, p, SCHED_NORMAL, 0);
8242 if (on_rq) {
8243 activate_task(rq, p, 0);
8244 resched_task(rq->curr);
8245 }
8246}
8247
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248void normalize_rt_tasks(void)
8249{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008250 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008251 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008252 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008254 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008255 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008256 /*
8257 * Only normalize user tasks:
8258 */
8259 if (!p->mm)
8260 continue;
8261
Ingo Molnardd41f592007-07-09 18:51:59 +02008262 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008263#ifdef CONFIG_SCHEDSTATS
8264 p->se.wait_start = 0;
8265 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008266 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008267#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008268 task_rq(p)->clock = 0;
8269
8270 if (!rt_task(p)) {
8271 /*
8272 * Renice negative nice level userspace
8273 * tasks back to 0:
8274 */
8275 if (TASK_NICE(p) < 0 && p->mm)
8276 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008280 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008281 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282
Ingo Molnar178be792007-10-15 17:00:18 +02008283 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008284
Ingo Molnarb29739f2006-06-27 02:54:51 -07008285 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008286 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008287 } while_each_thread(g, p);
8288
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008289 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008290}
8291
8292#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008293
8294#ifdef CONFIG_IA64
8295/*
8296 * These functions are only useful for the IA64 MCA handling.
8297 *
8298 * They can only be called when the whole system has been
8299 * stopped - every CPU needs to be quiescent, and no scheduling
8300 * activity can take place. Using them for anything else would
8301 * be a serious bug, and as a result, they aren't even visible
8302 * under any other configuration.
8303 */
8304
8305/**
8306 * curr_task - return the current task for a given cpu.
8307 * @cpu: the processor in question.
8308 *
8309 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8310 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008311struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008312{
8313 return cpu_curr(cpu);
8314}
8315
8316/**
8317 * set_curr_task - set the current task for a given cpu.
8318 * @cpu: the processor in question.
8319 * @p: the task pointer to set.
8320 *
8321 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008322 * are serviced on a separate stack. It allows the architecture to switch the
8323 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008324 * must be called with all CPU's synchronized, and interrupts disabled, the
8325 * and caller must save the original value of the current task (see
8326 * curr_task() above) and restore that value before reenabling interrupts and
8327 * re-starting the system.
8328 *
8329 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8330 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008331void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008332{
8333 cpu_curr(cpu) = p;
8334}
8335
8336#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008337
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008338#ifdef CONFIG_FAIR_GROUP_SCHED
8339static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008340{
8341 int i;
8342
8343 for_each_possible_cpu(i) {
8344 if (tg->cfs_rq)
8345 kfree(tg->cfs_rq[i]);
8346 if (tg->se)
8347 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008348 }
8349
8350 kfree(tg->cfs_rq);
8351 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008352}
8353
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008354static
8355int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008356{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008357 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008358 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008359 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008360 int i;
8361
Mike Travis434d53b2008-04-04 18:11:04 -07008362 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008363 if (!tg->cfs_rq)
8364 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008365 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008366 if (!tg->se)
8367 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008368
8369 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008370
8371 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008372 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008373
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008374 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8375 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008376 if (!cfs_rq)
8377 goto err;
8378
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008379 se = kmalloc_node(sizeof(struct sched_entity),
8380 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008381 if (!se)
8382 goto err;
8383
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008384 parent_se = parent ? parent->se[i] : NULL;
8385 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008386 }
8387
8388 return 1;
8389
8390 err:
8391 return 0;
8392}
8393
8394static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8395{
8396 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8397 &cpu_rq(cpu)->leaf_cfs_rq_list);
8398}
8399
8400static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8401{
8402 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8403}
8404#else
8405static inline void free_fair_sched_group(struct task_group *tg)
8406{
8407}
8408
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008409static inline
8410int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008411{
8412 return 1;
8413}
8414
8415static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8416{
8417}
8418
8419static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8420{
8421}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008422#endif
8423
8424#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008425static void free_rt_sched_group(struct task_group *tg)
8426{
8427 int i;
8428
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008429 destroy_rt_bandwidth(&tg->rt_bandwidth);
8430
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008431 for_each_possible_cpu(i) {
8432 if (tg->rt_rq)
8433 kfree(tg->rt_rq[i]);
8434 if (tg->rt_se)
8435 kfree(tg->rt_se[i]);
8436 }
8437
8438 kfree(tg->rt_rq);
8439 kfree(tg->rt_se);
8440}
8441
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008442static
8443int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008444{
8445 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008446 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008447 struct rq *rq;
8448 int i;
8449
Mike Travis434d53b2008-04-04 18:11:04 -07008450 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008451 if (!tg->rt_rq)
8452 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008453 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008454 if (!tg->rt_se)
8455 goto err;
8456
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008457 init_rt_bandwidth(&tg->rt_bandwidth,
8458 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008459
8460 for_each_possible_cpu(i) {
8461 rq = cpu_rq(i);
8462
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008463 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8464 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8465 if (!rt_rq)
8466 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008467
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008468 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8469 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8470 if (!rt_se)
8471 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008472
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008473 parent_se = parent ? parent->rt_se[i] : NULL;
8474 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008475 }
8476
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008477 return 1;
8478
8479 err:
8480 return 0;
8481}
8482
8483static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8484{
8485 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8486 &cpu_rq(cpu)->leaf_rt_rq_list);
8487}
8488
8489static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8490{
8491 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8492}
8493#else
8494static inline void free_rt_sched_group(struct task_group *tg)
8495{
8496}
8497
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008498static inline
8499int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008500{
8501 return 1;
8502}
8503
8504static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8505{
8506}
8507
8508static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8509{
8510}
8511#endif
8512
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008513#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008514static void free_sched_group(struct task_group *tg)
8515{
8516 free_fair_sched_group(tg);
8517 free_rt_sched_group(tg);
8518 kfree(tg);
8519}
8520
8521/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008522struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008523{
8524 struct task_group *tg;
8525 unsigned long flags;
8526 int i;
8527
8528 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8529 if (!tg)
8530 return ERR_PTR(-ENOMEM);
8531
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008532 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008533 goto err;
8534
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008535 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008536 goto err;
8537
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008538 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008539 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008540 register_fair_sched_group(tg, i);
8541 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008542 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008543 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008544
8545 WARN_ON(!parent); /* root should already exist */
8546
8547 tg->parent = parent;
8548 list_add_rcu(&tg->siblings, &parent->children);
8549 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008550 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008551
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008552 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008553
8554err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008555 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008556 return ERR_PTR(-ENOMEM);
8557}
8558
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008559/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008560static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008561{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008562 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008563 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008564}
8565
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008566/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008567void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008568{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008569 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008570 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008571
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008572 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008573 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008574 unregister_fair_sched_group(tg, i);
8575 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008576 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008577 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008578 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008579 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008580
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008581 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008582 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008583}
8584
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008585/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008586 * The caller of this function should have put the task in its new group
8587 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8588 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008589 */
8590void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008591{
8592 int on_rq, running;
8593 unsigned long flags;
8594 struct rq *rq;
8595
8596 rq = task_rq_lock(tsk, &flags);
8597
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008598 update_rq_clock(rq);
8599
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008600 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008601 on_rq = tsk->se.on_rq;
8602
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008603 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008604 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008605 if (unlikely(running))
8606 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008607
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008608 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008609
Peter Zijlstra810b3812008-02-29 15:21:01 -05008610#ifdef CONFIG_FAIR_GROUP_SCHED
8611 if (tsk->sched_class->moved_group)
8612 tsk->sched_class->moved_group(tsk);
8613#endif
8614
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008615 if (unlikely(running))
8616 tsk->sched_class->set_curr_task(rq);
8617 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008618 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008619
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008620 task_rq_unlock(rq, &flags);
8621}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008622#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008623
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008624#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008625static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008626{
8627 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008628 int on_rq;
8629
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008630 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008631 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008632 dequeue_entity(cfs_rq, se, 0);
8633
8634 se->load.weight = shares;
8635 se->load.inv_weight = div64_64((1ULL<<32), shares);
8636
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008637 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008638 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008639}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008640
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008641static void set_se_shares(struct sched_entity *se, unsigned long shares)
8642{
8643 struct cfs_rq *cfs_rq = se->cfs_rq;
8644 struct rq *rq = cfs_rq->rq;
8645 unsigned long flags;
8646
8647 spin_lock_irqsave(&rq->lock, flags);
8648 __set_se_shares(se, shares);
8649 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008650}
8651
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008652static DEFINE_MUTEX(shares_mutex);
8653
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008654int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008655{
8656 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008657 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008658
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008659 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008660 * We can't change the weight of the root cgroup.
8661 */
8662 if (!tg->se[0])
8663 return -EINVAL;
8664
8665 /*
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008666 * A weight of 0 or 1 can cause arithmetics problems.
8667 * (The default weight is 1024 - so there's no practical
8668 * limitation from this.)
8669 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008670 if (shares < MIN_SHARES)
8671 shares = MIN_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008672
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008673 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008674 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008675 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008676
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008677 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008678 for_each_possible_cpu(i)
8679 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008680 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008681 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008682
8683 /* wait for any ongoing reference to this group to finish */
8684 synchronize_sched();
8685
8686 /*
8687 * Now we are free to modify the group's share on each cpu
8688 * w/o tripping rebalance_share or load_balance_fair.
8689 */
8690 tg->shares = shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008691 for_each_possible_cpu(i) {
8692 /*
8693 * force a rebalance
8694 */
8695 cfs_rq_set_shares(tg->cfs_rq[i], 0);
8696 set_se_shares(tg->se[i], shares/nr_cpu_ids);
8697 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008698
8699 /*
8700 * Enable load balance activity on this group, by inserting it back on
8701 * each cpu's rq->leaf_cfs_rq_list.
8702 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008703 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008704 for_each_possible_cpu(i)
8705 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008706 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008707 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008708done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008709 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008710 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008711}
8712
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008713unsigned long sched_group_shares(struct task_group *tg)
8714{
8715 return tg->shares;
8716}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008717#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008718
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008719#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008720/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008721 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008722 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008723static DEFINE_MUTEX(rt_constraints_mutex);
8724
8725static unsigned long to_ratio(u64 period, u64 runtime)
8726{
8727 if (runtime == RUNTIME_INF)
8728 return 1ULL << 16;
8729
Peter Zijlstra2692a242008-02-27 12:00:46 +01008730 return div64_64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008731}
8732
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008733#ifdef CONFIG_CGROUP_SCHED
8734static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8735{
8736 struct task_group *tgi, *parent = tg->parent;
8737 unsigned long total = 0;
8738
8739 if (!parent) {
8740 if (global_rt_period() < period)
8741 return 0;
8742
8743 return to_ratio(period, runtime) <
8744 to_ratio(global_rt_period(), global_rt_runtime());
8745 }
8746
8747 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8748 return 0;
8749
8750 rcu_read_lock();
8751 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8752 if (tgi == tg)
8753 continue;
8754
8755 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8756 tgi->rt_bandwidth.rt_runtime);
8757 }
8758 rcu_read_unlock();
8759
8760 return total + to_ratio(period, runtime) <
8761 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8762 parent->rt_bandwidth.rt_runtime);
8763}
8764#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008765static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008766{
8767 struct task_group *tgi;
8768 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008769 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008770 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008771
8772 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008773 list_for_each_entry_rcu(tgi, &task_groups, list) {
8774 if (tgi == tg)
8775 continue;
8776
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008777 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8778 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008779 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008780 rcu_read_unlock();
8781
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008782 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008783}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008784#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008785
Dhaval Giani521f1a242008-02-28 15:21:56 +05308786/* Must be called with tasklist_lock held */
8787static inline int tg_has_rt_tasks(struct task_group *tg)
8788{
8789 struct task_struct *g, *p;
8790 do_each_thread(g, p) {
8791 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8792 return 1;
8793 } while_each_thread(g, p);
8794 return 0;
8795}
8796
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008797static int tg_set_bandwidth(struct task_group *tg,
8798 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008799{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008800 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008801
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008802 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308803 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008804 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308805 err = -EBUSY;
8806 goto unlock;
8807 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008808 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8809 err = -EINVAL;
8810 goto unlock;
8811 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008812
8813 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008814 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8815 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008816
8817 for_each_possible_cpu(i) {
8818 struct rt_rq *rt_rq = tg->rt_rq[i];
8819
8820 spin_lock(&rt_rq->rt_runtime_lock);
8821 rt_rq->rt_runtime = rt_runtime;
8822 spin_unlock(&rt_rq->rt_runtime_lock);
8823 }
8824 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008825 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308826 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008827 mutex_unlock(&rt_constraints_mutex);
8828
8829 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008830}
8831
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008832int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8833{
8834 u64 rt_runtime, rt_period;
8835
8836 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8837 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8838 if (rt_runtime_us < 0)
8839 rt_runtime = RUNTIME_INF;
8840
8841 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8842}
8843
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008844long sched_group_rt_runtime(struct task_group *tg)
8845{
8846 u64 rt_runtime_us;
8847
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008848 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008849 return -1;
8850
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008851 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008852 do_div(rt_runtime_us, NSEC_PER_USEC);
8853 return rt_runtime_us;
8854}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008855
8856int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8857{
8858 u64 rt_runtime, rt_period;
8859
8860 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8861 rt_runtime = tg->rt_bandwidth.rt_runtime;
8862
8863 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8864}
8865
8866long sched_group_rt_period(struct task_group *tg)
8867{
8868 u64 rt_period_us;
8869
8870 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8871 do_div(rt_period_us, NSEC_PER_USEC);
8872 return rt_period_us;
8873}
8874
8875static int sched_rt_global_constraints(void)
8876{
8877 int ret = 0;
8878
8879 mutex_lock(&rt_constraints_mutex);
8880 if (!__rt_schedulable(NULL, 1, 0))
8881 ret = -EINVAL;
8882 mutex_unlock(&rt_constraints_mutex);
8883
8884 return ret;
8885}
8886#else
8887static int sched_rt_global_constraints(void)
8888{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008889 unsigned long flags;
8890 int i;
8891
8892 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
8893 for_each_possible_cpu(i) {
8894 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8895
8896 spin_lock(&rt_rq->rt_runtime_lock);
8897 rt_rq->rt_runtime = global_rt_runtime();
8898 spin_unlock(&rt_rq->rt_runtime_lock);
8899 }
8900 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
8901
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008902 return 0;
8903}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008904#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008905
8906int sched_rt_handler(struct ctl_table *table, int write,
8907 struct file *filp, void __user *buffer, size_t *lenp,
8908 loff_t *ppos)
8909{
8910 int ret;
8911 int old_period, old_runtime;
8912 static DEFINE_MUTEX(mutex);
8913
8914 mutex_lock(&mutex);
8915 old_period = sysctl_sched_rt_period;
8916 old_runtime = sysctl_sched_rt_runtime;
8917
8918 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8919
8920 if (!ret && write) {
8921 ret = sched_rt_global_constraints();
8922 if (ret) {
8923 sysctl_sched_rt_period = old_period;
8924 sysctl_sched_rt_runtime = old_runtime;
8925 } else {
8926 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8927 def_rt_bandwidth.rt_period =
8928 ns_to_ktime(global_rt_period());
8929 }
8930 }
8931 mutex_unlock(&mutex);
8932
8933 return ret;
8934}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008935
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008936#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008937
8938/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008939static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008940{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008941 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8942 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008943}
8944
8945static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008946cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008947{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008948 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008949
Paul Menage2b01dfe2007-10-24 18:23:50 +02008950 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008951 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008952 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008953 return &init_task_group.css;
8954 }
8955
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008956 parent = cgroup_tg(cgrp->parent);
8957 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008958 if (IS_ERR(tg))
8959 return ERR_PTR(-ENOMEM);
8960
8961 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008962 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008963
8964 return &tg->css;
8965}
8966
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008967static void
8968cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008969{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008970 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008971
8972 sched_destroy_group(tg);
8973}
8974
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008975static int
8976cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8977 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008978{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008979#ifdef CONFIG_RT_GROUP_SCHED
8980 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008981 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008982 return -EINVAL;
8983#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008984 /* We don't support RT-tasks being in separate groups */
8985 if (tsk->sched_class != &fair_sched_class)
8986 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008987#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008988
8989 return 0;
8990}
8991
8992static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008993cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008994 struct cgroup *old_cont, struct task_struct *tsk)
8995{
8996 sched_move_task(tsk);
8997}
8998
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008999#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menage2b01dfe2007-10-24 18:23:50 +02009000static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9001 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009002{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009003 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009004}
9005
Paul Menage2b01dfe2007-10-24 18:23:50 +02009006static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009007{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009008 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009009
9010 return (u64) tg->shares;
9011}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009012#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009013
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009014#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009015static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009016 struct file *file,
9017 const char __user *userbuf,
9018 size_t nbytes, loff_t *unused_ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009019{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009020 char buffer[64];
9021 int retval = 0;
9022 s64 val;
9023 char *end;
9024
9025 if (!nbytes)
9026 return -EINVAL;
9027 if (nbytes >= sizeof(buffer))
9028 return -E2BIG;
9029 if (copy_from_user(buffer, userbuf, nbytes))
9030 return -EFAULT;
9031
9032 buffer[nbytes] = 0; /* nul-terminate */
9033
9034 /* strip newline if necessary */
9035 if (nbytes && (buffer[nbytes-1] == '\n'))
9036 buffer[nbytes-1] = 0;
9037 val = simple_strtoll(buffer, &end, 0);
9038 if (*end)
9039 return -EINVAL;
9040
9041 /* Pass to subsystem */
9042 retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
9043 if (!retval)
9044 retval = nbytes;
9045 return retval;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009046}
9047
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009048static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
9049 struct file *file,
9050 char __user *buf, size_t nbytes,
9051 loff_t *ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009052{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009053 char tmp[64];
9054 long val = sched_group_rt_runtime(cgroup_tg(cgrp));
9055 int len = sprintf(tmp, "%ld\n", val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009056
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009057 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009058}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009059
9060static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9061 u64 rt_period_us)
9062{
9063 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9064}
9065
9066static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9067{
9068 return sched_group_rt_period(cgroup_tg(cgrp));
9069}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009070#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009071
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009072static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009073#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009074 {
9075 .name = "shares",
9076 .read_uint = cpu_shares_read_uint,
9077 .write_uint = cpu_shares_write_uint,
9078 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009079#endif
9080#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009081 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009082 .name = "rt_runtime_us",
9083 .read = cpu_rt_runtime_read,
9084 .write = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009085 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009086 {
9087 .name = "rt_period_us",
9088 .read_uint = cpu_rt_period_read_uint,
9089 .write_uint = cpu_rt_period_write_uint,
9090 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009091#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009092};
9093
9094static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9095{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009096 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009097}
9098
9099struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009100 .name = "cpu",
9101 .create = cpu_cgroup_create,
9102 .destroy = cpu_cgroup_destroy,
9103 .can_attach = cpu_cgroup_can_attach,
9104 .attach = cpu_cgroup_attach,
9105 .populate = cpu_cgroup_populate,
9106 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009107 .early_init = 1,
9108};
9109
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009110#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009111
9112#ifdef CONFIG_CGROUP_CPUACCT
9113
9114/*
9115 * CPU accounting code for task groups.
9116 *
9117 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9118 * (balbir@in.ibm.com).
9119 */
9120
9121/* track cpu usage of a group of tasks */
9122struct cpuacct {
9123 struct cgroup_subsys_state css;
9124 /* cpuusage holds pointer to a u64-type object on every cpu */
9125 u64 *cpuusage;
9126};
9127
9128struct cgroup_subsys cpuacct_subsys;
9129
9130/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309131static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009132{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309133 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009134 struct cpuacct, css);
9135}
9136
9137/* return cpu accounting group to which this task belongs */
9138static inline struct cpuacct *task_ca(struct task_struct *tsk)
9139{
9140 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9141 struct cpuacct, css);
9142}
9143
9144/* create a new cpu accounting group */
9145static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309146 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009147{
9148 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9149
9150 if (!ca)
9151 return ERR_PTR(-ENOMEM);
9152
9153 ca->cpuusage = alloc_percpu(u64);
9154 if (!ca->cpuusage) {
9155 kfree(ca);
9156 return ERR_PTR(-ENOMEM);
9157 }
9158
9159 return &ca->css;
9160}
9161
9162/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009163static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309164cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009165{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309166 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009167
9168 free_percpu(ca->cpuusage);
9169 kfree(ca);
9170}
9171
9172/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309173static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009174{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309175 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009176 u64 totalcpuusage = 0;
9177 int i;
9178
9179 for_each_possible_cpu(i) {
9180 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9181
9182 /*
9183 * Take rq->lock to make 64-bit addition safe on 32-bit
9184 * platforms.
9185 */
9186 spin_lock_irq(&cpu_rq(i)->lock);
9187 totalcpuusage += *cpuusage;
9188 spin_unlock_irq(&cpu_rq(i)->lock);
9189 }
9190
9191 return totalcpuusage;
9192}
9193
Dhaval Giani0297b802008-02-29 10:02:44 +05309194static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9195 u64 reset)
9196{
9197 struct cpuacct *ca = cgroup_ca(cgrp);
9198 int err = 0;
9199 int i;
9200
9201 if (reset) {
9202 err = -EINVAL;
9203 goto out;
9204 }
9205
9206 for_each_possible_cpu(i) {
9207 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9208
9209 spin_lock_irq(&cpu_rq(i)->lock);
9210 *cpuusage = 0;
9211 spin_unlock_irq(&cpu_rq(i)->lock);
9212 }
9213out:
9214 return err;
9215}
9216
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009217static struct cftype files[] = {
9218 {
9219 .name = "usage",
9220 .read_uint = cpuusage_read,
Dhaval Giani0297b802008-02-29 10:02:44 +05309221 .write_uint = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009222 },
9223};
9224
Dhaval Giani32cd7562008-02-29 10:02:43 +05309225static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009226{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309227 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009228}
9229
9230/*
9231 * charge this task's execution time to its accounting group.
9232 *
9233 * called with rq->lock held.
9234 */
9235static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9236{
9237 struct cpuacct *ca;
9238
9239 if (!cpuacct_subsys.active)
9240 return;
9241
9242 ca = task_ca(tsk);
9243 if (ca) {
9244 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9245
9246 *cpuusage += cputime;
9247 }
9248}
9249
9250struct cgroup_subsys cpuacct_subsys = {
9251 .name = "cpuacct",
9252 .create = cpuacct_create,
9253 .destroy = cpuacct_destroy,
9254 .populate = cpuacct_populate,
9255 .subsys_id = cpuacct_subsys_id,
9256};
9257#endif /* CONFIG_CGROUP_CPUACCT */