blob: 475e3fcab738b15d5e2953e2a5fc83b7bcc594de [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
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100319static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100320#endif
321
322/* Default task group.
323 * Every task in system belong to this group at bootup.
324 */
Mike Travis434d53b2008-04-04 18:11:04 -0700325struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200326
327/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200328static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200329{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200330 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200331
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100332#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200333 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100334#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700335 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
336 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200337#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100338 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200339#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200340 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200341}
342
343/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100344static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200345{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100346#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100347 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
348 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100349#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100350
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100351#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100352 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
353 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100354#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200355}
356
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100357static inline void lock_doms_cur(void)
358{
359 mutex_lock(&doms_cur_mutex);
360}
361
362static inline void unlock_doms_cur(void)
363{
364 mutex_unlock(&doms_cur_mutex);
365}
366
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200367#else
368
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100369static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100370static inline void lock_doms_cur(void) { }
371static inline void unlock_doms_cur(void) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200372
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100373#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200374
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200375/* CFS-related fields in a runqueue */
376struct cfs_rq {
377 struct load_weight load;
378 unsigned long nr_running;
379
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200380 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200381 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200382
383 struct rb_root tasks_timeline;
384 struct rb_node *rb_leftmost;
385 struct rb_node *rb_load_balance_curr;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200386 /* 'curr' points to currently running entity on this cfs_rq.
387 * It is set to NULL otherwise (i.e when none are currently running).
388 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100389 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200390
391 unsigned long nr_spread_over;
392
Ingo Molnar62160e32007-10-15 17:00:03 +0200393#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200394 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
395
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100396 /*
397 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200398 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
399 * (like users, containers etc.)
400 *
401 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
402 * list is used during load balance.
403 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100404 struct list_head leaf_cfs_rq_list;
405 struct task_group *tg; /* group that "owns" this runqueue */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200406#endif
407};
408
409/* Real-Time classes' related field in a runqueue: */
410struct rt_rq {
411 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100412 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100413#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100414 int highest_prio; /* highest queued rt task prio */
415#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100416#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100417 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100418 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100419#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100420 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100421 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200422 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100423 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200424 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100425
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100426#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100427 unsigned long rt_nr_boosted;
428
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100429 struct rq *rq;
430 struct list_head leaf_rt_rq_list;
431 struct task_group *tg;
432 struct sched_rt_entity *rt_se;
433#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200434};
435
Gregory Haskins57d885f2008-01-25 21:08:18 +0100436#ifdef CONFIG_SMP
437
438/*
439 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100440 * variables. Each exclusive cpuset essentially defines an island domain by
441 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100442 * exclusive cpuset is created, we also create and attach a new root-domain
443 * object.
444 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100445 */
446struct root_domain {
447 atomic_t refcount;
448 cpumask_t span;
449 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100450
Ingo Molnar0eab9142008-01-25 21:08:19 +0100451 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100452 * The "RT overload" flag: it gets set if a CPU has more than
453 * one runnable RT task.
454 */
455 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100456 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100457};
458
Gregory Haskinsdc938522008-01-25 21:08:26 +0100459/*
460 * By default the system creates a single root-domain with all cpus as
461 * members (mimicking the global state we have today).
462 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100463static struct root_domain def_root_domain;
464
465#endif
466
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200467/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * This is the main, per-CPU runqueue data structure.
469 *
470 * Locking rule: those places that want to lock multiple runqueues
471 * (such as the load balancing or the thread migration code), lock
472 * acquire operations must be ordered by ascending &runqueue.
473 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700474struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200475 /* runqueue lock: */
476 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /*
479 * nr_running and cpu_load should be in the same cacheline because
480 * remote CPUs use both these fields when doing load calculation.
481 */
482 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200483 #define CPU_LOAD_IDX_MAX 5
484 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700485 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700486#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200487 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700488 unsigned char in_nohz_recently;
489#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200490 /* capture load from *all* tasks on this cpu: */
491 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200492 unsigned long nr_load_updates;
493 u64 nr_switches;
494
495 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100496 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100497
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200498#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200499 /* list of leaf cfs_rq on this cpu: */
500 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100501#endif
502#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100503 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /*
507 * This is part of a global counter where only the total sum
508 * over all CPUs matters. A task can increase this counter on
509 * one CPU and if it got migrated afterwards it may decrease
510 * it on another CPU. Always updated under the runqueue lock:
511 */
512 unsigned long nr_uninterruptible;
513
Ingo Molnar36c8b582006-07-03 00:25:41 -0700514 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800515 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200517
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200518 u64 clock, prev_clock_raw;
519 s64 clock_max_delta;
520
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100521 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200522 u64 idle_clock;
523 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200524 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 atomic_t nr_iowait;
527
528#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100529 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct sched_domain *sd;
531
532 /* For active balancing */
533 int active_balance;
534 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200535 /* cpu of this runqueue: */
536 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Ingo Molnar36c8b582006-07-03 00:25:41 -0700538 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct list_head migration_queue;
540#endif
541
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100542#ifdef CONFIG_SCHED_HRTICK
543 unsigned long hrtick_flags;
544 ktime_t hrtick_expire;
545 struct hrtimer hrtick_timer;
546#endif
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#ifdef CONFIG_SCHEDSTATS
549 /* latency stats */
550 struct sched_info rq_sched_info;
551
552 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200553 unsigned int yld_exp_empty;
554 unsigned int yld_act_empty;
555 unsigned int yld_both_empty;
556 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200559 unsigned int sched_switch;
560 unsigned int sched_count;
561 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200564 unsigned int ttwu_count;
565 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200566
567 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200568 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700570 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571};
572
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700573static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Ingo Molnardd41f592007-07-09 18:51:59 +0200575static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
576{
577 rq->curr->sched_class->check_preempt_curr(rq, p);
578}
579
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700580static inline int cpu_of(struct rq *rq)
581{
582#ifdef CONFIG_SMP
583 return rq->cpu;
584#else
585 return 0;
586#endif
587}
588
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200589#ifdef CONFIG_NO_HZ
590static inline bool nohz_on(int cpu)
591{
592 return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE;
593}
594
595static inline u64 max_skipped_ticks(struct rq *rq)
596{
597 return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1;
598}
599
600static inline void update_last_tick_seen(struct rq *rq)
601{
602 rq->last_tick_seen = jiffies;
603}
604#else
605static inline u64 max_skipped_ticks(struct rq *rq)
606{
607 return 1;
608}
609
610static inline void update_last_tick_seen(struct rq *rq)
611{
612}
613#endif
614
Nick Piggin674311d2005-06-25 14:57:27 -0700615/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200616 * Update the per-runqueue clock, as finegrained as the platform can give
617 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200618 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200619static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200620{
621 u64 prev_raw = rq->prev_clock_raw;
622 u64 now = sched_clock();
623 s64 delta = now - prev_raw;
624 u64 clock = rq->clock;
625
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200626#ifdef CONFIG_SCHED_DEBUG
627 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
628#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200629 /*
630 * Protect against sched_clock() occasionally going backwards:
631 */
632 if (unlikely(delta < 0)) {
633 clock++;
634 rq->clock_warps++;
635 } else {
636 /*
637 * Catch too large forward jumps too:
638 */
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200639 u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC;
640 u64 max_time = rq->tick_timestamp + max_jump;
641
642 if (unlikely(clock + delta > max_time)) {
643 if (clock < max_time)
644 clock = max_time;
Ingo Molnar529c7722007-08-10 23:05:11 +0200645 else
646 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200647 rq->clock_overflows++;
648 } else {
649 if (unlikely(delta > rq->clock_max_delta))
650 rq->clock_max_delta = delta;
651 clock += delta;
652 }
653 }
654
655 rq->prev_clock_raw = now;
656 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200657}
658
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200659static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200660{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200661 if (likely(smp_processor_id() == cpu_of(rq)))
662 __update_rq_clock(rq);
663}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200664
Ingo Molnar20d315d2007-07-09 18:51:58 +0200665/*
Nick Piggin674311d2005-06-25 14:57:27 -0700666 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700667 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700668 *
669 * The domain tree of any CPU may only be accessed from within
670 * preempt-disabled sections.
671 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700672#define for_each_domain(cpu, __sd) \
673 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
676#define this_rq() (&__get_cpu_var(runqueues))
677#define task_rq(p) cpu_rq(task_cpu(p))
678#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
679
Ingo Molnare436d802007-07-19 21:28:35 +0200680/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200681 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
682 */
683#ifdef CONFIG_SCHED_DEBUG
684# define const_debug __read_mostly
685#else
686# define const_debug static const
687#endif
688
689/*
690 * Debugging: various feature bits
691 */
692enum {
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200693 SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
Ingo Molnar96126332007-11-15 20:57:40 +0100694 SCHED_FEAT_WAKEUP_PREEMPT = 2,
695 SCHED_FEAT_START_DEBIT = 4,
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100696 SCHED_FEAT_AFFINE_WAKEUPS = 8,
697 SCHED_FEAT_CACHE_HOT_BUDDY = 16,
Ingo Molnar02e2b832008-03-19 01:37:10 +0100698 SCHED_FEAT_SYNC_WAKEUPS = 32,
699 SCHED_FEAT_HRTICK = 64,
700 SCHED_FEAT_DOUBLE_TICK = 128,
Peter Zijlstra112f53f2008-03-19 11:43:36 +0100701 SCHED_FEAT_NORMALIZED_SLEEPER = 256,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200702};
703
704const_debug unsigned int sysctl_sched_features =
Ingo Molnar8401f772007-10-18 21:32:55 +0200705 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
Ingo Molnar96126332007-11-15 20:57:40 +0100706 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
Ingo Molnar8401f772007-10-18 21:32:55 +0200707 SCHED_FEAT_START_DEBIT * 1 |
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100708 SCHED_FEAT_AFFINE_WAKEUPS * 1 |
709 SCHED_FEAT_CACHE_HOT_BUDDY * 1 |
Ingo Molnar02e2b832008-03-19 01:37:10 +0100710 SCHED_FEAT_SYNC_WAKEUPS * 1 |
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100711 SCHED_FEAT_HRTICK * 1 |
Peter Zijlstra112f53f2008-03-19 11:43:36 +0100712 SCHED_FEAT_DOUBLE_TICK * 0 |
713 SCHED_FEAT_NORMALIZED_SLEEPER * 1;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200714
715#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
716
717/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100718 * Number of tasks to iterate in a single balance run.
719 * Limited because this is done with IRQs disabled.
720 */
721const_debug unsigned int sysctl_sched_nr_migrate = 32;
722
723/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100724 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100725 * default: 1s
726 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100727unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100728
Ingo Molnar6892b752008-02-13 14:02:36 +0100729static __read_mostly int scheduler_running;
730
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100731/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100732 * part of the period that we allow rt tasks to run in us.
733 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100734 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100735int sysctl_sched_rt_runtime = 950000;
736
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200737static inline u64 global_rt_period(void)
738{
739 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
740}
741
742static inline u64 global_rt_runtime(void)
743{
744 if (sysctl_sched_rt_period < 0)
745 return RUNTIME_INF;
746
747 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
748}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100749
Ingo Molnar27ec4402008-02-28 21:00:21 +0100750static const unsigned long long time_sync_thresh = 100000;
751
752static DEFINE_PER_CPU(unsigned long long, time_offset);
753static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
754
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100755/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100756 * Global lock which we take every now and then to synchronize
757 * the CPUs time. This method is not warp-safe, but it's good
758 * enough to synchronize slowly diverging time sources and thus
759 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200760 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100761static DEFINE_SPINLOCK(time_sync_lock);
762static unsigned long long prev_global_time;
763
764static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
765{
766 unsigned long flags;
767
768 spin_lock_irqsave(&time_sync_lock, flags);
769
770 if (time < prev_global_time) {
771 per_cpu(time_offset, cpu) += prev_global_time - time;
772 time = prev_global_time;
773 } else {
774 prev_global_time = time;
775 }
776
777 spin_unlock_irqrestore(&time_sync_lock, flags);
778
779 return time;
780}
781
782static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200783{
Ingo Molnare436d802007-07-19 21:28:35 +0200784 unsigned long long now;
785 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200786 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200787
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100788 /*
789 * Only call sched_clock() if the scheduler has already been
790 * initialized (some code might call cpu_clock() very early):
791 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100792 if (unlikely(!scheduler_running))
793 return 0;
794
795 local_irq_save(flags);
796 rq = cpu_rq(cpu);
797 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200798 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200799 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200800
801 return now;
802}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100803
804/*
805 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
806 * clock constructed from sched_clock():
807 */
808unsigned long long cpu_clock(int cpu)
809{
810 unsigned long long prev_cpu_time, time, delta_time;
811
812 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
813 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
814 delta_time = time-prev_cpu_time;
815
816 if (unlikely(delta_time > time_sync_thresh))
817 time = __sync_cpu_clock(time, cpu);
818
819 return time;
820}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200821EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700824# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700826#ifndef finish_arch_switch
827# define finish_arch_switch(prev) do { } while (0)
828#endif
829
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100830static inline int task_current(struct rq *rq, struct task_struct *p)
831{
832 return rq->curr == p;
833}
834
Nick Piggin4866cde2005-06-25 14:57:23 -0700835#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700836static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700837{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100838 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700839}
840
Ingo Molnar70b97a72006-07-03 00:25:42 -0700841static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700842{
843}
844
Ingo Molnar70b97a72006-07-03 00:25:42 -0700845static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700846{
Ingo Molnarda04c032005-09-13 11:17:59 +0200847#ifdef CONFIG_DEBUG_SPINLOCK
848 /* this is a valid case when another task releases the spinlock */
849 rq->lock.owner = current;
850#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700851 /*
852 * If we are tracking spinlock dependencies then we have to
853 * fix up the runqueue lock - which gets 'carried over' from
854 * prev into current:
855 */
856 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
857
Nick Piggin4866cde2005-06-25 14:57:23 -0700858 spin_unlock_irq(&rq->lock);
859}
860
861#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700862static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700863{
864#ifdef CONFIG_SMP
865 return p->oncpu;
866#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100867 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700868#endif
869}
870
Ingo Molnar70b97a72006-07-03 00:25:42 -0700871static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700872{
873#ifdef CONFIG_SMP
874 /*
875 * We can optimise this out completely for !SMP, because the
876 * SMP rebalancing from interrupt is the only thing that cares
877 * here.
878 */
879 next->oncpu = 1;
880#endif
881#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
882 spin_unlock_irq(&rq->lock);
883#else
884 spin_unlock(&rq->lock);
885#endif
886}
887
Ingo Molnar70b97a72006-07-03 00:25:42 -0700888static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700889{
890#ifdef CONFIG_SMP
891 /*
892 * After ->oncpu is cleared, the task can be moved to a different CPU.
893 * We must ensure this doesn't happen until the switch is completely
894 * finished.
895 */
896 smp_wmb();
897 prev->oncpu = 0;
898#endif
899#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
900 local_irq_enable();
901#endif
902}
903#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700906 * __task_rq_lock - lock the runqueue a given task resides on.
907 * Must be called interrupts disabled.
908 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700909static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700910 __acquires(rq->lock)
911{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200912 for (;;) {
913 struct rq *rq = task_rq(p);
914 spin_lock(&rq->lock);
915 if (likely(rq == task_rq(p)))
916 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700917 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700918 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700919}
920
921/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100923 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * explicitly disabling preemption.
925 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700926static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 __acquires(rq->lock)
928{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700929 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Andi Kleen3a5c3592007-10-15 17:00:14 +0200931 for (;;) {
932 local_irq_save(*flags);
933 rq = task_rq(p);
934 spin_lock(&rq->lock);
935 if (likely(rq == task_rq(p)))
936 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Alexey Dobriyana9957442007-10-15 17:00:13 +0200941static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700942 __releases(rq->lock)
943{
944 spin_unlock(&rq->lock);
945}
946
Ingo Molnar70b97a72006-07-03 00:25:42 -0700947static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 __releases(rq->lock)
949{
950 spin_unlock_irqrestore(&rq->lock, *flags);
951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800954 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200956static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 __acquires(rq->lock)
958{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700959 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 local_irq_disable();
962 rq = this_rq();
963 spin_lock(&rq->lock);
964
965 return rq;
966}
967
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200968/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200969 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200970 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200971void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200972{
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200973 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200974
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200975 spin_lock(&rq->lock);
976 __update_rq_clock(rq);
977 spin_unlock(&rq->lock);
978 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200979}
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200980EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
981
982/*
983 * We just idled delta nanoseconds (called with irqs disabled):
984 */
985void sched_clock_idle_wakeup_event(u64 delta_ns)
986{
987 struct rq *rq = cpu_rq(smp_processor_id());
988 u64 now = sched_clock();
989
990 rq->idle_clock += delta_ns;
991 /*
992 * Override the previous timestamp and ignore all
993 * sched_clock() deltas that occured while we idled,
994 * and use the PM-provided delta_ns to advance the
995 * rq clock:
996 */
997 spin_lock(&rq->lock);
998 rq->prev_clock_raw = now;
999 rq->clock += delta_ns;
1000 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +01001001 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001002}
1003EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001004
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001005static void __resched_task(struct task_struct *p, int tif_bit);
1006
1007static inline void resched_task(struct task_struct *p)
1008{
1009 __resched_task(p, TIF_NEED_RESCHED);
1010}
1011
1012#ifdef CONFIG_SCHED_HRTICK
1013/*
1014 * Use HR-timers to deliver accurate preemption points.
1015 *
1016 * Its all a bit involved since we cannot program an hrt while holding the
1017 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1018 * reschedule event.
1019 *
1020 * When we get rescheduled we reprogram the hrtick_timer outside of the
1021 * rq->lock.
1022 */
1023static inline void resched_hrt(struct task_struct *p)
1024{
1025 __resched_task(p, TIF_HRTICK_RESCHED);
1026}
1027
1028static inline void resched_rq(struct rq *rq)
1029{
1030 unsigned long flags;
1031
1032 spin_lock_irqsave(&rq->lock, flags);
1033 resched_task(rq->curr);
1034 spin_unlock_irqrestore(&rq->lock, flags);
1035}
1036
1037enum {
1038 HRTICK_SET, /* re-programm hrtick_timer */
1039 HRTICK_RESET, /* not a new slice */
1040};
1041
1042/*
1043 * Use hrtick when:
1044 * - enabled by features
1045 * - hrtimer is actually high res
1046 */
1047static inline int hrtick_enabled(struct rq *rq)
1048{
1049 if (!sched_feat(HRTICK))
1050 return 0;
1051 return hrtimer_is_hres_active(&rq->hrtick_timer);
1052}
1053
1054/*
1055 * Called to set the hrtick timer state.
1056 *
1057 * called with rq->lock held and irqs disabled
1058 */
1059static void hrtick_start(struct rq *rq, u64 delay, int reset)
1060{
1061 assert_spin_locked(&rq->lock);
1062
1063 /*
1064 * preempt at: now + delay
1065 */
1066 rq->hrtick_expire =
1067 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1068 /*
1069 * indicate we need to program the timer
1070 */
1071 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1072 if (reset)
1073 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1074
1075 /*
1076 * New slices are called from the schedule path and don't need a
1077 * forced reschedule.
1078 */
1079 if (reset)
1080 resched_hrt(rq->curr);
1081}
1082
1083static void hrtick_clear(struct rq *rq)
1084{
1085 if (hrtimer_active(&rq->hrtick_timer))
1086 hrtimer_cancel(&rq->hrtick_timer);
1087}
1088
1089/*
1090 * Update the timer from the possible pending state.
1091 */
1092static void hrtick_set(struct rq *rq)
1093{
1094 ktime_t time;
1095 int set, reset;
1096 unsigned long flags;
1097
1098 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1099
1100 spin_lock_irqsave(&rq->lock, flags);
1101 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1102 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1103 time = rq->hrtick_expire;
1104 clear_thread_flag(TIF_HRTICK_RESCHED);
1105 spin_unlock_irqrestore(&rq->lock, flags);
1106
1107 if (set) {
1108 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1109 if (reset && !hrtimer_active(&rq->hrtick_timer))
1110 resched_rq(rq);
1111 } else
1112 hrtick_clear(rq);
1113}
1114
1115/*
1116 * High-resolution timer tick.
1117 * Runs from hardirq context with interrupts disabled.
1118 */
1119static enum hrtimer_restart hrtick(struct hrtimer *timer)
1120{
1121 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1122
1123 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1124
1125 spin_lock(&rq->lock);
1126 __update_rq_clock(rq);
1127 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1128 spin_unlock(&rq->lock);
1129
1130 return HRTIMER_NORESTART;
1131}
1132
1133static inline void init_rq_hrtick(struct rq *rq)
1134{
1135 rq->hrtick_flags = 0;
1136 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1137 rq->hrtick_timer.function = hrtick;
1138 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1139}
1140
1141void hrtick_resched(void)
1142{
1143 struct rq *rq;
1144 unsigned long flags;
1145
1146 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1147 return;
1148
1149 local_irq_save(flags);
1150 rq = cpu_rq(smp_processor_id());
1151 hrtick_set(rq);
1152 local_irq_restore(flags);
1153}
1154#else
1155static inline void hrtick_clear(struct rq *rq)
1156{
1157}
1158
1159static inline void hrtick_set(struct rq *rq)
1160{
1161}
1162
1163static inline void init_rq_hrtick(struct rq *rq)
1164{
1165}
1166
1167void hrtick_resched(void)
1168{
1169}
1170#endif
1171
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001172/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001173 * resched_task - mark a task 'to be rescheduled now'.
1174 *
1175 * On UP this means the setting of the need_resched flag, on SMP it
1176 * might also involve a cross-CPU call to trigger the scheduler on
1177 * the target CPU.
1178 */
1179#ifdef CONFIG_SMP
1180
1181#ifndef tsk_is_polling
1182#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1183#endif
1184
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001185static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001186{
1187 int cpu;
1188
1189 assert_spin_locked(&task_rq(p)->lock);
1190
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001191 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001192 return;
1193
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001194 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001195
1196 cpu = task_cpu(p);
1197 if (cpu == smp_processor_id())
1198 return;
1199
1200 /* NEED_RESCHED must be visible before we test polling */
1201 smp_mb();
1202 if (!tsk_is_polling(p))
1203 smp_send_reschedule(cpu);
1204}
1205
1206static void resched_cpu(int cpu)
1207{
1208 struct rq *rq = cpu_rq(cpu);
1209 unsigned long flags;
1210
1211 if (!spin_trylock_irqsave(&rq->lock, flags))
1212 return;
1213 resched_task(cpu_curr(cpu));
1214 spin_unlock_irqrestore(&rq->lock, flags);
1215}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001216
1217#ifdef CONFIG_NO_HZ
1218/*
1219 * When add_timer_on() enqueues a timer into the timer wheel of an
1220 * idle CPU then this timer might expire before the next timer event
1221 * which is scheduled to wake up that CPU. In case of a completely
1222 * idle system the next event might even be infinite time into the
1223 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1224 * leaves the inner idle loop so the newly added timer is taken into
1225 * account when the CPU goes back to idle and evaluates the timer
1226 * wheel for the next timer event.
1227 */
1228void wake_up_idle_cpu(int cpu)
1229{
1230 struct rq *rq = cpu_rq(cpu);
1231
1232 if (cpu == smp_processor_id())
1233 return;
1234
1235 /*
1236 * This is safe, as this function is called with the timer
1237 * wheel base lock of (cpu) held. When the CPU is on the way
1238 * to idle and has not yet set rq->curr to idle then it will
1239 * be serialized on the timer wheel base lock and take the new
1240 * timer into account automatically.
1241 */
1242 if (rq->curr != rq->idle)
1243 return;
1244
1245 /*
1246 * We can set TIF_RESCHED on the idle task of the other CPU
1247 * lockless. The worst case is that the other CPU runs the
1248 * idle task through an additional NOOP schedule()
1249 */
1250 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1251
1252 /* NEED_RESCHED must be visible before we test polling */
1253 smp_mb();
1254 if (!tsk_is_polling(rq->idle))
1255 smp_send_reschedule(cpu);
1256}
1257#endif
1258
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001259#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001260static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001261{
1262 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001263 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001264}
1265#endif
1266
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001267#if BITS_PER_LONG == 32
1268# define WMULT_CONST (~0UL)
1269#else
1270# define WMULT_CONST (1UL << 32)
1271#endif
1272
1273#define WMULT_SHIFT 32
1274
Ingo Molnar194081e2007-08-09 11:16:51 +02001275/*
1276 * Shift right and round:
1277 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001278#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001279
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001280static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001281calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1282 struct load_weight *lw)
1283{
1284 u64 tmp;
1285
1286 if (unlikely(!lw->inv_weight))
Ingo Molnar27d11722008-03-14 22:20:01 +01001287 lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001288
1289 tmp = (u64)delta_exec * weight;
1290 /*
1291 * Check whether we'd overflow the 64-bit multiplication:
1292 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001293 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001294 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001295 WMULT_SHIFT/2);
1296 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001297 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001298
Ingo Molnarecf691d2007-08-02 17:41:40 +02001299 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001300}
1301
1302static inline unsigned long
1303calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1304{
1305 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1306}
1307
Ingo Molnar10919852007-10-15 17:00:04 +02001308static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001309{
1310 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001311 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001312}
1313
Ingo Molnar10919852007-10-15 17:00:04 +02001314static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001315{
1316 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001317 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001321 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1322 * of tasks with abnormal "nice" values across CPUs the contribution that
1323 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001324 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001325 * scaled version of the new time slice allocation that they receive on time
1326 * slice expiry etc.
1327 */
1328
Ingo Molnardd41f592007-07-09 18:51:59 +02001329#define WEIGHT_IDLEPRIO 2
1330#define WMULT_IDLEPRIO (1 << 31)
1331
1332/*
1333 * Nice levels are multiplicative, with a gentle 10% change for every
1334 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1335 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1336 * that remained on nice 0.
1337 *
1338 * The "10% effect" is relative and cumulative: from _any_ nice level,
1339 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001340 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1341 * If a task goes up by ~10% and another task goes down by ~10% then
1342 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001343 */
1344static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001345 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1346 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1347 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1348 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1349 /* 0 */ 1024, 820, 655, 526, 423,
1350 /* 5 */ 335, 272, 215, 172, 137,
1351 /* 10 */ 110, 87, 70, 56, 45,
1352 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001353};
1354
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001355/*
1356 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1357 *
1358 * In cases where the weight does not change often, we can use the
1359 * precalculated inverse to speed up arithmetics by turning divisions
1360 * into multiplications:
1361 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001362static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001363 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1364 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1365 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1366 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1367 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1368 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1369 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1370 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001371};
Peter Williams2dd73a42006-06-27 02:54:34 -07001372
Ingo Molnardd41f592007-07-09 18:51:59 +02001373static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1374
1375/*
1376 * runqueue iterator, to support SMP load-balancing between different
1377 * scheduling classes, without having to expose their internal data
1378 * structures to the load-balancing proper:
1379 */
1380struct rq_iterator {
1381 void *arg;
1382 struct task_struct *(*start)(void *);
1383 struct task_struct *(*next)(void *);
1384};
1385
Peter Williamse1d14842007-10-24 18:23:51 +02001386#ifdef CONFIG_SMP
1387static unsigned long
1388balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1389 unsigned long max_load_move, struct sched_domain *sd,
1390 enum cpu_idle_type idle, int *all_pinned,
1391 int *this_best_prio, struct rq_iterator *iterator);
1392
1393static int
1394iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1395 struct sched_domain *sd, enum cpu_idle_type idle,
1396 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001397#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001398
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001399#ifdef CONFIG_CGROUP_CPUACCT
1400static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1401#else
1402static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1403#endif
1404
Gregory Haskinse7693a32008-01-25 21:08:09 +01001405#ifdef CONFIG_SMP
1406static unsigned long source_load(int cpu, int type);
1407static unsigned long target_load(int cpu, int type);
1408static unsigned long cpu_avg_load_per_task(int cpu);
1409static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1410#endif /* CONFIG_SMP */
1411
Ingo Molnardd41f592007-07-09 18:51:59 +02001412#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001413#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001414#include "sched_fair.c"
1415#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001416#ifdef CONFIG_SCHED_DEBUG
1417# include "sched_debug.c"
1418#endif
1419
1420#define sched_class_highest (&rt_sched_class)
1421
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001422static inline void inc_load(struct rq *rq, const struct task_struct *p)
Ingo Molnar9c217242007-08-02 17:41:40 +02001423{
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001424 update_load_add(&rq->load, p->se.load.weight);
Ingo Molnar9c217242007-08-02 17:41:40 +02001425}
1426
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001427static inline void dec_load(struct rq *rq, const struct task_struct *p)
1428{
1429 update_load_sub(&rq->load, p->se.load.weight);
1430}
1431
1432static void inc_nr_running(struct task_struct *p, struct rq *rq)
1433{
1434 rq->nr_running++;
1435 inc_load(rq, p);
1436}
1437
1438static void dec_nr_running(struct task_struct *p, struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001439{
1440 rq->nr_running--;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001441 dec_load(rq, p);
Ingo Molnar9c217242007-08-02 17:41:40 +02001442}
1443
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001444static void set_load_weight(struct task_struct *p)
1445{
1446 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001447 p->se.load.weight = prio_to_weight[0] * 2;
1448 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1449 return;
1450 }
1451
1452 /*
1453 * SCHED_IDLE tasks get minimal weight:
1454 */
1455 if (p->policy == SCHED_IDLE) {
1456 p->se.load.weight = WEIGHT_IDLEPRIO;
1457 p->se.load.inv_weight = WMULT_IDLEPRIO;
1458 return;
1459 }
1460
1461 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1462 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001463}
1464
Ingo Molnar8159f872007-08-09 11:16:49 +02001465static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001466{
1467 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001468 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001469 p->se.on_rq = 1;
1470}
1471
Ingo Molnar69be72c2007-08-09 11:16:49 +02001472static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001473{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001474 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001475 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001476}
1477
1478/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001479 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001480 */
Ingo Molnar14531182007-07-09 18:51:59 +02001481static inline int __normal_prio(struct task_struct *p)
1482{
Ingo Molnardd41f592007-07-09 18:51:59 +02001483 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001484}
1485
1486/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001487 * Calculate the expected normal priority: i.e. priority
1488 * without taking RT-inheritance into account. Might be
1489 * boosted by interactivity modifiers. Changes upon fork,
1490 * setprio syscalls, and whenever the interactivity
1491 * estimator recalculates.
1492 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001493static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001494{
1495 int prio;
1496
Ingo Molnare05606d2007-07-09 18:51:59 +02001497 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001498 prio = MAX_RT_PRIO-1 - p->rt_priority;
1499 else
1500 prio = __normal_prio(p);
1501 return prio;
1502}
1503
1504/*
1505 * Calculate the current priority, i.e. the priority
1506 * taken into account by the scheduler. This value might
1507 * be boosted by RT tasks, or might be boosted by
1508 * interactivity modifiers. Will be RT if the task got
1509 * RT-boosted. If not then it returns p->normal_prio.
1510 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001511static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001512{
1513 p->normal_prio = normal_prio(p);
1514 /*
1515 * If we are RT tasks or we were boosted to RT priority,
1516 * keep the priority unchanged. Otherwise, update priority
1517 * to the normal priority:
1518 */
1519 if (!rt_prio(p->prio))
1520 return p->normal_prio;
1521 return p->prio;
1522}
1523
1524/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001525 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001527static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001529 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001530 rq->nr_uninterruptible--;
1531
Ingo Molnar8159f872007-08-09 11:16:49 +02001532 enqueue_task(rq, p, wakeup);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001533 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 * deactivate_task - remove a task from the runqueue.
1538 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001539static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001541 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001542 rq->nr_uninterruptible++;
1543
Ingo Molnar69be72c2007-08-09 11:16:49 +02001544 dequeue_task(rq, p, sleep);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001545 dec_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548/**
1549 * task_curr - is this task currently executing on a CPU?
1550 * @p: the task in question.
1551 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001552inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 return cpu_curr(task_cpu(p)) == p;
1555}
1556
Peter Williams2dd73a42006-06-27 02:54:34 -07001557/* Used instead of source_load when we know the type == 0 */
1558unsigned long weighted_cpuload(const int cpu)
1559{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001560 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001561}
1562
1563static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1564{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001565 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001566#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001567 /*
1568 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1569 * successfuly executed on another CPU. We must ensure that updates of
1570 * per-task data have been completed by this moment.
1571 */
1572 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001573 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001574#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001575}
1576
Steven Rostedtcb469842008-01-25 21:08:22 +01001577static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1578 const struct sched_class *prev_class,
1579 int oldprio, int running)
1580{
1581 if (prev_class != p->sched_class) {
1582 if (prev_class->switched_from)
1583 prev_class->switched_from(rq, p, running);
1584 p->sched_class->switched_to(rq, p, running);
1585 } else
1586 p->sched_class->prio_changed(rq, p, oldprio, running);
1587}
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001590
Ingo Molnarcc367732007-10-15 17:00:18 +02001591/*
1592 * Is this task likely cache-hot:
1593 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001594static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001595task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1596{
1597 s64 delta;
1598
Ingo Molnarf540a602008-03-15 17:10:34 +01001599 /*
1600 * Buddy candidates are cache hot:
1601 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01001602 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01001603 return 1;
1604
Ingo Molnarcc367732007-10-15 17:00:18 +02001605 if (p->sched_class != &fair_sched_class)
1606 return 0;
1607
Ingo Molnar6bc16652007-10-15 17:00:18 +02001608 if (sysctl_sched_migration_cost == -1)
1609 return 1;
1610 if (sysctl_sched_migration_cost == 0)
1611 return 0;
1612
Ingo Molnarcc367732007-10-15 17:00:18 +02001613 delta = now - p->se.exec_start;
1614
1615 return delta < (s64)sysctl_sched_migration_cost;
1616}
1617
1618
Ingo Molnardd41f592007-07-09 18:51:59 +02001619void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001620{
Ingo Molnardd41f592007-07-09 18:51:59 +02001621 int old_cpu = task_cpu(p);
1622 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001623 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1624 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001625 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001626
1627 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001628
1629#ifdef CONFIG_SCHEDSTATS
1630 if (p->se.wait_start)
1631 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001632 if (p->se.sleep_start)
1633 p->se.sleep_start -= clock_offset;
1634 if (p->se.block_start)
1635 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001636 if (old_cpu != new_cpu) {
1637 schedstat_inc(p, se.nr_migrations);
1638 if (task_hot(p, old_rq->clock, NULL))
1639 schedstat_inc(p, se.nr_forced2_migrations);
1640 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001641#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001642 p->se.vruntime -= old_cfsrq->min_vruntime -
1643 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001644
1645 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001646}
1647
Ingo Molnar70b97a72006-07-03 00:25:42 -07001648struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Ingo Molnar36c8b582006-07-03 00:25:41 -07001651 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 int dest_cpu;
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001655};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657/*
1658 * The task's runqueue lock must be held.
1659 * Returns true if you have to wait for migration thread.
1660 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001661static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001662migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001664 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 /*
1667 * If the task is not on a runqueue (and not running), then
1668 * it is sufficient to simply update the task's cpu field.
1669 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001670 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 set_task_cpu(p, dest_cpu);
1672 return 0;
1673 }
1674
1675 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 req->task = p;
1677 req->dest_cpu = dest_cpu;
1678 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return 1;
1681}
1682
1683/*
1684 * wait_task_inactive - wait for a thread to unschedule.
1685 *
1686 * The caller must ensure that the task *will* unschedule sometime soon,
1687 * else this function might spin for a *long* time. This function can't
1688 * be called with interrupts off, or it may introduce deadlock with
1689 * smp_call_function() if an IPI is sent by the same process we are
1690 * waiting to become inactive.
1691 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001692void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001695 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001696 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Andi Kleen3a5c3592007-10-15 17:00:14 +02001698 for (;;) {
1699 /*
1700 * We do the initial early heuristics without holding
1701 * any task-queue locks at all. We'll only try to get
1702 * the runqueue lock when things look like they will
1703 * work out!
1704 */
1705 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001706
Andi Kleen3a5c3592007-10-15 17:00:14 +02001707 /*
1708 * If the task is actively running on another CPU
1709 * still, just relax and busy-wait without holding
1710 * any locks.
1711 *
1712 * NOTE! Since we don't hold any locks, it's not
1713 * even sure that "rq" stays as the right runqueue!
1714 * But we don't care, since "task_running()" will
1715 * return false if the runqueue has changed and p
1716 * is actually now running somewhere else!
1717 */
1718 while (task_running(rq, p))
1719 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001720
Andi Kleen3a5c3592007-10-15 17:00:14 +02001721 /*
1722 * Ok, time to look more closely! We need the rq
1723 * lock now, to be *sure*. If we're wrong, we'll
1724 * just go back and repeat.
1725 */
1726 rq = task_rq_lock(p, &flags);
1727 running = task_running(rq, p);
1728 on_rq = p->se.on_rq;
1729 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001730
Andi Kleen3a5c3592007-10-15 17:00:14 +02001731 /*
1732 * Was it really running after all now that we
1733 * checked with the proper locks actually held?
1734 *
1735 * Oops. Go back and try again..
1736 */
1737 if (unlikely(running)) {
1738 cpu_relax();
1739 continue;
1740 }
1741
1742 /*
1743 * It's not enough that it's not actively running,
1744 * it must be off the runqueue _entirely_, and not
1745 * preempted!
1746 *
1747 * So if it wa still runnable (but just not actively
1748 * running right now), it's preempted, and we should
1749 * yield - it could be a while.
1750 */
1751 if (unlikely(on_rq)) {
1752 schedule_timeout_uninterruptible(1);
1753 continue;
1754 }
1755
1756 /*
1757 * Ahh, all good. It wasn't running, and it wasn't
1758 * runnable, which means that it will never become
1759 * running in the future either. We're all done!
1760 */
1761 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
1765/***
1766 * kick_process - kick a running thread to enter/exit the kernel
1767 * @p: the to-be-kicked thread
1768 *
1769 * Cause a process which is running on another CPU to enter
1770 * kernel-mode, without any delay. (to get signals handled.)
1771 *
1772 * NOTE: this function doesnt have to take the runqueue lock,
1773 * because all it wants to ensure is that the remote task enters
1774 * the kernel. If the IPI races and the task has been migrated
1775 * to another CPU then no harm is done and the purpose has been
1776 * achieved as well.
1777 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001778void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 int cpu;
1781
1782 preempt_disable();
1783 cpu = task_cpu(p);
1784 if ((cpu != smp_processor_id()) && task_curr(p))
1785 smp_send_reschedule(cpu);
1786 preempt_enable();
1787}
1788
1789/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001790 * Return a low guess at the load of a migration-source cpu weighted
1791 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 *
1793 * We want to under-estimate the load of migration sources, to
1794 * balance conservatively.
1795 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001796static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001797{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001798 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001799 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001800
Peter Williams2dd73a42006-06-27 02:54:34 -07001801 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001802 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001803
Ingo Molnardd41f592007-07-09 18:51:59 +02001804 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
1807/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001808 * Return a high guess at the load of a migration-target cpu weighted
1809 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001811static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001812{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001813 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001814 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001815
Peter Williams2dd73a42006-06-27 02:54:34 -07001816 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001817 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001818
Ingo Molnardd41f592007-07-09 18:51:59 +02001819 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001820}
1821
1822/*
1823 * Return the average load per task on the cpu's run queue
1824 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001825static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07001826{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001827 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001828 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001829 unsigned long n = rq->nr_running;
1830
Ingo Molnardd41f592007-07-09 18:51:59 +02001831 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
Nick Piggin147cbb42005-06-25 14:57:19 -07001834/*
1835 * find_idlest_group finds and returns the least busy CPU group within the
1836 * domain.
1837 */
1838static struct sched_group *
1839find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1840{
1841 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1842 unsigned long min_load = ULONG_MAX, this_load = 0;
1843 int load_idx = sd->forkexec_idx;
1844 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1845
1846 do {
1847 unsigned long load, avg_load;
1848 int local_group;
1849 int i;
1850
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001851 /* Skip over this group if it has no CPUs allowed */
1852 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001853 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001854
Nick Piggin147cbb42005-06-25 14:57:19 -07001855 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001856
1857 /* Tally up the load of all CPUs in the group */
1858 avg_load = 0;
1859
1860 for_each_cpu_mask(i, group->cpumask) {
1861 /* Bias balancing toward cpus of our domain */
1862 if (local_group)
1863 load = source_load(i, load_idx);
1864 else
1865 load = target_load(i, load_idx);
1866
1867 avg_load += load;
1868 }
1869
1870 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001871 avg_load = sg_div_cpu_power(group,
1872 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001873
1874 if (local_group) {
1875 this_load = avg_load;
1876 this = group;
1877 } else if (avg_load < min_load) {
1878 min_load = avg_load;
1879 idlest = group;
1880 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02001881 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07001882
1883 if (!idlest || 100*this_load < imbalance*min_load)
1884 return NULL;
1885 return idlest;
1886}
1887
1888/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001889 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001890 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001891static int
Mike Travis7c16ec52008-04-04 18:11:11 -07001892find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
1893 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07001894{
1895 unsigned long load, min_load = ULONG_MAX;
1896 int idlest = -1;
1897 int i;
1898
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001899 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07001900 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001901
Mike Travis7c16ec52008-04-04 18:11:11 -07001902 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001903 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001904
1905 if (load < min_load || (load == min_load && i == this_cpu)) {
1906 min_load = load;
1907 idlest = i;
1908 }
1909 }
1910
1911 return idlest;
1912}
1913
Nick Piggin476d1392005-06-25 14:57:29 -07001914/*
1915 * sched_balance_self: balance the current task (running on cpu) in domains
1916 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1917 * SD_BALANCE_EXEC.
1918 *
1919 * Balance, ie. select the least loaded group.
1920 *
1921 * Returns the target CPU number, or the same CPU if no balancing is needed.
1922 *
1923 * preempt must be disabled.
1924 */
1925static int sched_balance_self(int cpu, int flag)
1926{
1927 struct task_struct *t = current;
1928 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07001929
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001930 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02001931 /*
1932 * If power savings logic is enabled for a domain, stop there.
1933 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07001934 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1935 break;
Nick Piggin476d1392005-06-25 14:57:29 -07001936 if (tmp->flags & flag)
1937 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001938 }
Nick Piggin476d1392005-06-25 14:57:29 -07001939
1940 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07001941 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07001942 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001943 int new_cpu, weight;
1944
1945 if (!(sd->flags & flag)) {
1946 sd = sd->child;
1947 continue;
1948 }
Nick Piggin476d1392005-06-25 14:57:29 -07001949
1950 span = sd->span;
1951 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001952 if (!group) {
1953 sd = sd->child;
1954 continue;
1955 }
Nick Piggin476d1392005-06-25 14:57:29 -07001956
Mike Travis7c16ec52008-04-04 18:11:11 -07001957 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001958 if (new_cpu == -1 || new_cpu == cpu) {
1959 /* Now try balancing at a lower domain level of cpu */
1960 sd = sd->child;
1961 continue;
1962 }
Nick Piggin476d1392005-06-25 14:57:29 -07001963
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001964 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07001965 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07001966 sd = NULL;
1967 weight = cpus_weight(span);
1968 for_each_domain(cpu, tmp) {
1969 if (weight <= cpus_weight(tmp->span))
1970 break;
1971 if (tmp->flags & flag)
1972 sd = tmp;
1973 }
1974 /* while loop will break here if sd == NULL */
1975 }
1976
1977 return cpu;
1978}
1979
1980#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982/***
1983 * try_to_wake_up - wake up a thread
1984 * @p: the to-be-woken-up thread
1985 * @state: the mask of task states that can be woken
1986 * @sync: do a synchronous wakeup?
1987 *
1988 * Put it on the run-queue if it's not already there. The "current"
1989 * thread is always on the run-queue (except when the actual
1990 * re-schedule is in progress), and as such you're allowed to do
1991 * the simpler "current->state = TASK_RUNNING" to mark yourself
1992 * runnable without the overhead of this.
1993 *
1994 * returns failure only if the task is already active.
1995 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001996static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Ingo Molnarcc367732007-10-15 17:00:18 +02001998 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 unsigned long flags;
2000 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002001 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Ingo Molnarb85d0662008-03-16 20:03:22 +01002003 if (!sched_feat(SYNC_WAKEUPS))
2004 sync = 0;
2005
Linus Torvalds04e2f172008-02-23 18:05:03 -08002006 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 rq = task_rq_lock(p, &flags);
2008 old_state = p->state;
2009 if (!(old_state & state))
2010 goto out;
2011
Ingo Molnardd41f592007-07-09 18:51:59 +02002012 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 goto out_running;
2014
2015 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002016 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 this_cpu = smp_processor_id();
2018
2019#ifdef CONFIG_SMP
2020 if (unlikely(task_running(rq, p)))
2021 goto out_activate;
2022
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002023 cpu = p->sched_class->select_task_rq(p, sync);
2024 if (cpu != orig_cpu) {
2025 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 task_rq_unlock(rq, &flags);
2027 /* might preempt at this point */
2028 rq = task_rq_lock(p, &flags);
2029 old_state = p->state;
2030 if (!(old_state & state))
2031 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002032 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 goto out_running;
2034
2035 this_cpu = smp_processor_id();
2036 cpu = task_cpu(p);
2037 }
2038
Gregory Haskinse7693a32008-01-25 21:08:09 +01002039#ifdef CONFIG_SCHEDSTATS
2040 schedstat_inc(rq, ttwu_count);
2041 if (cpu == this_cpu)
2042 schedstat_inc(rq, ttwu_local);
2043 else {
2044 struct sched_domain *sd;
2045 for_each_domain(this_cpu, sd) {
2046 if (cpu_isset(cpu, sd->span)) {
2047 schedstat_inc(sd, ttwu_wake_remote);
2048 break;
2049 }
2050 }
2051 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002052#endif
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054out_activate:
2055#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002056 schedstat_inc(p, se.nr_wakeups);
2057 if (sync)
2058 schedstat_inc(p, se.nr_wakeups_sync);
2059 if (orig_cpu != cpu)
2060 schedstat_inc(p, se.nr_wakeups_migrate);
2061 if (cpu == this_cpu)
2062 schedstat_inc(p, se.nr_wakeups_local);
2063 else
2064 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002065 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002066 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 success = 1;
2068
2069out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002070 check_preempt_curr(rq, p);
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002073#ifdef CONFIG_SMP
2074 if (p->sched_class->task_wake_up)
2075 p->sched_class->task_wake_up(rq, p);
2076#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077out:
2078 task_rq_unlock(rq, &flags);
2079
2080 return success;
2081}
2082
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002083int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002085 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087EXPORT_SYMBOL(wake_up_process);
2088
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002089int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 return try_to_wake_up(p, state, 0);
2092}
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094/*
2095 * Perform scheduler related setup for a newly forked process p.
2096 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002097 *
2098 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002100static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
Ingo Molnardd41f592007-07-09 18:51:59 +02002102 p->se.exec_start = 0;
2103 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002104 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002105 p->se.last_wakeup = 0;
2106 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002107
2108#ifdef CONFIG_SCHEDSTATS
2109 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002110 p->se.sum_sleep_runtime = 0;
2111 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002112 p->se.block_start = 0;
2113 p->se.sleep_max = 0;
2114 p->se.block_max = 0;
2115 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002116 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002117 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002118#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002119
Peter Zijlstrafa717062008-01-25 21:08:27 +01002120 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002121 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07002122
Avi Kivitye107be32007-07-26 13:40:43 +02002123#ifdef CONFIG_PREEMPT_NOTIFIERS
2124 INIT_HLIST_HEAD(&p->preempt_notifiers);
2125#endif
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 /*
2128 * We mark the process as running here, but have not actually
2129 * inserted it onto the runqueue yet. This guarantees that
2130 * nobody will actually run it, and a signal or other external
2131 * event cannot wake it up and insert it on the runqueue either.
2132 */
2133 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002134}
2135
2136/*
2137 * fork()/clone()-time setup:
2138 */
2139void sched_fork(struct task_struct *p, int clone_flags)
2140{
2141 int cpu = get_cpu();
2142
2143 __sched_fork(p);
2144
2145#ifdef CONFIG_SMP
2146 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2147#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002148 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002149
2150 /*
2151 * Make sure we do not leak PI boosting priority to the child:
2152 */
2153 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002154 if (!rt_prio(p->prio))
2155 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002156
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002157#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002158 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002159 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002161#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002162 p->oncpu = 0;
2163#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002165 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002166 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002168 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
2171/*
2172 * wake_up_new_task - wake up a newly created task for the first time.
2173 *
2174 * This function will do some initial scheduler statistics housekeeping
2175 * that must be done for every newly created context, then puts the task
2176 * on the runqueue and wakes it.
2177 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002178void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002181 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002185 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 p->prio = effective_prio(p);
2188
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002189 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002190 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002193 * Let the scheduling class do new task startup
2194 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002196 p->sched_class->task_new(rq, p);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002197 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002199 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002200#ifdef CONFIG_SMP
2201 if (p->sched_class->task_wake_up)
2202 p->sched_class->task_wake_up(rq, p);
2203#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002204 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
Avi Kivitye107be32007-07-26 13:40:43 +02002207#ifdef CONFIG_PREEMPT_NOTIFIERS
2208
2209/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002210 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2211 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002212 */
2213void preempt_notifier_register(struct preempt_notifier *notifier)
2214{
2215 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2216}
2217EXPORT_SYMBOL_GPL(preempt_notifier_register);
2218
2219/**
2220 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002221 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002222 *
2223 * This is safe to call from within a preemption notifier.
2224 */
2225void preempt_notifier_unregister(struct preempt_notifier *notifier)
2226{
2227 hlist_del(&notifier->link);
2228}
2229EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2230
2231static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2232{
2233 struct preempt_notifier *notifier;
2234 struct hlist_node *node;
2235
2236 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2237 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2238}
2239
2240static void
2241fire_sched_out_preempt_notifiers(struct task_struct *curr,
2242 struct task_struct *next)
2243{
2244 struct preempt_notifier *notifier;
2245 struct hlist_node *node;
2246
2247 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2248 notifier->ops->sched_out(notifier, next);
2249}
2250
2251#else
2252
2253static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2254{
2255}
2256
2257static void
2258fire_sched_out_preempt_notifiers(struct task_struct *curr,
2259 struct task_struct *next)
2260{
2261}
2262
2263#endif
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002266 * prepare_task_switch - prepare to switch tasks
2267 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002268 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002269 * @next: the task we are going to switch to.
2270 *
2271 * This is called with the rq lock held and interrupts off. It must
2272 * be paired with a subsequent finish_task_switch after the context
2273 * switch.
2274 *
2275 * prepare_task_switch sets up locking and calls architecture specific
2276 * hooks.
2277 */
Avi Kivitye107be32007-07-26 13:40:43 +02002278static inline void
2279prepare_task_switch(struct rq *rq, struct task_struct *prev,
2280 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002281{
Avi Kivitye107be32007-07-26 13:40:43 +02002282 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002283 prepare_lock_switch(rq, next);
2284 prepare_arch_switch(next);
2285}
2286
2287/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002289 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 * @prev: the thread we just switched away from.
2291 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002292 * finish_task_switch must be called after the context switch, paired
2293 * with a prepare_task_switch call before the context switch.
2294 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2295 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 *
2297 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002298 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 * with the lock held can cause deadlocks; see schedule() for
2300 * details.)
2301 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002302static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 __releases(rq->lock)
2304{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002306 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 rq->prev_mm = NULL;
2309
2310 /*
2311 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002312 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002313 * schedule one last time. The schedule call will never return, and
2314 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002315 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 * still held, otherwise prev could be scheduled on another cpu, die
2317 * there before we look at prev->state, and then the reference would
2318 * be dropped twice.
2319 * Manfred Spraul <manfred@colorfullife.com>
2320 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002321 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002322 finish_arch_switch(prev);
2323 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002324#ifdef CONFIG_SMP
2325 if (current->sched_class->post_schedule)
2326 current->sched_class->post_schedule(rq);
2327#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002328
Avi Kivitye107be32007-07-26 13:40:43 +02002329 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (mm)
2331 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002332 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002333 /*
2334 * Remove function-return probe instances associated with this
2335 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002336 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002337 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
2342/**
2343 * schedule_tail - first thing a freshly forked thread must call.
2344 * @prev: the thread we just switched away from.
2345 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002346asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 __releases(rq->lock)
2348{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002349 struct rq *rq = this_rq();
2350
Nick Piggin4866cde2005-06-25 14:57:23 -07002351 finish_task_switch(rq, prev);
2352#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2353 /* In this case, finish_task_switch does not reenable preemption */
2354 preempt_enable();
2355#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002357 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
2360/*
2361 * context_switch - switch to the new MM and the new
2362 * thread's register state.
2363 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002364static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002365context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002366 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
Ingo Molnardd41f592007-07-09 18:51:59 +02002368 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Avi Kivitye107be32007-07-26 13:40:43 +02002370 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002371 mm = next->mm;
2372 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002373 /*
2374 * For paravirt, this is coupled with an exit in switch_to to
2375 * combine the page table reload and the switch backend into
2376 * one hypercall.
2377 */
2378 arch_enter_lazy_cpu_mode();
2379
Ingo Molnardd41f592007-07-09 18:51:59 +02002380 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 next->active_mm = oldmm;
2382 atomic_inc(&oldmm->mm_count);
2383 enter_lazy_tlb(oldmm, next);
2384 } else
2385 switch_mm(oldmm, mm, next);
2386
Ingo Molnardd41f592007-07-09 18:51:59 +02002387 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 rq->prev_mm = oldmm;
2390 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002391 /*
2392 * Since the runqueue lock will be released by the next
2393 * task (which is an invalid locking op but in the case
2394 * of the scheduler it's an obvious special-case), so we
2395 * do an early lockdep release here:
2396 */
2397#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002398 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002399#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 /* Here we just switch the register state and the stack. */
2402 switch_to(prev, next, prev);
2403
Ingo Molnardd41f592007-07-09 18:51:59 +02002404 barrier();
2405 /*
2406 * this_rq must be evaluated again because prev may have moved
2407 * CPUs since it called schedule(), thus the 'rq' on its stack
2408 * frame will be invalid.
2409 */
2410 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
2413/*
2414 * nr_running, nr_uninterruptible and nr_context_switches:
2415 *
2416 * externally visible scheduler statistics: current number of runnable
2417 * threads, current number of uninterruptible-sleeping threads, total
2418 * number of context switches performed since bootup.
2419 */
2420unsigned long nr_running(void)
2421{
2422 unsigned long i, sum = 0;
2423
2424 for_each_online_cpu(i)
2425 sum += cpu_rq(i)->nr_running;
2426
2427 return sum;
2428}
2429
2430unsigned long nr_uninterruptible(void)
2431{
2432 unsigned long i, sum = 0;
2433
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002434 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 sum += cpu_rq(i)->nr_uninterruptible;
2436
2437 /*
2438 * Since we read the counters lockless, it might be slightly
2439 * inaccurate. Do not allow it to go below zero though:
2440 */
2441 if (unlikely((long)sum < 0))
2442 sum = 0;
2443
2444 return sum;
2445}
2446
2447unsigned long long nr_context_switches(void)
2448{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002449 int i;
2450 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002452 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 sum += cpu_rq(i)->nr_switches;
2454
2455 return sum;
2456}
2457
2458unsigned long nr_iowait(void)
2459{
2460 unsigned long i, sum = 0;
2461
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002462 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2464
2465 return sum;
2466}
2467
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002468unsigned long nr_active(void)
2469{
2470 unsigned long i, running = 0, uninterruptible = 0;
2471
2472 for_each_online_cpu(i) {
2473 running += cpu_rq(i)->nr_running;
2474 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2475 }
2476
2477 if (unlikely((long)uninterruptible < 0))
2478 uninterruptible = 0;
2479
2480 return running + uninterruptible;
2481}
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002484 * Update rq->cpu_load[] statistics. This function is usually called every
2485 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002486 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002487static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002488{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002489 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002490 int i, scale;
2491
2492 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002493
2494 /* Update our load: */
2495 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2496 unsigned long old_load, new_load;
2497
2498 /* scale is effectively 1 << i now, and >> i divides by scale */
2499
2500 old_load = this_rq->cpu_load[i];
2501 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002502 /*
2503 * Round up the averaging division if load is increasing. This
2504 * prevents us from getting stuck on 9 if the load is 10, for
2505 * example.
2506 */
2507 if (new_load > old_load)
2508 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002509 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2510 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002511}
2512
Ingo Molnardd41f592007-07-09 18:51:59 +02002513#ifdef CONFIG_SMP
2514
Ingo Molnar48f24c42006-07-03 00:25:40 -07002515/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 * double_rq_lock - safely lock two runqueues
2517 *
2518 * Note this does not disable interrupts like task_rq_lock,
2519 * you need to do so manually before calling.
2520 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002521static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 __acquires(rq1->lock)
2523 __acquires(rq2->lock)
2524{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002525 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (rq1 == rq2) {
2527 spin_lock(&rq1->lock);
2528 __acquire(rq2->lock); /* Fake it out ;) */
2529 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002530 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 spin_lock(&rq1->lock);
2532 spin_lock(&rq2->lock);
2533 } else {
2534 spin_lock(&rq2->lock);
2535 spin_lock(&rq1->lock);
2536 }
2537 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002538 update_rq_clock(rq1);
2539 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
2542/*
2543 * double_rq_unlock - safely unlock two runqueues
2544 *
2545 * Note this does not restore interrupts like task_rq_unlock,
2546 * you need to do so manually after calling.
2547 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002548static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 __releases(rq1->lock)
2550 __releases(rq2->lock)
2551{
2552 spin_unlock(&rq1->lock);
2553 if (rq1 != rq2)
2554 spin_unlock(&rq2->lock);
2555 else
2556 __release(rq2->lock);
2557}
2558
2559/*
2560 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2561 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002562static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 __releases(this_rq->lock)
2564 __acquires(busiest->lock)
2565 __acquires(this_rq->lock)
2566{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002567 int ret = 0;
2568
Kirill Korotaev054b9102006-12-10 02:20:11 -08002569 if (unlikely(!irqs_disabled())) {
2570 /* printk() doesn't work good under rq->lock */
2571 spin_unlock(&this_rq->lock);
2572 BUG_ON(1);
2573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002575 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 spin_unlock(&this_rq->lock);
2577 spin_lock(&busiest->lock);
2578 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002579 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 } else
2581 spin_lock(&busiest->lock);
2582 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002583 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
2586/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 * If dest_cpu is allowed for this process, migrate the task to it.
2588 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002589 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 * the cpu_allowed mask is restored.
2591 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002592static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002594 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002596 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 rq = task_rq_lock(p, &flags);
2599 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2600 || unlikely(cpu_is_offline(dest_cpu)))
2601 goto out;
2602
2603 /* force the process onto the specified CPU */
2604 if (migrate_task(p, dest_cpu, &req)) {
2605 /* Need to wait for migration thread (might exit: take ref). */
2606 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 get_task_struct(mt);
2609 task_rq_unlock(rq, &flags);
2610 wake_up_process(mt);
2611 put_task_struct(mt);
2612 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return;
2615 }
2616out:
2617 task_rq_unlock(rq, &flags);
2618}
2619
2620/*
Nick Piggin476d1392005-06-25 14:57:29 -07002621 * sched_exec - execve() is a valuable balancing opportunity, because at
2622 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 */
2624void sched_exec(void)
2625{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002627 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002629 if (new_cpu != this_cpu)
2630 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
2633/*
2634 * pull_task - move a task from a remote runqueue to the local runqueue.
2635 * Both runqueues must be locked.
2636 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002637static void pull_task(struct rq *src_rq, struct task_struct *p,
2638 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002640 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002642 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /*
2644 * Note that idle threads have a prio of MAX_PRIO, for this test
2645 * to be always true for them.
2646 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002647 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
2650/*
2651 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2652 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002653static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002654int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002655 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002656 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
2658 /*
2659 * We do not migrate tasks that are:
2660 * 1) running (obviously), or
2661 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2662 * 3) are cache-hot on their current CPU.
2663 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002664 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2665 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002667 }
Nick Piggin81026792005-06-25 14:57:07 -07002668 *all_pinned = 0;
2669
Ingo Molnarcc367732007-10-15 17:00:18 +02002670 if (task_running(rq, p)) {
2671 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002672 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Ingo Molnarda84d962007-10-15 17:00:18 +02002675 /*
2676 * Aggressive migration if:
2677 * 1) task is cache cold, or
2678 * 2) too many balance attempts have failed.
2679 */
2680
Ingo Molnar6bc16652007-10-15 17:00:18 +02002681 if (!task_hot(p, rq->clock, sd) ||
2682 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002683#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002684 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002685 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002686 schedstat_inc(p, se.nr_forced_migrations);
2687 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002688#endif
2689 return 1;
2690 }
2691
Ingo Molnarcc367732007-10-15 17:00:18 +02002692 if (task_hot(p, rq->clock, sd)) {
2693 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002694 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return 1;
2697}
2698
Peter Williamse1d14842007-10-24 18:23:51 +02002699static unsigned long
2700balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2701 unsigned long max_load_move, struct sched_domain *sd,
2702 enum cpu_idle_type idle, int *all_pinned,
2703 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002704{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002705 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02002706 struct task_struct *p;
2707 long rem_load_move = max_load_move;
2708
Peter Williamse1d14842007-10-24 18:23:51 +02002709 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002710 goto out;
2711
2712 pinned = 1;
2713
2714 /*
2715 * Start the load-balancing iterator:
2716 */
2717 p = iterator->start(iterator->arg);
2718next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002719 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002720 goto out;
2721 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002722 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02002723 * skip a task if it will be the highest priority task (i.e. smallest
2724 * prio value) on its new queue regardless of its load weight
2725 */
2726 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2727 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002728 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002729 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002730 p = iterator->next(iterator->arg);
2731 goto next;
2732 }
2733
2734 pull_task(busiest, p, this_rq, this_cpu);
2735 pulled++;
2736 rem_load_move -= p->se.load.weight;
2737
2738 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002739 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002740 */
Peter Williamse1d14842007-10-24 18:23:51 +02002741 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002742 if (p->prio < *this_best_prio)
2743 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002744 p = iterator->next(iterator->arg);
2745 goto next;
2746 }
2747out:
2748 /*
Peter Williamse1d14842007-10-24 18:23:51 +02002749 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02002750 * so we can safely collect pull_task() stats here rather than
2751 * inside pull_task().
2752 */
2753 schedstat_add(sd, lb_gained[idle], pulled);
2754
2755 if (all_pinned)
2756 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02002757
2758 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02002759}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761/*
Peter Williams43010652007-08-09 11:16:46 +02002762 * move_tasks tries to move up to max_load_move weighted load from busiest to
2763 * this_rq, as part of a balancing operation within domain "sd".
2764 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 *
2766 * Called with both runqueues locked.
2767 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002768static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002769 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002770 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002771 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002773 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002774 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002775 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Ingo Molnardd41f592007-07-09 18:51:59 +02002777 do {
Peter Williams43010652007-08-09 11:16:46 +02002778 total_load_moved +=
2779 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02002780 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002781 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002782 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002783 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Peter Williams43010652007-08-09 11:16:46 +02002785 return total_load_moved > 0;
2786}
2787
Peter Williamse1d14842007-10-24 18:23:51 +02002788static int
2789iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2790 struct sched_domain *sd, enum cpu_idle_type idle,
2791 struct rq_iterator *iterator)
2792{
2793 struct task_struct *p = iterator->start(iterator->arg);
2794 int pinned = 0;
2795
2796 while (p) {
2797 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
2798 pull_task(busiest, p, this_rq, this_cpu);
2799 /*
2800 * Right now, this is only the second place pull_task()
2801 * is called, so we can safely collect pull_task()
2802 * stats here rather than inside pull_task().
2803 */
2804 schedstat_inc(sd, lb_gained[idle]);
2805
2806 return 1;
2807 }
2808 p = iterator->next(iterator->arg);
2809 }
2810
2811 return 0;
2812}
2813
Peter Williams43010652007-08-09 11:16:46 +02002814/*
2815 * move_one_task tries to move exactly one task from busiest to this_rq, as
2816 * part of active balancing operations within "domain".
2817 * Returns 1 if successful and 0 otherwise.
2818 *
2819 * Called with both runqueues locked.
2820 */
2821static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2822 struct sched_domain *sd, enum cpu_idle_type idle)
2823{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002824 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02002825
2826 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02002827 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02002828 return 1;
2829
2830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
2832
2833/*
2834 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002835 * domain. It calculates and returns the amount of weighted load which
2836 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 */
2838static struct sched_group *
2839find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002840 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07002841 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
2843 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2844 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002845 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002846 unsigned long busiest_load_per_task, busiest_nr_running;
2847 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002848 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002849#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2850 int power_savings_balance = 1;
2851 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2852 unsigned long min_nr_running = ULONG_MAX;
2853 struct sched_group *group_min = NULL, *group_leader = NULL;
2854#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002857 busiest_load_per_task = busiest_nr_running = 0;
2858 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002859 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002860 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002861 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002862 load_idx = sd->newidle_idx;
2863 else
2864 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 do {
Ken Chen908a7c12007-10-17 16:55:11 +02002867 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 int local_group;
2869 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02002870 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002871 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002872 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 local_group = cpu_isset(this_cpu, group->cpumask);
2875
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002876 if (local_group)
2877 balance_cpu = first_cpu(group->cpumask);
2878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002880 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02002881 max_cpu_load = 0;
2882 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
2884 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002885 struct rq *rq;
2886
2887 if (!cpu_isset(i, *cpus))
2888 continue;
2889
2890 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002891
Suresh Siddha9439aab2007-07-19 21:28:35 +02002892 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002893 *sd_idle = 0;
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002896 if (local_group) {
2897 if (idle_cpu(i) && !first_idle_cpu) {
2898 first_idle_cpu = 1;
2899 balance_cpu = i;
2900 }
2901
Nick Piggina2000572006-02-10 01:51:02 -08002902 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002903 } else {
Nick Piggina2000572006-02-10 01:51:02 -08002904 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002905 if (load > max_cpu_load)
2906 max_cpu_load = load;
2907 if (min_cpu_load > load)
2908 min_cpu_load = load;
2909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002912 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002913 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
2915
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002916 /*
2917 * First idle cpu or the first cpu(busiest) in this sched group
2918 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002919 * domains. In the newly idle case, we will allow all the cpu's
2920 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002921 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002922 if (idle != CPU_NEWLY_IDLE && local_group &&
2923 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002924 *balance = 0;
2925 goto ret;
2926 }
2927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002929 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002932 avg_load = sg_div_cpu_power(group,
2933 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Ken Chen908a7c12007-10-17 16:55:11 +02002935 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
2936 __group_imb = 1;
2937
Eric Dumazet5517d862007-05-08 00:32:57 -07002938 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 if (local_group) {
2941 this_load = avg_load;
2942 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002943 this_nr_running = sum_nr_running;
2944 this_load_per_task = sum_weighted_load;
2945 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02002946 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 max_load = avg_load;
2948 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002949 busiest_nr_running = sum_nr_running;
2950 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02002951 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002953
2954#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2955 /*
2956 * Busy processors will not participate in power savings
2957 * balance.
2958 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002959 if (idle == CPU_NOT_IDLE ||
2960 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2961 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002962
2963 /*
2964 * If the local group is idle or completely loaded
2965 * no need to do power savings balance at this domain
2966 */
2967 if (local_group && (this_nr_running >= group_capacity ||
2968 !this_nr_running))
2969 power_savings_balance = 0;
2970
Ingo Molnardd41f592007-07-09 18:51:59 +02002971 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002972 * If a group is already running at full capacity or idle,
2973 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002974 */
2975 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002976 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002977 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002978
Ingo Molnardd41f592007-07-09 18:51:59 +02002979 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002980 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002981 * This is the group from where we need to pick up the load
2982 * for saving power
2983 */
2984 if ((sum_nr_running < min_nr_running) ||
2985 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002986 first_cpu(group->cpumask) <
2987 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002988 group_min = group;
2989 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002990 min_load_per_task = sum_weighted_load /
2991 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002992 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002993
Ingo Molnardd41f592007-07-09 18:51:59 +02002994 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002995 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002996 * capacity but still has some space to pick up some load
2997 * from other group and save more power
2998 */
2999 if (sum_nr_running <= group_capacity - 1) {
3000 if (sum_nr_running > leader_nr_running ||
3001 (sum_nr_running == leader_nr_running &&
3002 first_cpu(group->cpumask) >
3003 first_cpu(group_leader->cpumask))) {
3004 group_leader = group;
3005 leader_nr_running = sum_nr_running;
3006 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003007 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003008group_next:
3009#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 group = group->next;
3011 } while (group != sd->groups);
3012
Peter Williams2dd73a42006-06-27 02:54:34 -07003013 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 goto out_balanced;
3015
3016 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3017
3018 if (this_load >= avg_load ||
3019 100*max_load <= sd->imbalance_pct*this_load)
3020 goto out_balanced;
3021
Peter Williams2dd73a42006-06-27 02:54:34 -07003022 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003023 if (group_imb)
3024 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 /*
3027 * We're trying to get all the cpus to the average_load, so we don't
3028 * want to push ourselves above the average load, nor do we wish to
3029 * reduce the max loaded cpu below the average load, as either of these
3030 * actions would just result in more rebalancing later, and ping-pong
3031 * tasks around. Thus we look for the minimum possible imbalance.
3032 * Negative imbalances (*we* are more loaded than anyone else) will
3033 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003034 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 * appear as very large values with unsigned longs.
3036 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003037 if (max_load <= busiest_load_per_task)
3038 goto out_balanced;
3039
3040 /*
3041 * In the presence of smp nice balancing, certain scenarios can have
3042 * max load less than avg load(as we skip the groups at or below
3043 * its cpu_power, while calculating max_load..)
3044 */
3045 if (max_load < avg_load) {
3046 *imbalance = 0;
3047 goto small_imbalance;
3048 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003049
3050 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003051 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003054 *imbalance = min(max_pull * busiest->__cpu_power,
3055 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 / SCHED_LOAD_SCALE;
3057
Peter Williams2dd73a42006-06-27 02:54:34 -07003058 /*
3059 * if *imbalance is less than the average load per runnable task
3060 * there is no gaurantee that any tasks will be moved so we'll have
3061 * a think about bumping its value to force at least one task to be
3062 * moved
3063 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003064 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003065 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003066 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
Peter Williams2dd73a42006-06-27 02:54:34 -07003068small_imbalance:
3069 pwr_move = pwr_now = 0;
3070 imbn = 2;
3071 if (this_nr_running) {
3072 this_load_per_task /= this_nr_running;
3073 if (busiest_load_per_task > this_load_per_task)
3074 imbn = 1;
3075 } else
3076 this_load_per_task = SCHED_LOAD_SCALE;
3077
Ingo Molnardd41f592007-07-09 18:51:59 +02003078 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3079 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003080 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return busiest;
3082 }
3083
3084 /*
3085 * OK, we don't have enough imbalance to justify moving tasks,
3086 * however we may be able to increase total CPU power used by
3087 * moving them.
3088 */
3089
Eric Dumazet5517d862007-05-08 00:32:57 -07003090 pwr_now += busiest->__cpu_power *
3091 min(busiest_load_per_task, max_load);
3092 pwr_now += this->__cpu_power *
3093 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 pwr_now /= SCHED_LOAD_SCALE;
3095
3096 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003097 tmp = sg_div_cpu_power(busiest,
3098 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003100 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003101 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
3103 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003104 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003105 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003106 tmp = sg_div_cpu_power(this,
3107 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003109 tmp = sg_div_cpu_power(this,
3110 busiest_load_per_task * SCHED_LOAD_SCALE);
3111 pwr_move += this->__cpu_power *
3112 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 pwr_move /= SCHED_LOAD_SCALE;
3114
3115 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003116 if (pwr_move > pwr_now)
3117 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 }
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 return busiest;
3121
3122out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003123#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003124 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003125 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003127 if (this == group_leader && group_leader != group_min) {
3128 *imbalance = min_load_per_task;
3129 return group_min;
3130 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003131#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003132ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 *imbalance = 0;
3134 return NULL;
3135}
3136
3137/*
3138 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3139 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003140static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003141find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003142 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003144 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003145 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 int i;
3147
3148 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003149 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003150
3151 if (!cpu_isset(i, *cpus))
3152 continue;
3153
Ingo Molnar48f24c42006-07-03 00:25:40 -07003154 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003155 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Ingo Molnardd41f592007-07-09 18:51:59 +02003157 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003158 continue;
3159
Ingo Molnardd41f592007-07-09 18:51:59 +02003160 if (wl > max_load) {
3161 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003162 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
3164 }
3165
3166 return busiest;
3167}
3168
3169/*
Nick Piggin77391d72005-06-25 14:57:30 -07003170 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3171 * so long as it is large enough.
3172 */
3173#define MAX_PINNED_INTERVAL 512
3174
3175/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3177 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003179static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003180 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003181 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Peter Williams43010652007-08-09 11:16:46 +02003183 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003186 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003187 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003188
Mike Travis7c16ec52008-04-04 18:11:11 -07003189 cpus_setall(*cpus);
3190
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003191 /*
3192 * When power savings policy is enabled for the parent domain, idle
3193 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003194 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003195 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003196 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003197 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003198 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003199 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Ingo Molnar2d723762007-10-15 17:00:12 +02003201 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003203redo:
3204 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003205 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003206
Chen, Kenneth W06066712006-12-10 02:20:35 -08003207 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003208 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003209
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 if (!group) {
3211 schedstat_inc(sd, lb_nobusyg[idle]);
3212 goto out_balanced;
3213 }
3214
Mike Travis7c16ec52008-04-04 18:11:11 -07003215 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 if (!busiest) {
3217 schedstat_inc(sd, lb_nobusyq[idle]);
3218 goto out_balanced;
3219 }
3220
Nick Piggindb935db2005-06-25 14:57:11 -07003221 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223 schedstat_add(sd, lb_imbalance[idle], imbalance);
3224
Peter Williams43010652007-08-09 11:16:46 +02003225 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (busiest->nr_running > 1) {
3227 /*
3228 * Attempt to move tasks. If find_busiest_group has found
3229 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003230 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 * correctly treated as an imbalance.
3232 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003233 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003234 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003235 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003236 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003237 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003238 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003239
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003240 /*
3241 * some other cpu did the load balance for us.
3242 */
Peter Williams43010652007-08-09 11:16:46 +02003243 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003244 resched_cpu(this_cpu);
3245
Nick Piggin81026792005-06-25 14:57:07 -07003246 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003247 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003248 cpu_clear(cpu_of(busiest), *cpus);
3249 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003250 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003251 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
Nick Piggin81026792005-06-25 14:57:07 -07003254
Peter Williams43010652007-08-09 11:16:46 +02003255 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 schedstat_inc(sd, lb_failed[idle]);
3257 sd->nr_balance_failed++;
3258
3259 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003261 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003262
3263 /* don't kick the migration_thread, if the curr
3264 * task on busiest cpu can't be moved to this_cpu
3265 */
3266 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003267 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003268 all_pinned = 1;
3269 goto out_one_pinned;
3270 }
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 if (!busiest->active_balance) {
3273 busiest->active_balance = 1;
3274 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003275 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003277 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003278 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 wake_up_process(busiest->migration_thread);
3280
3281 /*
3282 * We've kicked active balancing, reset the failure
3283 * counter.
3284 */
Nick Piggin39507452005-06-25 14:57:09 -07003285 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 }
Nick Piggin81026792005-06-25 14:57:07 -07003287 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 sd->nr_balance_failed = 0;
3289
Nick Piggin81026792005-06-25 14:57:07 -07003290 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 /* We were unbalanced, so reset the balancing interval */
3292 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003293 } else {
3294 /*
3295 * If we've begun active balancing, start to back off. This
3296 * case may not be covered by the all_pinned logic if there
3297 * is only 1 task on the busy runqueue (because we don't call
3298 * move_tasks).
3299 */
3300 if (sd->balance_interval < sd->max_interval)
3301 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
3303
Peter Williams43010652007-08-09 11:16:46 +02003304 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003305 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003306 return -1;
Peter Williams43010652007-08-09 11:16:46 +02003307 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 schedstat_inc(sd, lb_balanced[idle]);
3311
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003312 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003313
3314out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003316 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3317 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 sd->balance_interval *= 2;
3319
Ingo Molnar48f24c42006-07-03 00:25:40 -07003320 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003321 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003322 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 return 0;
3324}
3325
3326/*
3327 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3328 * tasks if there is an imbalance.
3329 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003330 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 * this_rq is locked.
3332 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003333static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003334load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3335 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
3337 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003338 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003340 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003341 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003342 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003343
3344 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003345
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003346 /*
3347 * When power savings policy is enabled for the parent domain, idle
3348 * sibling can pick up load irrespective of busy siblings. In this case,
3349 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003350 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003351 */
3352 if (sd->flags & SD_SHARE_CPUPOWER &&
3353 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003354 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Ingo Molnar2d723762007-10-15 17:00:12 +02003356 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003357redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003358 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003359 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003361 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003362 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
3364
Mike Travis7c16ec52008-04-04 18:11:11 -07003365 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003366 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003367 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003368 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 }
3370
Nick Piggindb935db2005-06-25 14:57:11 -07003371 BUG_ON(busiest == this_rq);
3372
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003373 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003374
Peter Williams43010652007-08-09 11:16:46 +02003375 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003376 if (busiest->nr_running > 1) {
3377 /* Attempt to move tasks */
3378 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003379 /* this_rq->clock is already updated */
3380 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003381 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003382 imbalance, sd, CPU_NEWLY_IDLE,
3383 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003384 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003385
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003386 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003387 cpu_clear(cpu_of(busiest), *cpus);
3388 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003389 goto redo;
3390 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003391 }
3392
Peter Williams43010652007-08-09 11:16:46 +02003393 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003394 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003395 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3396 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003397 return -1;
3398 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003399 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Peter Williams43010652007-08-09 11:16:46 +02003401 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003402
3403out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003404 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003405 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003406 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003407 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003408 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003409
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003410 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411}
3412
3413/*
3414 * idle_balance is called by schedule() if this_cpu is about to become
3415 * idle. Attempts to pull tasks from other CPUs.
3416 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003417static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
3419 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003420 int pulled_task = -1;
3421 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003422 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
3424 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003425 unsigned long interval;
3426
3427 if (!(sd->flags & SD_LOAD_BALANCE))
3428 continue;
3429
3430 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003431 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003432 pulled_task = load_balance_newidle(this_cpu, this_rq,
3433 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003434
3435 interval = msecs_to_jiffies(sd->balance_interval);
3436 if (time_after(next_balance, sd->last_balance + interval))
3437 next_balance = sd->last_balance + interval;
3438 if (pulled_task)
3439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003441 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003442 /*
3443 * We are going idle. next_balance may be set based on
3444 * a busy processor. So reset next_balance.
3445 */
3446 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448}
3449
3450/*
3451 * active_load_balance is run by migration threads. It pushes running tasks
3452 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3453 * running on each physical CPU where possible, and avoids physical /
3454 * logical imbalances.
3455 *
3456 * Called with busiest_rq locked.
3457 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003458static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459{
Nick Piggin39507452005-06-25 14:57:09 -07003460 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003461 struct sched_domain *sd;
3462 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003463
Ingo Molnar48f24c42006-07-03 00:25:40 -07003464 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003465 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003466 return;
3467
3468 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470 /*
Nick Piggin39507452005-06-25 14:57:09 -07003471 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003472 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003473 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 */
Nick Piggin39507452005-06-25 14:57:09 -07003475 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Nick Piggin39507452005-06-25 14:57:09 -07003477 /* move a task from busiest_rq to target_rq */
3478 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003479 update_rq_clock(busiest_rq);
3480 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Nick Piggin39507452005-06-25 14:57:09 -07003482 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003483 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003484 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003485 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003486 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Ingo Molnar48f24c42006-07-03 00:25:40 -07003489 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003490 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Peter Williams43010652007-08-09 11:16:46 +02003492 if (move_one_task(target_rq, target_cpu, busiest_rq,
3493 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003494 schedstat_inc(sd, alb_pushed);
3495 else
3496 schedstat_inc(sd, alb_failed);
3497 }
Nick Piggin39507452005-06-25 14:57:09 -07003498 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003501#ifdef CONFIG_NO_HZ
3502static struct {
3503 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003504 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003505} nohz ____cacheline_aligned = {
3506 .load_balancer = ATOMIC_INIT(-1),
3507 .cpu_mask = CPU_MASK_NONE,
3508};
3509
Christoph Lameter7835b982006-12-10 02:20:22 -08003510/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003511 * This routine will try to nominate the ilb (idle load balancing)
3512 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3513 * load balancing on behalf of all those cpus. If all the cpus in the system
3514 * go into this tickless mode, then there will be no ilb owner (as there is
3515 * no need for one) and all the cpus will sleep till the next wakeup event
3516 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003517 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003518 * For the ilb owner, tick is not stopped. And this tick will be used
3519 * for idle load balancing. ilb owner will still be part of
3520 * nohz.cpu_mask..
3521 *
3522 * While stopping the tick, this cpu will become the ilb owner if there
3523 * is no other owner. And will be the owner till that cpu becomes busy
3524 * or if all cpus in the system stop their ticks at which point
3525 * there is no need for ilb owner.
3526 *
3527 * When the ilb owner becomes busy, it nominates another owner, during the
3528 * next busy scheduler_tick()
3529 */
3530int select_nohz_load_balancer(int stop_tick)
3531{
3532 int cpu = smp_processor_id();
3533
3534 if (stop_tick) {
3535 cpu_set(cpu, nohz.cpu_mask);
3536 cpu_rq(cpu)->in_nohz_recently = 1;
3537
3538 /*
3539 * If we are going offline and still the leader, give up!
3540 */
3541 if (cpu_is_offline(cpu) &&
3542 atomic_read(&nohz.load_balancer) == cpu) {
3543 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3544 BUG();
3545 return 0;
3546 }
3547
3548 /* time for ilb owner also to sleep */
3549 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3550 if (atomic_read(&nohz.load_balancer) == cpu)
3551 atomic_set(&nohz.load_balancer, -1);
3552 return 0;
3553 }
3554
3555 if (atomic_read(&nohz.load_balancer) == -1) {
3556 /* make me the ilb owner */
3557 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3558 return 1;
3559 } else if (atomic_read(&nohz.load_balancer) == cpu)
3560 return 1;
3561 } else {
3562 if (!cpu_isset(cpu, nohz.cpu_mask))
3563 return 0;
3564
3565 cpu_clear(cpu, nohz.cpu_mask);
3566
3567 if (atomic_read(&nohz.load_balancer) == cpu)
3568 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3569 BUG();
3570 }
3571 return 0;
3572}
3573#endif
3574
3575static DEFINE_SPINLOCK(balancing);
3576
3577/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003578 * It checks each scheduling domain to see if it is due to be balanced,
3579 * and initiates a balancing operation if so.
3580 *
3581 * Balancing parameters are set up in arch_init_sched_domains.
3582 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003583static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003584{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003585 int balance = 1;
3586 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003587 unsigned long interval;
3588 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003589 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003590 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003591 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003592 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003594 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (!(sd->flags & SD_LOAD_BALANCE))
3596 continue;
3597
3598 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003599 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 interval *= sd->busy_factor;
3601
3602 /* scale ms to jiffies */
3603 interval = msecs_to_jiffies(interval);
3604 if (unlikely(!interval))
3605 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003606 if (interval > HZ*NR_CPUS/10)
3607 interval = HZ*NR_CPUS/10;
3608
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Christoph Lameter08c183f2006-12-10 02:20:29 -08003610 if (sd->flags & SD_SERIALIZE) {
3611 if (!spin_trylock(&balancing))
3612 goto out;
3613 }
3614
Christoph Lameterc9819f42006-12-10 02:20:25 -08003615 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003616 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003617 /*
3618 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003619 * longer idle, or one of our SMT siblings is
3620 * not idle.
3621 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003622 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003624 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003626 if (sd->flags & SD_SERIALIZE)
3627 spin_unlock(&balancing);
3628out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003629 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003630 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003631 update_next_balance = 1;
3632 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003633
3634 /*
3635 * Stop the load balance at this level. There is another
3636 * CPU in our sched group which is doing load balancing more
3637 * actively.
3638 */
3639 if (!balance)
3640 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003642
3643 /*
3644 * next_balance will be updated only when there is a need.
3645 * When the cpu is attached to null domain for ex, it will not be
3646 * updated.
3647 */
3648 if (likely(update_next_balance))
3649 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003650}
3651
3652/*
3653 * run_rebalance_domains is triggered when needed from the scheduler tick.
3654 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3655 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3656 */
3657static void run_rebalance_domains(struct softirq_action *h)
3658{
Ingo Molnardd41f592007-07-09 18:51:59 +02003659 int this_cpu = smp_processor_id();
3660 struct rq *this_rq = cpu_rq(this_cpu);
3661 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3662 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003663
Ingo Molnardd41f592007-07-09 18:51:59 +02003664 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003665
3666#ifdef CONFIG_NO_HZ
3667 /*
3668 * If this cpu is the owner for idle load balancing, then do the
3669 * balancing on behalf of the other idle cpus whose ticks are
3670 * stopped.
3671 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003672 if (this_rq->idle_at_tick &&
3673 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003674 cpumask_t cpus = nohz.cpu_mask;
3675 struct rq *rq;
3676 int balance_cpu;
3677
Ingo Molnardd41f592007-07-09 18:51:59 +02003678 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003679 for_each_cpu_mask(balance_cpu, cpus) {
3680 /*
3681 * If this cpu gets work to do, stop the load balancing
3682 * work being done for other cpus. Next load
3683 * balancing owner will pick it up.
3684 */
3685 if (need_resched())
3686 break;
3687
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003688 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003689
3690 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003691 if (time_after(this_rq->next_balance, rq->next_balance))
3692 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003693 }
3694 }
3695#endif
3696}
3697
3698/*
3699 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3700 *
3701 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3702 * idle load balancing owner or decide to stop the periodic load balancing,
3703 * if the whole system is idle.
3704 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003705static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003706{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003707#ifdef CONFIG_NO_HZ
3708 /*
3709 * If we were in the nohz mode recently and busy at the current
3710 * scheduler tick, then check if we need to nominate new idle
3711 * load balancer.
3712 */
3713 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3714 rq->in_nohz_recently = 0;
3715
3716 if (atomic_read(&nohz.load_balancer) == cpu) {
3717 cpu_clear(cpu, nohz.cpu_mask);
3718 atomic_set(&nohz.load_balancer, -1);
3719 }
3720
3721 if (atomic_read(&nohz.load_balancer) == -1) {
3722 /*
3723 * simple selection for now: Nominate the
3724 * first cpu in the nohz list to be the next
3725 * ilb owner.
3726 *
3727 * TBD: Traverse the sched domains and nominate
3728 * the nearest cpu in the nohz.cpu_mask.
3729 */
3730 int ilb = first_cpu(nohz.cpu_mask);
3731
Mike Travis434d53b2008-04-04 18:11:04 -07003732 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003733 resched_cpu(ilb);
3734 }
3735 }
3736
3737 /*
3738 * If this cpu is idle and doing idle load balancing for all the
3739 * cpus with ticks stopped, is it time for that to stop?
3740 */
3741 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3742 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3743 resched_cpu(cpu);
3744 return;
3745 }
3746
3747 /*
3748 * If this cpu is idle and the idle load balancing is done by
3749 * someone else, then no need raise the SCHED_SOFTIRQ
3750 */
3751 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3752 cpu_isset(cpu, nohz.cpu_mask))
3753 return;
3754#endif
3755 if (time_after_eq(jiffies, rq->next_balance))
3756 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757}
Ingo Molnardd41f592007-07-09 18:51:59 +02003758
3759#else /* CONFIG_SMP */
3760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761/*
3762 * on UP we do not need to balance between CPUs:
3763 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003764static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765{
3766}
Ingo Molnardd41f592007-07-09 18:51:59 +02003767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768#endif
3769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770DEFINE_PER_CPU(struct kernel_stat, kstat);
3771
3772EXPORT_PER_CPU_SYMBOL(kstat);
3773
3774/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003775 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3776 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003778unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003781 u64 ns, delta_exec;
3782 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003783
Ingo Molnar41b86e92007-07-09 18:51:58 +02003784 rq = task_rq_lock(p, &flags);
3785 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003786 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003787 update_rq_clock(rq);
3788 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003789 if ((s64)delta_exec > 0)
3790 ns += delta_exec;
3791 }
3792 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003793
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 return ns;
3795}
3796
3797/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 * Account user cpu time to a process.
3799 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 * @cputime: the cpu time spent in user space since the last update
3801 */
3802void account_user_time(struct task_struct *p, cputime_t cputime)
3803{
3804 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3805 cputime64_t tmp;
3806
3807 p->utime = cputime_add(p->utime, cputime);
3808
3809 /* Add user time to cpustat. */
3810 tmp = cputime_to_cputime64(cputime);
3811 if (TASK_NICE(p) > 0)
3812 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3813 else
3814 cpustat->user = cputime64_add(cpustat->user, tmp);
3815}
3816
3817/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003818 * Account guest cpu time to a process.
3819 * @p: the process that the cpu time gets accounted to
3820 * @cputime: the cpu time spent in virtual machine since the last update
3821 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01003822static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02003823{
3824 cputime64_t tmp;
3825 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3826
3827 tmp = cputime_to_cputime64(cputime);
3828
3829 p->utime = cputime_add(p->utime, cputime);
3830 p->gtime = cputime_add(p->gtime, cputime);
3831
3832 cpustat->user = cputime64_add(cpustat->user, tmp);
3833 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3834}
3835
3836/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003837 * Account scaled user cpu time to a process.
3838 * @p: the process that the cpu time gets accounted to
3839 * @cputime: the cpu time spent in user space since the last update
3840 */
3841void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
3842{
3843 p->utimescaled = cputime_add(p->utimescaled, cputime);
3844}
3845
3846/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 * Account system cpu time to a process.
3848 * @p: the process that the cpu time gets accounted to
3849 * @hardirq_offset: the offset to subtract from hardirq_count()
3850 * @cputime: the cpu time spent in kernel space since the last update
3851 */
3852void account_system_time(struct task_struct *p, int hardirq_offset,
3853 cputime_t cputime)
3854{
3855 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003856 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 cputime64_t tmp;
3858
Christian Borntraeger97783852007-11-15 20:57:39 +01003859 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
3860 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003861
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 p->stime = cputime_add(p->stime, cputime);
3863
3864 /* Add system time to cpustat. */
3865 tmp = cputime_to_cputime64(cputime);
3866 if (hardirq_count() - hardirq_offset)
3867 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3868 else if (softirq_count())
3869 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003870 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003872 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3874 else
3875 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3876 /* Account for system time used */
3877 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878}
3879
3880/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003881 * Account scaled system cpu time to a process.
3882 * @p: the process that the cpu time gets accounted to
3883 * @hardirq_offset: the offset to subtract from hardirq_count()
3884 * @cputime: the cpu time spent in kernel space since the last update
3885 */
3886void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
3887{
3888 p->stimescaled = cputime_add(p->stimescaled, cputime);
3889}
3890
3891/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 * Account for involuntary wait time.
3893 * @p: the process from which the cpu time has been stolen
3894 * @steal: the cpu time spent in involuntary wait
3895 */
3896void account_steal_time(struct task_struct *p, cputime_t steal)
3897{
3898 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3899 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003900 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
3902 if (p == rq->idle) {
3903 p->stime = cputime_add(p->stime, steal);
3904 if (atomic_read(&rq->nr_iowait) > 0)
3905 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3906 else
3907 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003908 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3910}
3911
Christoph Lameter7835b982006-12-10 02:20:22 -08003912/*
3913 * This function gets called by the timer code, with HZ frequency.
3914 * We call it with interrupts disabled.
3915 *
3916 * It also gets called by the fork code, when changing the parent's
3917 * timeslices.
3918 */
3919void scheduler_tick(void)
3920{
Christoph Lameter7835b982006-12-10 02:20:22 -08003921 int cpu = smp_processor_id();
3922 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003923 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02003924 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08003925
Ingo Molnardd41f592007-07-09 18:51:59 +02003926 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02003927 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02003928 /*
3929 * Let rq->clock advance by at least TICK_NSEC:
3930 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003931 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02003932 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003933 rq->clock_underflows++;
3934 }
Ingo Molnar529c7722007-08-10 23:05:11 +02003935 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02003936 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003937 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003938 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02003939 spin_unlock(&rq->lock);
3940
Christoph Lametere418e1c2006-12-10 02:20:23 -08003941#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003942 rq->idle_at_tick = idle_cpu(cpu);
3943 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003944#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945}
3946
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3948
Srinivasa Ds43627582008-02-23 15:24:04 -08003949void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
3951 /*
3952 * Underflow?
3953 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003954 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3955 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 preempt_count() += val;
3957 /*
3958 * Spinlock count overflowing soon?
3959 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003960 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3961 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962}
3963EXPORT_SYMBOL(add_preempt_count);
3964
Srinivasa Ds43627582008-02-23 15:24:04 -08003965void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
3967 /*
3968 * Underflow?
3969 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003970 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3971 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 /*
3973 * Is the spinlock portion underflowing?
3974 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003975 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3976 !(preempt_count() & PREEMPT_MASK)))
3977 return;
3978
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 preempt_count() -= val;
3980}
3981EXPORT_SYMBOL(sub_preempt_count);
3982
3983#endif
3984
3985/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003986 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003988static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
Satyam Sharma838225b2007-10-24 18:23:50 +02003990 struct pt_regs *regs = get_irq_regs();
3991
3992 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3993 prev->comm, prev->pid, preempt_count());
3994
Ingo Molnardd41f592007-07-09 18:51:59 +02003995 debug_show_held_locks(prev);
3996 if (irqs_disabled())
3997 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02003998
3999 if (regs)
4000 show_regs(regs);
4001 else
4002 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Ingo Molnardd41f592007-07-09 18:51:59 +02004005/*
4006 * Various schedule()-time debugging checks and statistics:
4007 */
4008static inline void schedule_debug(struct task_struct *prev)
4009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004011 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 * schedule() atomically, we ignore that path for now.
4013 * Otherwise, whine if we are scheduling when we should not be.
4014 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004015 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4016 __schedule_bug(prev);
4017
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4019
Ingo Molnar2d723762007-10-15 17:00:12 +02004020 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004021#ifdef CONFIG_SCHEDSTATS
4022 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004023 schedstat_inc(this_rq(), bkl_count);
4024 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004025 }
4026#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004027}
4028
4029/*
4030 * Pick up the highest-prio task:
4031 */
4032static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004033pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004034{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004035 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004036 struct task_struct *p;
4037
4038 /*
4039 * Optimization: we know that if all tasks are in
4040 * the fair class we can call that function directly:
4041 */
4042 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004043 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004044 if (likely(p))
4045 return p;
4046 }
4047
4048 class = sched_class_highest;
4049 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004050 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004051 if (p)
4052 return p;
4053 /*
4054 * Will never be NULL as the idle class always
4055 * returns a non-NULL p:
4056 */
4057 class = class->next;
4058 }
4059}
4060
4061/*
4062 * schedule() is the main scheduler function.
4063 */
4064asmlinkage void __sched schedule(void)
4065{
4066 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004067 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004068 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004069 int cpu;
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071need_resched:
4072 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004073 cpu = smp_processor_id();
4074 rq = cpu_rq(cpu);
4075 rcu_qsctr_inc(cpu);
4076 prev = rq->curr;
4077 switch_count = &prev->nivcsw;
4078
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 release_kernel_lock(prev);
4080need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
Ingo Molnardd41f592007-07-09 18:51:59 +02004082 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004084 hrtick_clear(rq);
4085
Ingo Molnar1e819952007-10-15 17:00:13 +02004086 /*
4087 * Do the rq-clock update outside the rq lock:
4088 */
4089 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004090 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004091 spin_lock(&rq->lock);
4092 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Ingo Molnardd41f592007-07-09 18:51:59 +02004094 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4095 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004096 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004097 prev->state = TASK_RUNNING;
4098 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004099 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004100 }
4101 switch_count = &prev->nvcsw;
4102 }
4103
Steven Rostedt9a897c52008-01-25 21:08:22 +01004104#ifdef CONFIG_SMP
4105 if (prev->sched_class->pre_schedule)
4106 prev->sched_class->pre_schedule(rq, prev);
4107#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004108
Ingo Molnardd41f592007-07-09 18:51:59 +02004109 if (unlikely(!rq->nr_running))
4110 idle_balance(cpu, rq);
4111
Ingo Molnar31ee5292007-08-09 11:16:49 +02004112 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004113 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
4115 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02004116
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 rq->nr_switches++;
4119 rq->curr = next;
4120 ++*switch_count;
4121
Ingo Molnardd41f592007-07-09 18:51:59 +02004122 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004123 /*
4124 * the context switch might have flipped the stack from under
4125 * us, hence refresh the local variables.
4126 */
4127 cpu = smp_processor_id();
4128 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 } else
4130 spin_unlock_irq(&rq->lock);
4131
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004132 hrtick_set(rq);
4133
4134 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004136
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 preempt_enable_no_resched();
4138 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4139 goto need_resched;
4140}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141EXPORT_SYMBOL(schedule);
4142
4143#ifdef CONFIG_PREEMPT
4144/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004145 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004146 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 * occur there and call schedule directly.
4148 */
4149asmlinkage void __sched preempt_schedule(void)
4150{
4151 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 struct task_struct *task = current;
4153 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 /*
4156 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004157 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004159 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return;
4161
Andi Kleen3a5c3592007-10-15 17:00:14 +02004162 do {
4163 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Andi Kleen3a5c3592007-10-15 17:00:14 +02004165 /*
4166 * We keep the big kernel semaphore locked, but we
4167 * clear ->lock_depth so that schedule() doesnt
4168 * auto-release the semaphore:
4169 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004170 saved_lock_depth = task->lock_depth;
4171 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004172 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004173 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004174 sub_preempt_count(PREEMPT_ACTIVE);
4175
4176 /*
4177 * Check again in case we missed a preemption opportunity
4178 * between schedule and now.
4179 */
4180 barrier();
4181 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183EXPORT_SYMBOL(preempt_schedule);
4184
4185/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004186 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 * off of irq context.
4188 * Note, that this is called and return with irqs disabled. This will
4189 * protect us against recursive calling from irq.
4190 */
4191asmlinkage void __sched preempt_schedule_irq(void)
4192{
4193 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 struct task_struct *task = current;
4195 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004196
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004197 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 BUG_ON(ti->preempt_count || !irqs_disabled());
4199
Andi Kleen3a5c3592007-10-15 17:00:14 +02004200 do {
4201 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Andi Kleen3a5c3592007-10-15 17:00:14 +02004203 /*
4204 * We keep the big kernel semaphore locked, but we
4205 * clear ->lock_depth so that schedule() doesnt
4206 * auto-release the semaphore:
4207 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004208 saved_lock_depth = task->lock_depth;
4209 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004210 local_irq_enable();
4211 schedule();
4212 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004213 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004214 sub_preempt_count(PREEMPT_ACTIVE);
4215
4216 /*
4217 * Check again in case we missed a preemption opportunity
4218 * between schedule and now.
4219 */
4220 barrier();
4221 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222}
4223
4224#endif /* CONFIG_PREEMPT */
4225
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004226int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4227 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004229 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231EXPORT_SYMBOL(default_wake_function);
4232
4233/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004234 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4235 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 * number) then we wake all the non-exclusive tasks and one exclusive task.
4237 *
4238 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004239 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4241 */
4242static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4243 int nr_exclusive, int sync, void *key)
4244{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004245 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004247 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004248 unsigned flags = curr->flags;
4249
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004251 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 break;
4253 }
4254}
4255
4256/**
4257 * __wake_up - wake up threads blocked on a waitqueue.
4258 * @q: the waitqueue
4259 * @mode: which threads
4260 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004261 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004263void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004264 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265{
4266 unsigned long flags;
4267
4268 spin_lock_irqsave(&q->lock, flags);
4269 __wake_up_common(q, mode, nr_exclusive, 0, key);
4270 spin_unlock_irqrestore(&q->lock, flags);
4271}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272EXPORT_SYMBOL(__wake_up);
4273
4274/*
4275 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4276 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004277void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278{
4279 __wake_up_common(q, mode, 1, 0, NULL);
4280}
4281
4282/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004283 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 * @q: the waitqueue
4285 * @mode: which threads
4286 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4287 *
4288 * The sync wakeup differs that the waker knows that it will schedule
4289 * away soon, so while the target thread will be woken up, it will not
4290 * be migrated to another CPU - ie. the two threads are 'synchronized'
4291 * with each other. This can prevent needless bouncing between CPUs.
4292 *
4293 * On UP it can prevent extra preemption.
4294 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004295void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004296__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{
4298 unsigned long flags;
4299 int sync = 1;
4300
4301 if (unlikely(!q))
4302 return;
4303
4304 if (unlikely(!nr_exclusive))
4305 sync = 0;
4306
4307 spin_lock_irqsave(&q->lock, flags);
4308 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4309 spin_unlock_irqrestore(&q->lock, flags);
4310}
4311EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4312
Ingo Molnarb15136e2007-10-24 18:23:48 +02004313void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314{
4315 unsigned long flags;
4316
4317 spin_lock_irqsave(&x->wait.lock, flags);
4318 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004319 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 spin_unlock_irqrestore(&x->wait.lock, flags);
4321}
4322EXPORT_SYMBOL(complete);
4323
Ingo Molnarb15136e2007-10-24 18:23:48 +02004324void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325{
4326 unsigned long flags;
4327
4328 spin_lock_irqsave(&x->wait.lock, flags);
4329 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004330 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 spin_unlock_irqrestore(&x->wait.lock, flags);
4332}
4333EXPORT_SYMBOL(complete_all);
4334
Andi Kleen8cbbe862007-10-15 17:00:14 +02004335static inline long __sched
4336do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 if (!x->done) {
4339 DECLARE_WAITQUEUE(wait, current);
4340
4341 wait.flags |= WQ_FLAG_EXCLUSIVE;
4342 __add_wait_queue_tail(&x->wait, &wait);
4343 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004344 if ((state == TASK_INTERRUPTIBLE &&
4345 signal_pending(current)) ||
4346 (state == TASK_KILLABLE &&
4347 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004348 __remove_wait_queue(&x->wait, &wait);
4349 return -ERESTARTSYS;
4350 }
4351 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004353 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004355 if (!timeout) {
4356 __remove_wait_queue(&x->wait, &wait);
4357 return timeout;
4358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 } while (!x->done);
4360 __remove_wait_queue(&x->wait, &wait);
4361 }
4362 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004363 return timeout;
4364}
4365
4366static long __sched
4367wait_for_common(struct completion *x, long timeout, int state)
4368{
4369 might_sleep();
4370
4371 spin_lock_irq(&x->wait.lock);
4372 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004374 return timeout;
4375}
4376
Ingo Molnarb15136e2007-10-24 18:23:48 +02004377void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004378{
4379 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380}
4381EXPORT_SYMBOL(wait_for_completion);
4382
Ingo Molnarb15136e2007-10-24 18:23:48 +02004383unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4385{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004386 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387}
4388EXPORT_SYMBOL(wait_for_completion_timeout);
4389
Andi Kleen8cbbe862007-10-15 17:00:14 +02004390int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391{
Andi Kleen51e97992007-10-18 21:32:55 +02004392 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4393 if (t == -ERESTARTSYS)
4394 return t;
4395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396}
4397EXPORT_SYMBOL(wait_for_completion_interruptible);
4398
Ingo Molnarb15136e2007-10-24 18:23:48 +02004399unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400wait_for_completion_interruptible_timeout(struct completion *x,
4401 unsigned long timeout)
4402{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004403 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404}
4405EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4406
Matthew Wilcox009e5772007-12-06 12:29:54 -05004407int __sched wait_for_completion_killable(struct completion *x)
4408{
4409 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4410 if (t == -ERESTARTSYS)
4411 return t;
4412 return 0;
4413}
4414EXPORT_SYMBOL(wait_for_completion_killable);
4415
Andi Kleen8cbbe862007-10-15 17:00:14 +02004416static long __sched
4417sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004418{
4419 unsigned long flags;
4420 wait_queue_t wait;
4421
4422 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Andi Kleen8cbbe862007-10-15 17:00:14 +02004424 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Andi Kleen8cbbe862007-10-15 17:00:14 +02004426 spin_lock_irqsave(&q->lock, flags);
4427 __add_wait_queue(q, &wait);
4428 spin_unlock(&q->lock);
4429 timeout = schedule_timeout(timeout);
4430 spin_lock_irq(&q->lock);
4431 __remove_wait_queue(q, &wait);
4432 spin_unlock_irqrestore(&q->lock, flags);
4433
4434 return timeout;
4435}
4436
4437void __sched interruptible_sleep_on(wait_queue_head_t *q)
4438{
4439 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441EXPORT_SYMBOL(interruptible_sleep_on);
4442
Ingo Molnar0fec1712007-07-09 18:52:01 +02004443long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004444interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004446 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4449
Ingo Molnar0fec1712007-07-09 18:52:01 +02004450void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004452 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454EXPORT_SYMBOL(sleep_on);
4455
Ingo Molnar0fec1712007-07-09 18:52:01 +02004456long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004458 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460EXPORT_SYMBOL(sleep_on_timeout);
4461
Ingo Molnarb29739f2006-06-27 02:54:51 -07004462#ifdef CONFIG_RT_MUTEXES
4463
4464/*
4465 * rt_mutex_setprio - set the current priority of a task
4466 * @p: task
4467 * @prio: prio value (kernel-internal form)
4468 *
4469 * This function changes the 'effective' priority of a task. It does
4470 * not touch ->normal_prio like __setscheduler().
4471 *
4472 * Used by the rt_mutex code to implement priority inheritance logic.
4473 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004474void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004475{
4476 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004477 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004478 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004479 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004480
4481 BUG_ON(prio < 0 || prio > MAX_PRIO);
4482
4483 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004484 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004485
Andrew Mortond5f9f942007-05-08 20:27:06 -07004486 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004487 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004488 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004489 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004490 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004491 if (running)
4492 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004493
4494 if (rt_prio(prio))
4495 p->sched_class = &rt_sched_class;
4496 else
4497 p->sched_class = &fair_sched_class;
4498
Ingo Molnarb29739f2006-06-27 02:54:51 -07004499 p->prio = prio;
4500
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004501 if (running)
4502 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004503 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004504 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004505
4506 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004507 }
4508 task_rq_unlock(rq, &flags);
4509}
4510
4511#endif
4512
Ingo Molnar36c8b582006-07-03 00:25:41 -07004513void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514{
Ingo Molnardd41f592007-07-09 18:51:59 +02004515 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004517 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
4519 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4520 return;
4521 /*
4522 * We have to be careful, if called from sys_setpriority(),
4523 * the task might be in the middle of scheduling on another CPU.
4524 */
4525 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004526 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 /*
4528 * The RT priorities are set via sched_setscheduler(), but we still
4529 * allow the 'normal' nice value to be set - but as expected
4530 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004531 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004533 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 p->static_prio = NICE_TO_PRIO(nice);
4535 goto out_unlock;
4536 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004537 on_rq = p->se.on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004538 if (on_rq) {
Ingo Molnar69be72c2007-08-09 11:16:49 +02004539 dequeue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004540 dec_load(rq, p);
4541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004544 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004545 old_prio = p->prio;
4546 p->prio = effective_prio(p);
4547 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
Ingo Molnardd41f592007-07-09 18:51:59 +02004549 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004550 enqueue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004551 inc_load(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004553 * If the task increased its priority or is running and
4554 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004556 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 resched_task(rq->curr);
4558 }
4559out_unlock:
4560 task_rq_unlock(rq, &flags);
4561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562EXPORT_SYMBOL(set_user_nice);
4563
Matt Mackalle43379f2005-05-01 08:59:00 -07004564/*
4565 * can_nice - check if a task can reduce its nice value
4566 * @p: task
4567 * @nice: nice value
4568 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004569int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004570{
Matt Mackall024f4742005-08-18 11:24:19 -07004571 /* convert nice value [19,-20] to rlimit style value [1,40] */
4572 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004573
Matt Mackalle43379f2005-05-01 08:59:00 -07004574 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4575 capable(CAP_SYS_NICE));
4576}
4577
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578#ifdef __ARCH_WANT_SYS_NICE
4579
4580/*
4581 * sys_nice - change the priority of the current process.
4582 * @increment: priority increment
4583 *
4584 * sys_setpriority is a more generic, but much slower function that
4585 * does similar things.
4586 */
4587asmlinkage long sys_nice(int increment)
4588{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004589 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
4591 /*
4592 * Setpriority might change our priority at the same moment.
4593 * We don't have to worry. Conceptually one call occurs first
4594 * and we have a single winner.
4595 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004596 if (increment < -40)
4597 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 if (increment > 40)
4599 increment = 40;
4600
4601 nice = PRIO_TO_NICE(current->static_prio) + increment;
4602 if (nice < -20)
4603 nice = -20;
4604 if (nice > 19)
4605 nice = 19;
4606
Matt Mackalle43379f2005-05-01 08:59:00 -07004607 if (increment < 0 && !can_nice(current, nice))
4608 return -EPERM;
4609
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 retval = security_task_setnice(current, nice);
4611 if (retval)
4612 return retval;
4613
4614 set_user_nice(current, nice);
4615 return 0;
4616}
4617
4618#endif
4619
4620/**
4621 * task_prio - return the priority value of a given task.
4622 * @p: the task in question.
4623 *
4624 * This is the priority value as seen by users in /proc.
4625 * RT tasks are offset by -200. Normal tasks are centered
4626 * around 0, value goes from -16 to +15.
4627 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004628int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629{
4630 return p->prio - MAX_RT_PRIO;
4631}
4632
4633/**
4634 * task_nice - return the nice value of a given task.
4635 * @p: the task in question.
4636 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004637int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638{
4639 return TASK_NICE(p);
4640}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004641EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
4643/**
4644 * idle_cpu - is a given cpu idle currently?
4645 * @cpu: the processor in question.
4646 */
4647int idle_cpu(int cpu)
4648{
4649 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4650}
4651
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652/**
4653 * idle_task - return the idle task for a given cpu.
4654 * @cpu: the processor in question.
4655 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004656struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
4658 return cpu_rq(cpu)->idle;
4659}
4660
4661/**
4662 * find_process_by_pid - find a process with a matching PID value.
4663 * @pid: the pid in question.
4664 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004665static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004667 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668}
4669
4670/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004671static void
4672__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673{
Ingo Molnardd41f592007-07-09 18:51:59 +02004674 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004675
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004677 switch (p->policy) {
4678 case SCHED_NORMAL:
4679 case SCHED_BATCH:
4680 case SCHED_IDLE:
4681 p->sched_class = &fair_sched_class;
4682 break;
4683 case SCHED_FIFO:
4684 case SCHED_RR:
4685 p->sched_class = &rt_sched_class;
4686 break;
4687 }
4688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004690 p->normal_prio = normal_prio(p);
4691 /* we are holding p->pi_lock already */
4692 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004693 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694}
4695
4696/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004697 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 * @p: the task in question.
4699 * @policy: new policy.
4700 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004701 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004702 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004704int sched_setscheduler(struct task_struct *p, int policy,
4705 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004707 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01004709 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004710 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
Steven Rostedt66e53932006-06-27 02:54:44 -07004712 /* may grab non-irq protected spin_locks */
4713 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714recheck:
4715 /* double check policy once rq lock held */
4716 if (policy < 0)
4717 policy = oldpolicy = p->policy;
4718 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004719 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4720 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004721 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 /*
4723 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004724 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4725 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 */
4727 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004728 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004729 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004731 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 return -EINVAL;
4733
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004734 /*
4735 * Allow unprivileged RT tasks to decrease priority:
4736 */
4737 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004738 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004739 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004740
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004741 if (!lock_task_sighand(p, &flags))
4742 return -ESRCH;
4743 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4744 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004745
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004746 /* can't set/change the rt policy */
4747 if (policy != p->policy && !rlim_rtprio)
4748 return -EPERM;
4749
4750 /* can't increase priority */
4751 if (param->sched_priority > p->rt_priority &&
4752 param->sched_priority > rlim_rtprio)
4753 return -EPERM;
4754 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004755 /*
4756 * Like positive nice levels, dont allow tasks to
4757 * move out of SCHED_IDLE either:
4758 */
4759 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4760 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004761
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004762 /* can't change other user's priorities */
4763 if ((current->euid != p->euid) &&
4764 (current->euid != p->uid))
4765 return -EPERM;
4766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004768#ifdef CONFIG_RT_GROUP_SCHED
4769 /*
4770 * Do not allow realtime tasks into groups that have no runtime
4771 * assigned.
4772 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02004773 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004774 return -EPERM;
4775#endif
4776
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 retval = security_task_setscheduler(p, policy, param);
4778 if (retval)
4779 return retval;
4780 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004781 * make sure no PI-waiters arrive (or leave) while we are
4782 * changing the priority of the task:
4783 */
4784 spin_lock_irqsave(&p->pi_lock, flags);
4785 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 * To be able to change p->policy safely, the apropriate
4787 * runqueue lock must be held.
4788 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004789 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 /* recheck policy now with rq lock held */
4791 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4792 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004793 __task_rq_unlock(rq);
4794 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 goto recheck;
4796 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004797 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004798 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004799 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004800 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004801 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004802 if (running)
4803 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02004804
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004806 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02004807
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004808 if (running)
4809 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004810 if (on_rq) {
4811 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004812
4813 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004815 __task_rq_unlock(rq);
4816 spin_unlock_irqrestore(&p->pi_lock, flags);
4817
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004818 rt_mutex_adjust_pi(p);
4819
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 return 0;
4821}
4822EXPORT_SYMBOL_GPL(sched_setscheduler);
4823
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004824static int
4825do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 struct sched_param lparam;
4828 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004829 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
4831 if (!param || pid < 0)
4832 return -EINVAL;
4833 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4834 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004835
4836 rcu_read_lock();
4837 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004839 if (p != NULL)
4840 retval = sched_setscheduler(p, policy, &lparam);
4841 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004842
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 return retval;
4844}
4845
4846/**
4847 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4848 * @pid: the pid in question.
4849 * @policy: new policy.
4850 * @param: structure containing the new RT priority.
4851 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004852asmlinkage long
4853sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854{
Jason Baronc21761f2006-01-18 17:43:03 -08004855 /* negative values for policy are not valid */
4856 if (policy < 0)
4857 return -EINVAL;
4858
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 return do_sched_setscheduler(pid, policy, param);
4860}
4861
4862/**
4863 * sys_sched_setparam - set/change the RT priority of a thread
4864 * @pid: the pid in question.
4865 * @param: structure containing the new RT priority.
4866 */
4867asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4868{
4869 return do_sched_setscheduler(pid, -1, param);
4870}
4871
4872/**
4873 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4874 * @pid: the pid in question.
4875 */
4876asmlinkage long sys_sched_getscheduler(pid_t pid)
4877{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004878 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004879 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
4881 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004882 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
4884 retval = -ESRCH;
4885 read_lock(&tasklist_lock);
4886 p = find_process_by_pid(pid);
4887 if (p) {
4888 retval = security_task_getscheduler(p);
4889 if (!retval)
4890 retval = p->policy;
4891 }
4892 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 return retval;
4894}
4895
4896/**
4897 * sys_sched_getscheduler - get the RT priority of a thread
4898 * @pid: the pid in question.
4899 * @param: structure containing the RT priority.
4900 */
4901asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4902{
4903 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004904 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004905 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
4907 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004908 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
4910 read_lock(&tasklist_lock);
4911 p = find_process_by_pid(pid);
4912 retval = -ESRCH;
4913 if (!p)
4914 goto out_unlock;
4915
4916 retval = security_task_getscheduler(p);
4917 if (retval)
4918 goto out_unlock;
4919
4920 lp.sched_priority = p->rt_priority;
4921 read_unlock(&tasklist_lock);
4922
4923 /*
4924 * This one might sleep, we cannot do it with a spinlock held ...
4925 */
4926 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 return retval;
4929
4930out_unlock:
4931 read_unlock(&tasklist_lock);
4932 return retval;
4933}
4934
Mike Travisb53e9212008-04-04 18:11:08 -07004935long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07004938 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004939 struct task_struct *p;
4940 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004942 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 read_lock(&tasklist_lock);
4944
4945 p = find_process_by_pid(pid);
4946 if (!p) {
4947 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004948 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return -ESRCH;
4950 }
4951
4952 /*
4953 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004954 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 * usage count and then drop tasklist_lock.
4956 */
4957 get_task_struct(p);
4958 read_unlock(&tasklist_lock);
4959
4960 retval = -EPERM;
4961 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4962 !capable(CAP_SYS_NICE))
4963 goto out_unlock;
4964
David Quigleye7834f82006-06-23 02:03:59 -07004965 retval = security_task_setscheduler(p, 0, NULL);
4966 if (retval)
4967 goto out_unlock;
4968
Mike Travisf9a86fc2008-04-04 18:11:07 -07004969 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004971 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07004972 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Paul Menage8707d8b2007-10-18 23:40:22 -07004974 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07004975 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004976 if (!cpus_subset(new_mask, cpus_allowed)) {
4977 /*
4978 * We must have raced with a concurrent cpuset
4979 * update. Just reset the cpus_allowed to the
4980 * cpuset's cpus_allowed
4981 */
4982 new_mask = cpus_allowed;
4983 goto again;
4984 }
4985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986out_unlock:
4987 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004988 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 return retval;
4990}
4991
4992static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4993 cpumask_t *new_mask)
4994{
4995 if (len < sizeof(cpumask_t)) {
4996 memset(new_mask, 0, sizeof(cpumask_t));
4997 } else if (len > sizeof(cpumask_t)) {
4998 len = sizeof(cpumask_t);
4999 }
5000 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5001}
5002
5003/**
5004 * sys_sched_setaffinity - set the cpu affinity of a process
5005 * @pid: pid of the process
5006 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5007 * @user_mask_ptr: user-space pointer to the new cpu mask
5008 */
5009asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5010 unsigned long __user *user_mask_ptr)
5011{
5012 cpumask_t new_mask;
5013 int retval;
5014
5015 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5016 if (retval)
5017 return retval;
5018
Mike Travisb53e9212008-04-04 18:11:08 -07005019 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020}
5021
5022/*
5023 * Represents all cpu's present in the system
5024 * In systems capable of hotplug, this map could dynamically grow
5025 * as new cpu's are detected in the system via any platform specific
5026 * method, such as ACPI for e.g.
5027 */
5028
Andi Kleen4cef0c62006-01-11 22:44:57 +01005029cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030EXPORT_SYMBOL(cpu_present_map);
5031
5032#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005033cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005034EXPORT_SYMBOL(cpu_online_map);
5035
Andi Kleen4cef0c62006-01-11 22:44:57 +01005036cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005037EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038#endif
5039
5040long sched_getaffinity(pid_t pid, cpumask_t *mask)
5041{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005042 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005045 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 read_lock(&tasklist_lock);
5047
5048 retval = -ESRCH;
5049 p = find_process_by_pid(pid);
5050 if (!p)
5051 goto out_unlock;
5052
David Quigleye7834f82006-06-23 02:03:59 -07005053 retval = security_task_getscheduler(p);
5054 if (retval)
5055 goto out_unlock;
5056
Jack Steiner2f7016d2006-02-01 03:05:18 -08005057 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058
5059out_unlock:
5060 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005061 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
Ulrich Drepper9531b622007-08-09 11:16:46 +02005063 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064}
5065
5066/**
5067 * sys_sched_getaffinity - get the cpu affinity of a process
5068 * @pid: pid of the process
5069 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5070 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5071 */
5072asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5073 unsigned long __user *user_mask_ptr)
5074{
5075 int ret;
5076 cpumask_t mask;
5077
5078 if (len < sizeof(cpumask_t))
5079 return -EINVAL;
5080
5081 ret = sched_getaffinity(pid, &mask);
5082 if (ret < 0)
5083 return ret;
5084
5085 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5086 return -EFAULT;
5087
5088 return sizeof(cpumask_t);
5089}
5090
5091/**
5092 * sys_sched_yield - yield the current processor to other threads.
5093 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005094 * This function yields the current CPU to other tasks. If there are no
5095 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 */
5097asmlinkage long sys_sched_yield(void)
5098{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005099 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
Ingo Molnar2d723762007-10-15 17:00:12 +02005101 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005102 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
5104 /*
5105 * Since we are going to call schedule() anyway, there's
5106 * no need to preempt or enable interrupts:
5107 */
5108 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005109 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 _raw_spin_unlock(&rq->lock);
5111 preempt_enable_no_resched();
5112
5113 schedule();
5114
5115 return 0;
5116}
5117
Andrew Mortone7b38402006-06-30 01:56:00 -07005118static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005120#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5121 __might_sleep(__FILE__, __LINE__);
5122#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005123 /*
5124 * The BKS might be reacquired before we have dropped
5125 * PREEMPT_ACTIVE, which could trigger a second
5126 * cond_resched() call.
5127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 do {
5129 add_preempt_count(PREEMPT_ACTIVE);
5130 schedule();
5131 sub_preempt_count(PREEMPT_ACTIVE);
5132 } while (need_resched());
5133}
5134
Herbert Xu02b67cc32008-01-25 21:08:28 +01005135#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5136int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137{
Ingo Molnar94142322006-12-29 16:48:13 -08005138 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5139 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 __cond_resched();
5141 return 1;
5142 }
5143 return 0;
5144}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005145EXPORT_SYMBOL(_cond_resched);
5146#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
5148/*
5149 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5150 * call schedule, and on return reacquire the lock.
5151 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005152 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 * operations here to prevent schedule() from being called twice (once via
5154 * spin_unlock(), once by hand).
5155 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005156int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157{
Nick Piggin95c354f2008-01-30 13:31:20 +01005158 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005159 int ret = 0;
5160
Nick Piggin95c354f2008-01-30 13:31:20 +01005161 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005163 if (resched && need_resched())
5164 __cond_resched();
5165 else
5166 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005167 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005170 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172EXPORT_SYMBOL(cond_resched_lock);
5173
5174int __sched cond_resched_softirq(void)
5175{
5176 BUG_ON(!in_softirq());
5177
Ingo Molnar94142322006-12-29 16:48:13 -08005178 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005179 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 __cond_resched();
5181 local_bh_disable();
5182 return 1;
5183 }
5184 return 0;
5185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186EXPORT_SYMBOL(cond_resched_softirq);
5187
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188/**
5189 * yield - yield the current processor to other threads.
5190 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005191 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 * thread runnable and calls sys_sched_yield().
5193 */
5194void __sched yield(void)
5195{
5196 set_current_state(TASK_RUNNING);
5197 sys_sched_yield();
5198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199EXPORT_SYMBOL(yield);
5200
5201/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005202 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 * that process accounting knows that this is a task in IO wait state.
5204 *
5205 * But don't do that if it is a deliberate, throttling IO wait (this task
5206 * has set its backing_dev_info: the queue against which it should throttle)
5207 */
5208void __sched io_schedule(void)
5209{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005210 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005212 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 atomic_inc(&rq->nr_iowait);
5214 schedule();
5215 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005216 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218EXPORT_SYMBOL(io_schedule);
5219
5220long __sched io_schedule_timeout(long timeout)
5221{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005222 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 long ret;
5224
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005225 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 atomic_inc(&rq->nr_iowait);
5227 ret = schedule_timeout(timeout);
5228 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005229 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 return ret;
5231}
5232
5233/**
5234 * sys_sched_get_priority_max - return maximum RT priority.
5235 * @policy: scheduling class.
5236 *
5237 * this syscall returns the maximum rt_priority that can be used
5238 * by a given scheduling class.
5239 */
5240asmlinkage long sys_sched_get_priority_max(int policy)
5241{
5242 int ret = -EINVAL;
5243
5244 switch (policy) {
5245 case SCHED_FIFO:
5246 case SCHED_RR:
5247 ret = MAX_USER_RT_PRIO-1;
5248 break;
5249 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005250 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005251 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 ret = 0;
5253 break;
5254 }
5255 return ret;
5256}
5257
5258/**
5259 * sys_sched_get_priority_min - return minimum RT priority.
5260 * @policy: scheduling class.
5261 *
5262 * this syscall returns the minimum rt_priority that can be used
5263 * by a given scheduling class.
5264 */
5265asmlinkage long sys_sched_get_priority_min(int policy)
5266{
5267 int ret = -EINVAL;
5268
5269 switch (policy) {
5270 case SCHED_FIFO:
5271 case SCHED_RR:
5272 ret = 1;
5273 break;
5274 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005275 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005276 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 ret = 0;
5278 }
5279 return ret;
5280}
5281
5282/**
5283 * sys_sched_rr_get_interval - return the default timeslice of a process.
5284 * @pid: pid of the process.
5285 * @interval: userspace pointer to the timeslice value.
5286 *
5287 * this syscall writes the default timeslice value of a given process
5288 * into the user-space timespec buffer. A value of '0' means infinity.
5289 */
5290asmlinkage
5291long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5292{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005293 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005294 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005295 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
5298 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005299 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
5301 retval = -ESRCH;
5302 read_lock(&tasklist_lock);
5303 p = find_process_by_pid(pid);
5304 if (!p)
5305 goto out_unlock;
5306
5307 retval = security_task_getscheduler(p);
5308 if (retval)
5309 goto out_unlock;
5310
Ingo Molnar77034932007-12-04 17:04:39 +01005311 /*
5312 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5313 * tasks that are on an otherwise idle runqueue:
5314 */
5315 time_slice = 0;
5316 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005317 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005318 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005319 struct sched_entity *se = &p->se;
5320 unsigned long flags;
5321 struct rq *rq;
5322
5323 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005324 if (rq->cfs.load.weight)
5325 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005326 task_rq_unlock(rq, &flags);
5327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005329 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005332
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333out_unlock:
5334 read_unlock(&tasklist_lock);
5335 return retval;
5336}
5337
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005338static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005339
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005340void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005343 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005346 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005347 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005348#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005350 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005352 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353#else
5354 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005355 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005357 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358#endif
5359#ifdef CONFIG_DEBUG_STACK_USAGE
5360 {
Al Viro10ebffd2005-11-13 16:06:56 -08005361 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 while (!*n)
5363 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005364 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 }
5366#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005367 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005368 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005370 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371}
5372
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005373void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005375 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
Ingo Molnar4bd77322007-07-11 21:21:47 +02005377#if BITS_PER_LONG == 32
5378 printk(KERN_INFO
5379 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005381 printk(KERN_INFO
5382 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383#endif
5384 read_lock(&tasklist_lock);
5385 do_each_thread(g, p) {
5386 /*
5387 * reset the NMI-timeout, listing all files on a slow
5388 * console might take alot of time:
5389 */
5390 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005391 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005392 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 } while_each_thread(g, p);
5394
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005395 touch_all_softlockup_watchdogs();
5396
Ingo Molnardd41f592007-07-09 18:51:59 +02005397#ifdef CONFIG_SCHED_DEBUG
5398 sysrq_sched_debug_show();
5399#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005401 /*
5402 * Only show locks if all tasks are dumped:
5403 */
5404 if (state_filter == -1)
5405 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406}
5407
Ingo Molnar1df21052007-07-09 18:51:58 +02005408void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5409{
Ingo Molnardd41f592007-07-09 18:51:59 +02005410 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005411}
5412
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005413/**
5414 * init_idle - set up an idle thread for a given CPU
5415 * @idle: task in question
5416 * @cpu: cpu the idle task belongs to
5417 *
5418 * NOTE: this function does not set the idle thread's NEED_RESCHED
5419 * flag, to make booting more robust.
5420 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005421void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005423 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 unsigned long flags;
5425
Ingo Molnardd41f592007-07-09 18:51:59 +02005426 __sched_fork(idle);
5427 idle->se.exec_start = sched_clock();
5428
Ingo Molnarb29739f2006-06-27 02:54:51 -07005429 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005431 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
5433 spin_lock_irqsave(&rq->lock, flags);
5434 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005435#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5436 idle->oncpu = 1;
5437#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 spin_unlock_irqrestore(&rq->lock, flags);
5439
5440 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f542005-11-13 16:06:55 -08005441 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005442
Ingo Molnardd41f592007-07-09 18:51:59 +02005443 /*
5444 * The idle tasks have their own, simple scheduling class:
5445 */
5446 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447}
5448
5449/*
5450 * In a system that switches off the HZ timer nohz_cpu_mask
5451 * indicates which cpus entered this state. This is used
5452 * in the rcu update to wait only for active cpus. For system
5453 * which do not switch off the HZ timer nohz_cpu_mask should
5454 * always be CPU_MASK_NONE.
5455 */
5456cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5457
Ingo Molnar19978ca2007-11-09 22:39:38 +01005458/*
5459 * Increase the granularity value when there are more CPUs,
5460 * because with more CPUs the 'effective latency' as visible
5461 * to users decreases. But the relationship is not linear,
5462 * so pick a second-best guess by going with the log2 of the
5463 * number of CPUs.
5464 *
5465 * This idea comes from the SD scheduler of Con Kolivas:
5466 */
5467static inline void sched_init_granularity(void)
5468{
5469 unsigned int factor = 1 + ilog2(num_online_cpus());
5470 const unsigned long limit = 200000000;
5471
5472 sysctl_sched_min_granularity *= factor;
5473 if (sysctl_sched_min_granularity > limit)
5474 sysctl_sched_min_granularity = limit;
5475
5476 sysctl_sched_latency *= factor;
5477 if (sysctl_sched_latency > limit)
5478 sysctl_sched_latency = limit;
5479
5480 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005481}
5482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483#ifdef CONFIG_SMP
5484/*
5485 * This is how migration works:
5486 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005487 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 * runqueue and wake up that CPU's migration thread.
5489 * 2) we down() the locked semaphore => thread blocks.
5490 * 3) migration thread wakes up (implicitly it forces the migrated
5491 * thread off the CPU)
5492 * 4) it gets the migration request and checks whether the migrated
5493 * task is still in the wrong runqueue.
5494 * 5) if it's in the wrong runqueue then the migration thread removes
5495 * it and puts it into the right queue.
5496 * 6) migration thread up()s the semaphore.
5497 * 7) we wake up and the migration is done.
5498 */
5499
5500/*
5501 * Change a given task's CPU affinity. Migrate the thread to a
5502 * proper CPU and schedule it away if the CPU it's executing on
5503 * is removed from the allowed bitmask.
5504 *
5505 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005506 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 * call is not atomic; no spinlocks may be held.
5508 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005509int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005511 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005513 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005514 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
5516 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005517 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 ret = -EINVAL;
5519 goto out;
5520 }
5521
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005522 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005523 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005524 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005525 p->cpus_allowed = *new_mask;
5526 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005527 }
5528
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005530 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 goto out;
5532
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005533 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 /* Need help from migration thread: drop lock and wait. */
5535 task_rq_unlock(rq, &flags);
5536 wake_up_process(rq->migration_thread);
5537 wait_for_completion(&req.done);
5538 tlb_migrate_finish(p->mm);
5539 return 0;
5540 }
5541out:
5542 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005543
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 return ret;
5545}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005546EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
5548/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005549 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 * this because either it can't run here any more (set_cpus_allowed()
5551 * away from this CPU, or CPU going down), or because we're
5552 * attempting to rebalance this task on exec (sched_exec).
5553 *
5554 * So we race with normal scheduler movements, but that's OK, as long
5555 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005556 *
5557 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005559static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005561 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005562 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
5564 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005565 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
5567 rq_src = cpu_rq(src_cpu);
5568 rq_dest = cpu_rq(dest_cpu);
5569
5570 double_rq_lock(rq_src, rq_dest);
5571 /* Already moved. */
5572 if (task_cpu(p) != src_cpu)
5573 goto out;
5574 /* Affinity changed (again). */
5575 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5576 goto out;
5577
Ingo Molnardd41f592007-07-09 18:51:59 +02005578 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005579 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005580 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005581
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005583 if (on_rq) {
5584 activate_task(rq_dest, p, 0);
5585 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005587 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588out:
5589 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005590 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591}
5592
5593/*
5594 * migration_thread - this is a highprio system thread that performs
5595 * thread migration by bumping thread off CPU then 'pushing' onto
5596 * another runqueue.
5597 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005598static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005601 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
5603 rq = cpu_rq(cpu);
5604 BUG_ON(rq->migration_thread != current);
5605
5606 set_current_state(TASK_INTERRUPTIBLE);
5607 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005608 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 spin_lock_irq(&rq->lock);
5612
5613 if (cpu_is_offline(cpu)) {
5614 spin_unlock_irq(&rq->lock);
5615 goto wait_to_die;
5616 }
5617
5618 if (rq->active_balance) {
5619 active_load_balance(rq, cpu);
5620 rq->active_balance = 0;
5621 }
5622
5623 head = &rq->migration_queue;
5624
5625 if (list_empty(head)) {
5626 spin_unlock_irq(&rq->lock);
5627 schedule();
5628 set_current_state(TASK_INTERRUPTIBLE);
5629 continue;
5630 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005631 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 list_del_init(head->next);
5633
Nick Piggin674311d2005-06-25 14:57:27 -07005634 spin_unlock(&rq->lock);
5635 __migrate_task(req->task, cpu, req->dest_cpu);
5636 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
5638 complete(&req->done);
5639 }
5640 __set_current_state(TASK_RUNNING);
5641 return 0;
5642
5643wait_to_die:
5644 /* Wait for kthread_stop */
5645 set_current_state(TASK_INTERRUPTIBLE);
5646 while (!kthread_should_stop()) {
5647 schedule();
5648 set_current_state(TASK_INTERRUPTIBLE);
5649 }
5650 __set_current_state(TASK_RUNNING);
5651 return 0;
5652}
5653
5654#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005655
5656static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5657{
5658 int ret;
5659
5660 local_irq_disable();
5661 ret = __migrate_task(p, src_cpu, dest_cpu);
5662 local_irq_enable();
5663 return ret;
5664}
5665
Kirill Korotaev054b9102006-12-10 02:20:11 -08005666/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005667 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005668 * NOTE: interrupts should be disabled by the caller
5669 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005670static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005672 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005674 struct rq *rq;
5675 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676
Andi Kleen3a5c3592007-10-15 17:00:14 +02005677 do {
5678 /* On same node? */
5679 mask = node_to_cpumask(cpu_to_node(dead_cpu));
5680 cpus_and(mask, mask, p->cpus_allowed);
5681 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
Andi Kleen3a5c3592007-10-15 17:00:14 +02005683 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07005684 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005685 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
Andi Kleen3a5c3592007-10-15 17:00:14 +02005687 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07005688 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005689 cpumask_t cpus_allowed;
5690
5691 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07005692 /*
5693 * Try to stay on the same cpuset, where the
5694 * current cpuset may be a subset of all cpus.
5695 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005696 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07005697 * called within calls to cpuset_lock/cpuset_unlock.
5698 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02005699 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07005700 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005701 dest_cpu = any_online_cpu(p->cpus_allowed);
5702 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703
Andi Kleen3a5c3592007-10-15 17:00:14 +02005704 /*
5705 * Don't tell them about moving exiting tasks or
5706 * kernel threads (both mm NULL), since they never
5707 * leave kernel.
5708 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005709 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02005710 printk(KERN_INFO "process %d (%s) no "
5711 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005712 task_pid_nr(p), p->comm, dead_cpu);
5713 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02005714 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005715 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716}
5717
5718/*
5719 * While a dead CPU has no uninterruptible tasks queued at this point,
5720 * it might still have a nonzero ->nr_uninterruptible counter, because
5721 * for performance reasons the counter is not stricly tracking tasks to
5722 * their home CPUs. So we just add the counter to another CPU's counter,
5723 * to keep the global sum constant after CPU-down:
5724 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005725static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726{
Mike Travis7c16ec52008-04-04 18:11:11 -07005727 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 unsigned long flags;
5729
5730 local_irq_save(flags);
5731 double_rq_lock(rq_src, rq_dest);
5732 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5733 rq_src->nr_uninterruptible = 0;
5734 double_rq_unlock(rq_src, rq_dest);
5735 local_irq_restore(flags);
5736}
5737
5738/* Run through task list and migrate tasks from the dead cpu. */
5739static void migrate_live_tasks(int src_cpu)
5740{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005741 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005743 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744
Ingo Molnar48f24c42006-07-03 00:25:40 -07005745 do_each_thread(t, p) {
5746 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 continue;
5748
Ingo Molnar48f24c42006-07-03 00:25:40 -07005749 if (task_cpu(p) == src_cpu)
5750 move_task_off_dead_cpu(src_cpu, p);
5751 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005753 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754}
5755
Ingo Molnardd41f592007-07-09 18:51:59 +02005756/*
5757 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005758 * It does so by boosting its priority to highest possible.
5759 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 */
5761void sched_idle_next(void)
5762{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005763 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005764 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 struct task_struct *p = rq->idle;
5766 unsigned long flags;
5767
5768 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005769 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770
Ingo Molnar48f24c42006-07-03 00:25:40 -07005771 /*
5772 * Strictly not necessary since rest of the CPUs are stopped by now
5773 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 */
5775 spin_lock_irqsave(&rq->lock, flags);
5776
Ingo Molnardd41f592007-07-09 18:51:59 +02005777 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005778
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005779 update_rq_clock(rq);
5780 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
5782 spin_unlock_irqrestore(&rq->lock, flags);
5783}
5784
Ingo Molnar48f24c42006-07-03 00:25:40 -07005785/*
5786 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 * offline.
5788 */
5789void idle_task_exit(void)
5790{
5791 struct mm_struct *mm = current->active_mm;
5792
5793 BUG_ON(cpu_online(smp_processor_id()));
5794
5795 if (mm != &init_mm)
5796 switch_mm(mm, &init_mm, current);
5797 mmdrop(mm);
5798}
5799
Kirill Korotaev054b9102006-12-10 02:20:11 -08005800/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005801static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005803 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804
5805 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07005806 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
5808 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005809 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
Ingo Molnar48f24c42006-07-03 00:25:40 -07005811 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812
5813 /*
5814 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005815 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 * fine.
5817 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005818 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005819 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005820 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821
Ingo Molnar48f24c42006-07-03 00:25:40 -07005822 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823}
5824
5825/* release_task() removes task from tasklist, so we won't find dead tasks. */
5826static void migrate_dead_tasks(unsigned int dead_cpu)
5827{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005828 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005829 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
Ingo Molnardd41f592007-07-09 18:51:59 +02005831 for ( ; ; ) {
5832 if (!rq->nr_running)
5833 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005834 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02005835 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02005836 if (!next)
5837 break;
5838 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005839
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 }
5841}
5842#endif /* CONFIG_HOTPLUG_CPU */
5843
Nick Piggine692ab52007-07-26 13:40:43 +02005844#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5845
5846static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005847 {
5848 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005849 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005850 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005851 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005852};
5853
5854static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005855 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005856 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005857 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005858 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005859 .child = sd_ctl_dir,
5860 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005861 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005862};
5863
5864static struct ctl_table *sd_alloc_ctl_entry(int n)
5865{
5866 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005867 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005868
Nick Piggine692ab52007-07-26 13:40:43 +02005869 return entry;
5870}
5871
Milton Miller6382bc92007-10-15 17:00:19 +02005872static void sd_free_ctl_entry(struct ctl_table **tablep)
5873{
Milton Millercd7900762007-10-17 16:55:11 +02005874 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005875
Milton Millercd7900762007-10-17 16:55:11 +02005876 /*
5877 * In the intermediate directories, both the child directory and
5878 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005879 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02005880 * static strings and all have proc handlers.
5881 */
5882 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005883 if (entry->child)
5884 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02005885 if (entry->proc_handler == NULL)
5886 kfree(entry->procname);
5887 }
Milton Miller6382bc92007-10-15 17:00:19 +02005888
5889 kfree(*tablep);
5890 *tablep = NULL;
5891}
5892
Nick Piggine692ab52007-07-26 13:40:43 +02005893static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005894set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005895 const char *procname, void *data, int maxlen,
5896 mode_t mode, proc_handler *proc_handler)
5897{
Nick Piggine692ab52007-07-26 13:40:43 +02005898 entry->procname = procname;
5899 entry->data = data;
5900 entry->maxlen = maxlen;
5901 entry->mode = mode;
5902 entry->proc_handler = proc_handler;
5903}
5904
5905static struct ctl_table *
5906sd_alloc_ctl_domain_table(struct sched_domain *sd)
5907{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005908 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02005909
Milton Millerad1cdc12007-10-15 17:00:19 +02005910 if (table == NULL)
5911 return NULL;
5912
Alexey Dobriyane0361852007-08-09 11:16:46 +02005913 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005914 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005915 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005916 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005917 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005918 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005919 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005920 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005921 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005922 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005923 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005924 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005925 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005926 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005927 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005928 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005929 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005930 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005931 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005932 &sd->cache_nice_tries,
5933 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005934 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005935 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02005936 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005937
5938 return table;
5939}
5940
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005941static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005942{
5943 struct ctl_table *entry, *table;
5944 struct sched_domain *sd;
5945 int domain_num = 0, i;
5946 char buf[32];
5947
5948 for_each_domain(cpu, sd)
5949 domain_num++;
5950 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005951 if (table == NULL)
5952 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005953
5954 i = 0;
5955 for_each_domain(cpu, sd) {
5956 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005957 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005958 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005959 entry->child = sd_alloc_ctl_domain_table(sd);
5960 entry++;
5961 i++;
5962 }
5963 return table;
5964}
5965
5966static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005967static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005968{
5969 int i, cpu_num = num_online_cpus();
5970 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5971 char buf[32];
5972
Milton Miller73785472007-10-24 18:23:48 +02005973 WARN_ON(sd_ctl_dir[0].child);
5974 sd_ctl_dir[0].child = entry;
5975
Milton Millerad1cdc12007-10-15 17:00:19 +02005976 if (entry == NULL)
5977 return;
5978
Milton Miller97b6ea72007-10-15 17:00:19 +02005979 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005980 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005981 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005982 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005983 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005984 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005985 }
Milton Miller73785472007-10-24 18:23:48 +02005986
5987 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005988 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5989}
Milton Miller6382bc92007-10-15 17:00:19 +02005990
Milton Miller73785472007-10-24 18:23:48 +02005991/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005992static void unregister_sched_domain_sysctl(void)
5993{
Milton Miller73785472007-10-24 18:23:48 +02005994 if (sd_sysctl_header)
5995 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005996 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005997 if (sd_ctl_dir[0].child)
5998 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005999}
Nick Piggine692ab52007-07-26 13:40:43 +02006000#else
Milton Miller6382bc92007-10-15 17:00:19 +02006001static void register_sched_domain_sysctl(void)
6002{
6003}
6004static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006005{
6006}
6007#endif
6008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009/*
6010 * migration_call - callback that gets triggered when a CPU is added.
6011 * Here we can start up the necessary migration thread for the new CPU.
6012 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006013static int __cpuinit
6014migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006017 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006019 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
6021 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006022
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006024 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006025 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 if (IS_ERR(p))
6027 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 kthread_bind(p, cpu);
6029 /* Must be high prio: stop_machine expects to yield to it. */
6030 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006031 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 task_rq_unlock(rq, &flags);
6033 cpu_rq(cpu)->migration_thread = p;
6034 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006035
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006037 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006038 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006040
6041 /* Update our root-domain */
6042 rq = cpu_rq(cpu);
6043 spin_lock_irqsave(&rq->lock, flags);
6044 if (rq->rd) {
6045 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6046 cpu_set(cpu, rq->rd->online);
6047 }
6048 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006050
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051#ifdef CONFIG_HOTPLUG_CPU
6052 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006053 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006054 if (!cpu_rq(cpu)->migration_thread)
6055 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006056 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006057 kthread_bind(cpu_rq(cpu)->migration_thread,
6058 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 kthread_stop(cpu_rq(cpu)->migration_thread);
6060 cpu_rq(cpu)->migration_thread = NULL;
6061 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006062
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006064 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006065 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 migrate_live_tasks(cpu);
6067 rq = cpu_rq(cpu);
6068 kthread_stop(rq->migration_thread);
6069 rq->migration_thread = NULL;
6070 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006071 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006072 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006073 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006075 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6076 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006078 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006079 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 migrate_nr_uninterruptible(rq);
6081 BUG_ON(rq->nr_running != 0);
6082
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006083 /*
6084 * No need to migrate the tasks: it was best-effort if
6085 * they didn't take sched_hotcpu_mutex. Just wake up
6086 * the requestors.
6087 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 spin_lock_irq(&rq->lock);
6089 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006090 struct migration_req *req;
6091
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006093 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 list_del_init(&req->list);
6095 complete(&req->done);
6096 }
6097 spin_unlock_irq(&rq->lock);
6098 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006099
Gregory Haskins08f503b2008-03-10 17:59:11 -04006100 case CPU_DYING:
6101 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006102 /* Update our root-domain */
6103 rq = cpu_rq(cpu);
6104 spin_lock_irqsave(&rq->lock, flags);
6105 if (rq->rd) {
6106 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6107 cpu_clear(cpu, rq->rd->online);
6108 }
6109 spin_unlock_irqrestore(&rq->lock, flags);
6110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111#endif
6112 }
6113 return NOTIFY_OK;
6114}
6115
6116/* Register at highest priority so that task migration (migrate_all_tasks)
6117 * happens before everything else.
6118 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006119static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 .notifier_call = migration_call,
6121 .priority = 10
6122};
6123
Adrian Bunke6fe6642007-11-09 22:39:39 +01006124void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125{
6126 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006127 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006128
6129 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006130 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6131 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6133 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134}
6135#endif
6136
6137#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006138
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006139#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006140
Mike Travis7c16ec52008-04-04 18:11:11 -07006141static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6142 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006143{
6144 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006145 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006146
Mike Travis434d53b2008-04-04 18:11:04 -07006147 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006148 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006149
6150 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6151
6152 if (!(sd->flags & SD_LOAD_BALANCE)) {
6153 printk("does not load-balance\n");
6154 if (sd->parent)
6155 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6156 " has parent");
6157 return -1;
6158 }
6159
6160 printk(KERN_CONT "span %s\n", str);
6161
6162 if (!cpu_isset(cpu, sd->span)) {
6163 printk(KERN_ERR "ERROR: domain->span does not contain "
6164 "CPU%d\n", cpu);
6165 }
6166 if (!cpu_isset(cpu, group->cpumask)) {
6167 printk(KERN_ERR "ERROR: domain->groups does not contain"
6168 " CPU%d\n", cpu);
6169 }
6170
6171 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6172 do {
6173 if (!group) {
6174 printk("\n");
6175 printk(KERN_ERR "ERROR: group is NULL\n");
6176 break;
6177 }
6178
6179 if (!group->__cpu_power) {
6180 printk(KERN_CONT "\n");
6181 printk(KERN_ERR "ERROR: domain->cpu_power not "
6182 "set\n");
6183 break;
6184 }
6185
6186 if (!cpus_weight(group->cpumask)) {
6187 printk(KERN_CONT "\n");
6188 printk(KERN_ERR "ERROR: empty group\n");
6189 break;
6190 }
6191
Mike Travis7c16ec52008-04-04 18:11:11 -07006192 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006193 printk(KERN_CONT "\n");
6194 printk(KERN_ERR "ERROR: repeated CPUs\n");
6195 break;
6196 }
6197
Mike Travis7c16ec52008-04-04 18:11:11 -07006198 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006199
Mike Travis434d53b2008-04-04 18:11:04 -07006200 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006201 printk(KERN_CONT " %s", str);
6202
6203 group = group->next;
6204 } while (group != sd->groups);
6205 printk(KERN_CONT "\n");
6206
Mike Travis7c16ec52008-04-04 18:11:11 -07006207 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006208 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6209
Mike Travis7c16ec52008-04-04 18:11:11 -07006210 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006211 printk(KERN_ERR "ERROR: parent span is not a superset "
6212 "of domain->span\n");
6213 return 0;
6214}
6215
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216static void sched_domain_debug(struct sched_domain *sd, int cpu)
6217{
Mike Travis7c16ec52008-04-04 18:11:11 -07006218 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 int level = 0;
6220
Nick Piggin41c7ce92005-06-25 14:57:24 -07006221 if (!sd) {
6222 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6223 return;
6224 }
6225
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6227
Mike Travis7c16ec52008-04-04 18:11:11 -07006228 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6229 if (!groupmask) {
6230 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6231 return;
6232 }
6233
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006234 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006235 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 level++;
6238 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006239 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006240 break;
6241 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006242 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243}
6244#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006245# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246#endif
6247
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006248static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006249{
6250 if (cpus_weight(sd->span) == 1)
6251 return 1;
6252
6253 /* Following flags need at least 2 groups */
6254 if (sd->flags & (SD_LOAD_BALANCE |
6255 SD_BALANCE_NEWIDLE |
6256 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006257 SD_BALANCE_EXEC |
6258 SD_SHARE_CPUPOWER |
6259 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006260 if (sd->groups != sd->groups->next)
6261 return 0;
6262 }
6263
6264 /* Following flags don't use groups */
6265 if (sd->flags & (SD_WAKE_IDLE |
6266 SD_WAKE_AFFINE |
6267 SD_WAKE_BALANCE))
6268 return 0;
6269
6270 return 1;
6271}
6272
Ingo Molnar48f24c42006-07-03 00:25:40 -07006273static int
6274sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006275{
6276 unsigned long cflags = sd->flags, pflags = parent->flags;
6277
6278 if (sd_degenerate(parent))
6279 return 1;
6280
6281 if (!cpus_equal(sd->span, parent->span))
6282 return 0;
6283
6284 /* Does parent contain flags not in child? */
6285 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6286 if (cflags & SD_WAKE_AFFINE)
6287 pflags &= ~SD_WAKE_BALANCE;
6288 /* Flags needing groups don't count if only 1 group in parent */
6289 if (parent->groups == parent->groups->next) {
6290 pflags &= ~(SD_LOAD_BALANCE |
6291 SD_BALANCE_NEWIDLE |
6292 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006293 SD_BALANCE_EXEC |
6294 SD_SHARE_CPUPOWER |
6295 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006296 }
6297 if (~cflags & pflags)
6298 return 0;
6299
6300 return 1;
6301}
6302
Gregory Haskins57d885f2008-01-25 21:08:18 +01006303static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6304{
6305 unsigned long flags;
6306 const struct sched_class *class;
6307
6308 spin_lock_irqsave(&rq->lock, flags);
6309
6310 if (rq->rd) {
6311 struct root_domain *old_rd = rq->rd;
6312
Ingo Molnar0eab9142008-01-25 21:08:19 +01006313 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006314 if (class->leave_domain)
6315 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006316 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006317
Gregory Haskinsdc938522008-01-25 21:08:26 +01006318 cpu_clear(rq->cpu, old_rd->span);
6319 cpu_clear(rq->cpu, old_rd->online);
6320
Gregory Haskins57d885f2008-01-25 21:08:18 +01006321 if (atomic_dec_and_test(&old_rd->refcount))
6322 kfree(old_rd);
6323 }
6324
6325 atomic_inc(&rd->refcount);
6326 rq->rd = rd;
6327
Gregory Haskinsdc938522008-01-25 21:08:26 +01006328 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006329 if (cpu_isset(rq->cpu, cpu_online_map))
6330 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006331
Ingo Molnar0eab9142008-01-25 21:08:19 +01006332 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006333 if (class->join_domain)
6334 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006335 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006336
6337 spin_unlock_irqrestore(&rq->lock, flags);
6338}
6339
Gregory Haskinsdc938522008-01-25 21:08:26 +01006340static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006341{
6342 memset(rd, 0, sizeof(*rd));
6343
Gregory Haskinsdc938522008-01-25 21:08:26 +01006344 cpus_clear(rd->span);
6345 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006346}
6347
6348static void init_defrootdomain(void)
6349{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006350 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006351 atomic_set(&def_root_domain.refcount, 1);
6352}
6353
Gregory Haskinsdc938522008-01-25 21:08:26 +01006354static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006355{
6356 struct root_domain *rd;
6357
6358 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6359 if (!rd)
6360 return NULL;
6361
Gregory Haskinsdc938522008-01-25 21:08:26 +01006362 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006363
6364 return rd;
6365}
6366
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006368 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 * hold the hotplug lock.
6370 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006371static void
6372cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006374 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006375 struct sched_domain *tmp;
6376
6377 /* Remove the sched domains which do not contribute to scheduling. */
6378 for (tmp = sd; tmp; tmp = tmp->parent) {
6379 struct sched_domain *parent = tmp->parent;
6380 if (!parent)
6381 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006382 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006383 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006384 if (parent->parent)
6385 parent->parent->child = tmp;
6386 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006387 }
6388
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006389 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006390 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006391 if (sd)
6392 sd->child = NULL;
6393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394
6395 sched_domain_debug(sd, cpu);
6396
Gregory Haskins57d885f2008-01-25 21:08:18 +01006397 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006398 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399}
6400
6401/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006402static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403
6404/* Setup the mask of cpus configured for isolated domains */
6405static int __init isolated_cpu_setup(char *str)
6406{
6407 int ints[NR_CPUS], i;
6408
6409 str = get_options(str, ARRAY_SIZE(ints), ints);
6410 cpus_clear(cpu_isolated_map);
6411 for (i = 1; i <= ints[0]; i++)
6412 if (ints[i] < NR_CPUS)
6413 cpu_set(ints[i], cpu_isolated_map);
6414 return 1;
6415}
6416
Ingo Molnar8927f492007-10-15 17:00:13 +02006417__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418
6419/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006420 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6421 * to a function which identifies what group(along with sched group) a CPU
6422 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6423 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 *
6425 * init_sched_build_groups will build a circular linked list of the groups
6426 * covered by the given span, and will set each group's ->cpumask correctly,
6427 * and ->cpu_power to 0.
6428 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006429static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006430init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006431 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006432 struct sched_group **sg,
6433 cpumask_t *tmpmask),
6434 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435{
6436 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 int i;
6438
Mike Travis7c16ec52008-04-04 18:11:11 -07006439 cpus_clear(*covered);
6440
6441 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006442 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006443 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 int j;
6445
Mike Travis7c16ec52008-04-04 18:11:11 -07006446 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 continue;
6448
Mike Travis7c16ec52008-04-04 18:11:11 -07006449 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006450 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451
Mike Travis7c16ec52008-04-04 18:11:11 -07006452 for_each_cpu_mask(j, *span) {
6453 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 continue;
6455
Mike Travis7c16ec52008-04-04 18:11:11 -07006456 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 cpu_set(j, sg->cpumask);
6458 }
6459 if (!first)
6460 first = sg;
6461 if (last)
6462 last->next = sg;
6463 last = sg;
6464 }
6465 last->next = first;
6466}
6467
John Hawkes9c1cfda2005-09-06 15:18:14 -07006468#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469
John Hawkes9c1cfda2005-09-06 15:18:14 -07006470#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006471
John Hawkes9c1cfda2005-09-06 15:18:14 -07006472/**
6473 * find_next_best_node - find the next node to include in a sched_domain
6474 * @node: node whose sched_domain we're building
6475 * @used_nodes: nodes already in the sched_domain
6476 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006477 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006478 * finds the closest node not already in the @used_nodes map.
6479 *
6480 * Should use nodemask_t.
6481 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006482static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006483{
6484 int i, n, val, min_val, best_node = 0;
6485
6486 min_val = INT_MAX;
6487
6488 for (i = 0; i < MAX_NUMNODES; i++) {
6489 /* Start at @node */
6490 n = (node + i) % MAX_NUMNODES;
6491
6492 if (!nr_cpus_node(n))
6493 continue;
6494
6495 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006496 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006497 continue;
6498
6499 /* Simple min distance search */
6500 val = node_distance(node, n);
6501
6502 if (val < min_val) {
6503 min_val = val;
6504 best_node = n;
6505 }
6506 }
6507
Mike Travisc5f59f02008-04-04 18:11:10 -07006508 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006509 return best_node;
6510}
6511
6512/**
6513 * sched_domain_node_span - get a cpumask for a node's sched_domain
6514 * @node: node whose cpumask we're constructing
John Hawkes9c1cfda2005-09-06 15:18:14 -07006515 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006516 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006517 * should be one that prevents unnecessary balancing, but also spreads tasks
6518 * out optimally.
6519 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07006520static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006521{
Mike Travisc5f59f02008-04-04 18:11:10 -07006522 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07006523 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006524 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006525
Mike Travis4bdbaad32008-04-15 16:35:52 -07006526 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07006527 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006528
Mike Travis4bdbaad32008-04-15 16:35:52 -07006529 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07006530 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006531
6532 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07006533 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006534
Mike Travisc5f59f02008-04-04 18:11:10 -07006535 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07006536 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006537 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006538}
6539#endif
6540
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006541int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006542
John Hawkes9c1cfda2005-09-06 15:18:14 -07006543/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006544 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546#ifdef CONFIG_SCHED_SMT
6547static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006548static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006549
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006550static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006551cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6552 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006554 if (sg)
6555 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 return cpu;
6557}
6558#endif
6559
Ingo Molnar48f24c42006-07-03 00:25:40 -07006560/*
6561 * multi-core sched-domains:
6562 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006563#ifdef CONFIG_SCHED_MC
6564static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006565static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006566#endif
6567
6568#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006569static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006570cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6571 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006572{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006573 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07006574
6575 *mask = per_cpu(cpu_sibling_map, cpu);
6576 cpus_and(*mask, *mask, *cpu_map);
6577 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006578 if (sg)
6579 *sg = &per_cpu(sched_group_core, group);
6580 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006581}
6582#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006583static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006584cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6585 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006586{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006587 if (sg)
6588 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006589 return cpu;
6590}
6591#endif
6592
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006594static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006595
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006596static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006597cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6598 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006600 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006601#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07006602 *mask = cpu_coregroup_map(cpu);
6603 cpus_and(*mask, *mask, *cpu_map);
6604 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006605#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07006606 *mask = per_cpu(cpu_sibling_map, cpu);
6607 cpus_and(*mask, *mask, *cpu_map);
6608 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006610 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006612 if (sg)
6613 *sg = &per_cpu(sched_group_phys, group);
6614 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615}
6616
6617#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006618/*
6619 * The init_sched_build_groups can't handle what we want to do with node
6620 * groups, so roll our own. Now each node has its own list of groups which
6621 * gets dynamically allocated.
6622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07006624static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006625
6626static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006627static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006628
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006629static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006630 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006632 int group;
6633
Mike Travis7c16ec52008-04-04 18:11:11 -07006634 *nodemask = node_to_cpumask(cpu_to_node(cpu));
6635 cpus_and(*nodemask, *nodemask, *cpu_map);
6636 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006637
6638 if (sg)
6639 *sg = &per_cpu(sched_group_allnodes, group);
6640 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006642
Siddha, Suresh B08069032006-03-27 01:15:23 -08006643static void init_numa_sched_groups_power(struct sched_group *group_head)
6644{
6645 struct sched_group *sg = group_head;
6646 int j;
6647
6648 if (!sg)
6649 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006650 do {
6651 for_each_cpu_mask(j, sg->cpumask) {
6652 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006653
Andi Kleen3a5c3592007-10-15 17:00:14 +02006654 sd = &per_cpu(phys_domains, j);
6655 if (j != first_cpu(sd->groups->cpumask)) {
6656 /*
6657 * Only add "power" once for each
6658 * physical package.
6659 */
6660 continue;
6661 }
6662
6663 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006664 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006665 sg = sg->next;
6666 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006667}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668#endif
6669
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006670#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006671/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07006672static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006673{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006674 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006675
6676 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006677 struct sched_group **sched_group_nodes
6678 = sched_group_nodes_bycpu[cpu];
6679
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006680 if (!sched_group_nodes)
6681 continue;
6682
6683 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006684 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6685
Mike Travis7c16ec52008-04-04 18:11:11 -07006686 *nodemask = node_to_cpumask(i);
6687 cpus_and(*nodemask, *nodemask, *cpu_map);
6688 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006689 continue;
6690
6691 if (sg == NULL)
6692 continue;
6693 sg = sg->next;
6694next_sg:
6695 oldsg = sg;
6696 sg = sg->next;
6697 kfree(oldsg);
6698 if (oldsg != sched_group_nodes[i])
6699 goto next_sg;
6700 }
6701 kfree(sched_group_nodes);
6702 sched_group_nodes_bycpu[cpu] = NULL;
6703 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006704}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006705#else
Mike Travis7c16ec52008-04-04 18:11:11 -07006706static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006707{
6708}
6709#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006710
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006712 * Initialize sched groups cpu_power.
6713 *
6714 * cpu_power indicates the capacity of sched group, which is used while
6715 * distributing the load between different sched groups in a sched domain.
6716 * Typically cpu_power for all the groups in a sched domain will be same unless
6717 * there are asymmetries in the topology. If there are asymmetries, group
6718 * having more cpu_power will pickup more load compared to the group having
6719 * less cpu_power.
6720 *
6721 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
6722 * the maximum number of tasks a group can handle in the presence of other idle
6723 * or lightly loaded groups in the same sched domain.
6724 */
6725static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6726{
6727 struct sched_domain *child;
6728 struct sched_group *group;
6729
6730 WARN_ON(!sd || !sd->groups);
6731
6732 if (cpu != first_cpu(sd->groups->cpumask))
6733 return;
6734
6735 child = sd->child;
6736
Eric Dumazet5517d862007-05-08 00:32:57 -07006737 sd->groups->__cpu_power = 0;
6738
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006739 /*
6740 * For perf policy, if the groups in child domain share resources
6741 * (for example cores sharing some portions of the cache hierarchy
6742 * or SMT), then set this domain groups cpu_power such that each group
6743 * can handle only one task, when there are other idle groups in the
6744 * same sched domain.
6745 */
6746 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
6747 (child->flags &
6748 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07006749 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006750 return;
6751 }
6752
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006753 /*
6754 * add cpu_power of each child group to this groups cpu_power
6755 */
6756 group = child->groups;
6757 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07006758 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006759 group = group->next;
6760 } while (group != child->groups);
6761}
6762
6763/*
Mike Travis7c16ec52008-04-04 18:11:11 -07006764 * Initializers for schedule domains
6765 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6766 */
6767
6768#define SD_INIT(sd, type) sd_init_##type(sd)
6769#define SD_INIT_FUNC(type) \
6770static noinline void sd_init_##type(struct sched_domain *sd) \
6771{ \
6772 memset(sd, 0, sizeof(*sd)); \
6773 *sd = SD_##type##_INIT; \
6774}
6775
6776SD_INIT_FUNC(CPU)
6777#ifdef CONFIG_NUMA
6778 SD_INIT_FUNC(ALLNODES)
6779 SD_INIT_FUNC(NODE)
6780#endif
6781#ifdef CONFIG_SCHED_SMT
6782 SD_INIT_FUNC(SIBLING)
6783#endif
6784#ifdef CONFIG_SCHED_MC
6785 SD_INIT_FUNC(MC)
6786#endif
6787
6788/*
6789 * To minimize stack usage kmalloc room for cpumasks and share the
6790 * space as the usage in build_sched_domains() dictates. Used only
6791 * if the amount of space is significant.
6792 */
6793struct allmasks {
6794 cpumask_t tmpmask; /* make this one first */
6795 union {
6796 cpumask_t nodemask;
6797 cpumask_t this_sibling_map;
6798 cpumask_t this_core_map;
6799 };
6800 cpumask_t send_covered;
6801
6802#ifdef CONFIG_NUMA
6803 cpumask_t domainspan;
6804 cpumask_t covered;
6805 cpumask_t notcovered;
6806#endif
6807};
6808
6809#if NR_CPUS > 128
6810#define SCHED_CPUMASK_ALLOC 1
6811#define SCHED_CPUMASK_FREE(v) kfree(v)
6812#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
6813#else
6814#define SCHED_CPUMASK_ALLOC 0
6815#define SCHED_CPUMASK_FREE(v)
6816#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
6817#endif
6818
6819#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
6820 ((unsigned long)(a) + offsetof(struct allmasks, v))
6821
6822/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006823 * Build sched domains for a given set of cpus and attach the sched domains
6824 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006826static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827{
6828 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006829 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07006830 SCHED_CPUMASK_DECLARE(allmasks);
6831 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07006832#ifdef CONFIG_NUMA
6833 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006834 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07006835
6836 /*
6837 * Allocate the per-node list of sched groups
6838 */
Milton Miller5cf9f062007-10-15 17:00:19 +02006839 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006840 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006841 if (!sched_group_nodes) {
6842 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006843 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006844 }
John Hawkesd1b55132005-09-06 15:18:14 -07006845#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
Gregory Haskinsdc938522008-01-25 21:08:26 +01006847 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01006848 if (!rd) {
6849 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07006850#ifdef CONFIG_NUMA
6851 kfree(sched_group_nodes);
6852#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01006853 return -ENOMEM;
6854 }
6855
Mike Travis7c16ec52008-04-04 18:11:11 -07006856#if SCHED_CPUMASK_ALLOC
6857 /* get space for all scratch cpumask variables */
6858 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
6859 if (!allmasks) {
6860 printk(KERN_WARNING "Cannot alloc cpumask array\n");
6861 kfree(rd);
6862#ifdef CONFIG_NUMA
6863 kfree(sched_group_nodes);
6864#endif
6865 return -ENOMEM;
6866 }
6867#endif
6868 tmpmask = (cpumask_t *)allmasks;
6869
6870
6871#ifdef CONFIG_NUMA
6872 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6873#endif
6874
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006876 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006878 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07006880 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
Mike Travis7c16ec52008-04-04 18:11:11 -07006882 *nodemask = node_to_cpumask(cpu_to_node(i));
6883 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884
6885#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006886 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07006887 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006888 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07006889 SD_INIT(sd, ALLNODES);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006890 sd->span = *cpu_map;
Mike Travis7c16ec52008-04-04 18:11:11 -07006891 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006892 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006893 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006894 } else
6895 p = NULL;
6896
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07006898 SD_INIT(sd, NODE);
Mike Travis4bdbaad32008-04-15 16:35:52 -07006899 sched_domain_node_span(cpu_to_node(i), &sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006900 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006901 if (p)
6902 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006903 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904#endif
6905
6906 p = sd;
6907 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07006908 SD_INIT(sd, CPU);
6909 sd->span = *nodemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006911 if (p)
6912 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07006913 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006915#ifdef CONFIG_SCHED_MC
6916 p = sd;
6917 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07006918 SD_INIT(sd, MC);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006919 sd->span = cpu_coregroup_map(i);
6920 cpus_and(sd->span, sd->span, *cpu_map);
6921 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006922 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07006923 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006924#endif
6925
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926#ifdef CONFIG_SCHED_SMT
6927 p = sd;
6928 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07006929 SD_INIT(sd, SIBLING);
Mike Travisd5a74302007-10-16 01:24:05 -07006930 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006931 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006933 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07006934 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935#endif
6936 }
6937
6938#ifdef CONFIG_SCHED_SMT
6939 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006940 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006941 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
6942 SCHED_CPUMASK_VAR(send_covered, allmasks);
6943
6944 *this_sibling_map = per_cpu(cpu_sibling_map, i);
6945 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
6946 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 continue;
6948
Ingo Molnardd41f592007-07-09 18:51:59 +02006949 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006950 &cpu_to_cpu_group,
6951 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 }
6953#endif
6954
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006955#ifdef CONFIG_SCHED_MC
6956 /* Set up multi-core groups */
6957 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006958 SCHED_CPUMASK_VAR(this_core_map, allmasks);
6959 SCHED_CPUMASK_VAR(send_covered, allmasks);
6960
6961 *this_core_map = cpu_coregroup_map(i);
6962 cpus_and(*this_core_map, *this_core_map, *cpu_map);
6963 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006964 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07006965
Ingo Molnardd41f592007-07-09 18:51:59 +02006966 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006967 &cpu_to_core_group,
6968 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006969 }
6970#endif
6971
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 /* Set up physical groups */
6973 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006974 SCHED_CPUMASK_VAR(nodemask, allmasks);
6975 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976
Mike Travis7c16ec52008-04-04 18:11:11 -07006977 *nodemask = node_to_cpumask(i);
6978 cpus_and(*nodemask, *nodemask, *cpu_map);
6979 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 continue;
6981
Mike Travis7c16ec52008-04-04 18:11:11 -07006982 init_sched_build_groups(nodemask, cpu_map,
6983 &cpu_to_phys_group,
6984 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 }
6986
6987#ifdef CONFIG_NUMA
6988 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07006989 if (sd_allnodes) {
6990 SCHED_CPUMASK_VAR(send_covered, allmasks);
6991
6992 init_sched_build_groups(cpu_map, cpu_map,
6993 &cpu_to_allnodes_group,
6994 send_covered, tmpmask);
6995 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006996
6997 for (i = 0; i < MAX_NUMNODES; i++) {
6998 /* Set up node groups */
6999 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007000 SCHED_CPUMASK_VAR(nodemask, allmasks);
7001 SCHED_CPUMASK_VAR(domainspan, allmasks);
7002 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007003 int j;
7004
Mike Travis7c16ec52008-04-04 18:11:11 -07007005 *nodemask = node_to_cpumask(i);
7006 cpus_clear(*covered);
7007
7008 cpus_and(*nodemask, *nodemask, *cpu_map);
7009 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007010 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007011 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007012 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007013
Mike Travis4bdbaad32008-04-15 16:35:52 -07007014 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007015 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007016
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007017 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007018 if (!sg) {
7019 printk(KERN_WARNING "Can not alloc domain group for "
7020 "node %d\n", i);
7021 goto error;
7022 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007023 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007024 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007025 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007026
John Hawkes9c1cfda2005-09-06 15:18:14 -07007027 sd = &per_cpu(node_domains, j);
7028 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007029 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007030 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007031 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007032 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007033 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007034 prev = sg;
7035
7036 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007037 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007038 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007039 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007040
Mike Travis7c16ec52008-04-04 18:11:11 -07007041 cpus_complement(*notcovered, *covered);
7042 cpus_and(*tmpmask, *notcovered, *cpu_map);
7043 cpus_and(*tmpmask, *tmpmask, *domainspan);
7044 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007045 break;
7046
Mike Travis7c16ec52008-04-04 18:11:11 -07007047 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7048 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007049 continue;
7050
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007051 sg = kmalloc_node(sizeof(struct sched_group),
7052 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007053 if (!sg) {
7054 printk(KERN_WARNING
7055 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007056 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007057 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007058 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007059 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007060 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007061 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007062 prev->next = sg;
7063 prev = sg;
7064 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066#endif
7067
7068 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007069#ifdef CONFIG_SCHED_SMT
7070 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007071 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7072
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007073 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007074 }
7075#endif
7076#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007077 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007078 struct sched_domain *sd = &per_cpu(core_domains, i);
7079
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007080 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007081 }
7082#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007084 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007085 struct sched_domain *sd = &per_cpu(phys_domains, i);
7086
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007087 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 }
7089
John Hawkes9c1cfda2005-09-06 15:18:14 -07007090#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007091 for (i = 0; i < MAX_NUMNODES; i++)
7092 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007093
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007094 if (sd_allnodes) {
7095 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007096
Mike Travis7c16ec52008-04-04 18:11:11 -07007097 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7098 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007099 init_numa_sched_groups_power(sg);
7100 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007101#endif
7102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007104 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 struct sched_domain *sd;
7106#ifdef CONFIG_SCHED_SMT
7107 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007108#elif defined(CONFIG_SCHED_MC)
7109 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110#else
7111 sd = &per_cpu(phys_domains, i);
7112#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007113 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007115
Mike Travis7c16ec52008-04-04 18:11:11 -07007116 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007117 return 0;
7118
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007119#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007120error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007121 free_sched_groups(cpu_map, tmpmask);
7122 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007123 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007124#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125}
Paul Jackson029190c2007-10-18 23:40:20 -07007126
7127static cpumask_t *doms_cur; /* current sched domains */
7128static int ndoms_cur; /* number of sched domains in 'doms_cur' */
7129
7130/*
7131 * Special case: If a kmalloc of a doms_cur partition (array of
7132 * cpumask_t) fails, then fallback to a single sched domain,
7133 * as determined by the single cpumask_t fallback_doms.
7134 */
7135static cpumask_t fallback_doms;
7136
Heiko Carstens22e52b02008-03-12 18:31:59 +01007137void __attribute__((weak)) arch_update_cpu_topology(void)
7138{
7139}
7140
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007141/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007142 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007143 * For now this just excludes isolated cpus, but could be used to
7144 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007145 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007146static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007147{
Milton Miller73785472007-10-24 18:23:48 +02007148 int err;
7149
Heiko Carstens22e52b02008-03-12 18:31:59 +01007150 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007151 ndoms_cur = 1;
7152 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7153 if (!doms_cur)
7154 doms_cur = &fallback_doms;
7155 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Milton Miller73785472007-10-24 18:23:48 +02007156 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007157 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007158
7159 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007160}
7161
Mike Travis7c16ec52008-04-04 18:11:11 -07007162static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7163 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164{
Mike Travis7c16ec52008-04-04 18:11:11 -07007165 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007166}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007168/*
7169 * Detach sched domains from a group of cpus specified in cpu_map
7170 * These cpus will now be attached to the NULL domain
7171 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007172static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007173{
Mike Travis7c16ec52008-04-04 18:11:11 -07007174 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007175 int i;
7176
Milton Miller6382bc92007-10-15 17:00:19 +02007177 unregister_sched_domain_sysctl();
7178
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007179 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007180 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007181 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007182 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007183}
7184
Paul Jackson029190c2007-10-18 23:40:20 -07007185/*
7186 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007187 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007188 * doms_new[] to the current sched domain partitioning, doms_cur[].
7189 * It destroys each deleted domain and builds each new domain.
7190 *
7191 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007192 * The masks don't intersect (don't overlap.) We should setup one
7193 * sched domain for each mask. CPUs not in any of the cpumasks will
7194 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007195 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7196 * it as it is.
7197 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007198 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7199 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007200 * failed the kmalloc call, then it can pass in doms_new == NULL,
7201 * and partition_sched_domains() will fallback to the single partition
7202 * 'fallback_doms'.
7203 *
7204 * Call with hotplug lock held
7205 */
7206void partition_sched_domains(int ndoms_new, cpumask_t *doms_new)
7207{
7208 int i, j;
7209
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007210 lock_doms_cur();
7211
Milton Miller73785472007-10-24 18:23:48 +02007212 /* always unregister in case we don't destroy any domains */
7213 unregister_sched_domain_sysctl();
7214
Paul Jackson029190c2007-10-18 23:40:20 -07007215 if (doms_new == NULL) {
7216 ndoms_new = 1;
7217 doms_new = &fallback_doms;
7218 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
7219 }
7220
7221 /* Destroy deleted domains */
7222 for (i = 0; i < ndoms_cur; i++) {
7223 for (j = 0; j < ndoms_new; j++) {
7224 if (cpus_equal(doms_cur[i], doms_new[j]))
7225 goto match1;
7226 }
7227 /* no match - a current sched domain not in new doms_new[] */
7228 detach_destroy_domains(doms_cur + i);
7229match1:
7230 ;
7231 }
7232
7233 /* Build new domains */
7234 for (i = 0; i < ndoms_new; i++) {
7235 for (j = 0; j < ndoms_cur; j++) {
7236 if (cpus_equal(doms_new[i], doms_cur[j]))
7237 goto match2;
7238 }
7239 /* no match - add a new doms_new */
7240 build_sched_domains(doms_new + i);
7241match2:
7242 ;
7243 }
7244
7245 /* Remember the new sched domains */
7246 if (doms_cur != &fallback_doms)
7247 kfree(doms_cur);
7248 doms_cur = doms_new;
7249 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007250
7251 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007252
7253 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007254}
7255
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007256#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007257int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007258{
7259 int err;
7260
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007261 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007262 detach_destroy_domains(&cpu_online_map);
7263 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007264 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007265
7266 return err;
7267}
7268
7269static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7270{
7271 int ret;
7272
7273 if (buf[0] != '0' && buf[0] != '1')
7274 return -EINVAL;
7275
7276 if (smt)
7277 sched_smt_power_savings = (buf[0] == '1');
7278 else
7279 sched_mc_power_savings = (buf[0] == '1');
7280
7281 ret = arch_reinit_sched_domains();
7282
7283 return ret ? ret : count;
7284}
7285
Adrian Bunk6707de002007-08-12 18:08:19 +02007286#ifdef CONFIG_SCHED_MC
7287static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7288{
7289 return sprintf(page, "%u\n", sched_mc_power_savings);
7290}
7291static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7292 const char *buf, size_t count)
7293{
7294 return sched_power_savings_store(buf, count, 0);
7295}
7296static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7297 sched_mc_power_savings_store);
7298#endif
7299
7300#ifdef CONFIG_SCHED_SMT
7301static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7302{
7303 return sprintf(page, "%u\n", sched_smt_power_savings);
7304}
7305static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7306 const char *buf, size_t count)
7307{
7308 return sched_power_savings_store(buf, count, 1);
7309}
7310static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7311 sched_smt_power_savings_store);
7312#endif
7313
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007314int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7315{
7316 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007317
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007318#ifdef CONFIG_SCHED_SMT
7319 if (smt_capable())
7320 err = sysfs_create_file(&cls->kset.kobj,
7321 &attr_sched_smt_power_savings.attr);
7322#endif
7323#ifdef CONFIG_SCHED_MC
7324 if (!err && mc_capable())
7325 err = sysfs_create_file(&cls->kset.kobj,
7326 &attr_sched_mc_power_savings.attr);
7327#endif
7328 return err;
7329}
7330#endif
7331
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007333 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007335 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 * which will prevent rebalancing while the sched domains are recalculated.
7337 */
7338static int update_sched_domains(struct notifier_block *nfb,
7339 unsigned long action, void *hcpu)
7340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 switch (action) {
7342 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007343 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007345 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007346 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 return NOTIFY_OK;
7348
7349 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007350 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007352 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007354 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007356 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357 /*
7358 * Fall through and re-initialise the domains.
7359 */
7360 break;
7361 default:
7362 return NOTIFY_DONE;
7363 }
7364
7365 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007366 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367
7368 return NOTIFY_OK;
7369}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370
7371void __init sched_init_smp(void)
7372{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007373 cpumask_t non_isolated_cpus;
7374
Mike Travis434d53b2008-04-04 18:11:04 -07007375#if defined(CONFIG_NUMA)
7376 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7377 GFP_KERNEL);
7378 BUG_ON(sched_group_nodes_bycpu == NULL);
7379#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007380 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007381 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007382 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007383 if (cpus_empty(non_isolated_cpus))
7384 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007385 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386 /* XXX: Theoretical race here - CPU may be hotplugged now */
7387 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007388
7389 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07007390 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007391 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007392 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393}
7394#else
7395void __init sched_init_smp(void)
7396{
Mike Travis434d53b2008-04-04 18:11:04 -07007397#if defined(CONFIG_NUMA)
7398 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7399 GFP_KERNEL);
7400 BUG_ON(sched_group_nodes_bycpu == NULL);
7401#endif
Ingo Molnar19978ca2007-11-09 22:39:38 +01007402 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403}
7404#endif /* CONFIG_SMP */
7405
7406int in_sched_functions(unsigned long addr)
7407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 return in_lock_functions(addr) ||
7409 (addr >= (unsigned long)__sched_text_start
7410 && addr < (unsigned long)__sched_text_end);
7411}
7412
Alexey Dobriyana9957442007-10-15 17:00:13 +02007413static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007414{
7415 cfs_rq->tasks_timeline = RB_ROOT;
Ingo Molnardd41f592007-07-09 18:51:59 +02007416#ifdef CONFIG_FAIR_GROUP_SCHED
7417 cfs_rq->rq = rq;
7418#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007419 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007420}
7421
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007422static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7423{
7424 struct rt_prio_array *array;
7425 int i;
7426
7427 array = &rt_rq->active;
7428 for (i = 0; i < MAX_RT_PRIO; i++) {
7429 INIT_LIST_HEAD(array->queue + i);
7430 __clear_bit(i, array->bitmap);
7431 }
7432 /* delimiter for bitsearch: */
7433 __set_bit(MAX_RT_PRIO, array->bitmap);
7434
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007435#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007436 rt_rq->highest_prio = MAX_RT_PRIO;
7437#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007438#ifdef CONFIG_SMP
7439 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007440 rt_rq->overloaded = 0;
7441#endif
7442
7443 rt_rq->rt_time = 0;
7444 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007445 rt_rq->rt_runtime = 0;
7446 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007447
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007448#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007449 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007450 rt_rq->rq = rq;
7451#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007452}
7453
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007454#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007455static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7456 struct sched_entity *se, int cpu, int add,
7457 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007458{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007459 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007460 tg->cfs_rq[cpu] = cfs_rq;
7461 init_cfs_rq(cfs_rq, rq);
7462 cfs_rq->tg = tg;
7463 if (add)
7464 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7465
7466 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007467 /* se could be NULL for init_task_group */
7468 if (!se)
7469 return;
7470
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007471 if (!parent)
7472 se->cfs_rq = &rq->cfs;
7473 else
7474 se->cfs_rq = parent->my_q;
7475
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007476 se->my_q = cfs_rq;
7477 se->load.weight = tg->shares;
7478 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007479 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007480}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007481#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007482
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007483#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007484static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
7485 struct sched_rt_entity *rt_se, int cpu, int add,
7486 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007487{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007488 struct rq *rq = cpu_rq(cpu);
7489
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007490 tg->rt_rq[cpu] = rt_rq;
7491 init_rt_rq(rt_rq, rq);
7492 rt_rq->tg = tg;
7493 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007494 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007495 if (add)
7496 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7497
7498 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007499 if (!rt_se)
7500 return;
7501
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007502 if (!parent)
7503 rt_se->rt_rq = &rq->rt;
7504 else
7505 rt_se->rt_rq = parent->my_q;
7506
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007507 rt_se->rt_rq = &rq->rt;
7508 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007509 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007510 INIT_LIST_HEAD(&rt_se->run_list);
7511}
7512#endif
7513
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514void __init sched_init(void)
7515{
Ingo Molnardd41f592007-07-09 18:51:59 +02007516 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007517 unsigned long alloc_size = 0, ptr;
7518
7519#ifdef CONFIG_FAIR_GROUP_SCHED
7520 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7521#endif
7522#ifdef CONFIG_RT_GROUP_SCHED
7523 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7524#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007525#ifdef CONFIG_USER_SCHED
7526 alloc_size *= 2;
7527#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007528 /*
7529 * As sched_init() is called before page_alloc is setup,
7530 * we use alloc_bootmem().
7531 */
7532 if (alloc_size) {
7533 ptr = (unsigned long)alloc_bootmem_low(alloc_size);
7534
7535#ifdef CONFIG_FAIR_GROUP_SCHED
7536 init_task_group.se = (struct sched_entity **)ptr;
7537 ptr += nr_cpu_ids * sizeof(void **);
7538
7539 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
7540 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007541
7542#ifdef CONFIG_USER_SCHED
7543 root_task_group.se = (struct sched_entity **)ptr;
7544 ptr += nr_cpu_ids * sizeof(void **);
7545
7546 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
7547 ptr += nr_cpu_ids * sizeof(void **);
7548#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007549#endif
7550#ifdef CONFIG_RT_GROUP_SCHED
7551 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
7552 ptr += nr_cpu_ids * sizeof(void **);
7553
7554 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007555 ptr += nr_cpu_ids * sizeof(void **);
7556
7557#ifdef CONFIG_USER_SCHED
7558 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
7559 ptr += nr_cpu_ids * sizeof(void **);
7560
7561 root_task_group.rt_rq = (struct rt_rq **)ptr;
7562 ptr += nr_cpu_ids * sizeof(void **);
7563#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007564#endif
7565 }
Ingo Molnardd41f592007-07-09 18:51:59 +02007566
Gregory Haskins57d885f2008-01-25 21:08:18 +01007567#ifdef CONFIG_SMP
7568 init_defrootdomain();
7569#endif
7570
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007571 init_rt_bandwidth(&def_rt_bandwidth,
7572 global_rt_period(), global_rt_runtime());
7573
7574#ifdef CONFIG_RT_GROUP_SCHED
7575 init_rt_bandwidth(&init_task_group.rt_bandwidth,
7576 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007577#ifdef CONFIG_USER_SCHED
7578 init_rt_bandwidth(&root_task_group.rt_bandwidth,
7579 global_rt_period(), RUNTIME_INF);
7580#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007581#endif
7582
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007583#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007584 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007585 INIT_LIST_HEAD(&init_task_group.children);
7586
7587#ifdef CONFIG_USER_SCHED
7588 INIT_LIST_HEAD(&root_task_group.children);
7589 init_task_group.parent = &root_task_group;
7590 list_add(&init_task_group.siblings, &root_task_group.children);
7591#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007592#endif
7593
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007594 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007595 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596
7597 rq = cpu_rq(i);
7598 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07007599 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07007600 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007601 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02007602 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02007603 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007604 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007605#ifdef CONFIG_FAIR_GROUP_SCHED
7606 init_task_group.shares = init_task_group_load;
7607 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007608#ifdef CONFIG_CGROUP_SCHED
7609 /*
7610 * How much cpu bandwidth does init_task_group get?
7611 *
7612 * In case of task-groups formed thr' the cgroup filesystem, it
7613 * gets 100% of the cpu resources in the system. This overall
7614 * system cpu resource is divided among the tasks of
7615 * init_task_group and its child task-groups in a fair manner,
7616 * based on each entity's (task or task-group's) weight
7617 * (se->load.weight).
7618 *
7619 * In other words, if init_task_group has 10 tasks of weight
7620 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7621 * then A0's share of the cpu resource is:
7622 *
7623 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
7624 *
7625 * We achieve this by letting init_task_group's tasks sit
7626 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
7627 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007628 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007629#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007630 root_task_group.shares = NICE_0_LOAD;
7631 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007632 /*
7633 * In case of task-groups formed thr' the user id of tasks,
7634 * init_task_group represents tasks belonging to root user.
7635 * Hence it forms a sibling of all subsequent groups formed.
7636 * In this case, init_task_group gets only a fraction of overall
7637 * system cpu resource, based on the weight assigned to root
7638 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
7639 * by letting tasks of init_task_group sit in a separate cfs_rq
7640 * (init_cfs_rq) and having one entity represent this group of
7641 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
7642 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007643 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007644 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007645 &per_cpu(init_sched_entity, i), i, 1,
7646 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007647
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007648#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02007649#endif /* CONFIG_FAIR_GROUP_SCHED */
7650
7651 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007652#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007653 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007654#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007655 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007656#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007657 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007658 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007659 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007660 &per_cpu(init_sched_rt_entity, i), i, 1,
7661 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007662#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007663#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664
Ingo Molnardd41f592007-07-09 18:51:59 +02007665 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7666 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007668 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007669 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007671 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007673 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 rq->migration_thread = NULL;
7675 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007676 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007678 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680 }
7681
Peter Williams2dd73a42006-06-27 02:54:34 -07007682 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007683
Avi Kivitye107be32007-07-26 13:40:43 +02007684#ifdef CONFIG_PREEMPT_NOTIFIERS
7685 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7686#endif
7687
Christoph Lameterc9819f42006-12-10 02:20:25 -08007688#ifdef CONFIG_SMP
7689 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
7690#endif
7691
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007692#ifdef CONFIG_RT_MUTEXES
7693 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
7694#endif
7695
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696 /*
7697 * The boot idle thread does lazy MMU switching as well:
7698 */
7699 atomic_inc(&init_mm.mm_count);
7700 enter_lazy_tlb(&init_mm, current);
7701
7702 /*
7703 * Make us the idle thread. Technically, schedule() should not be
7704 * called from this thread, however somewhere below it might be,
7705 * but because we are the idle thread, we just pick up running again
7706 * when this runqueue becomes "idle".
7707 */
7708 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02007709 /*
7710 * During early bootup we pretend to be a normal task:
7711 */
7712 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01007713
7714 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715}
7716
7717#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
7718void __might_sleep(char *file, int line)
7719{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007720#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007721 static unsigned long prev_jiffy; /* ratelimiting */
7722
7723 if ((in_atomic() || irqs_disabled()) &&
7724 system_state == SYSTEM_RUNNING && !oops_in_progress) {
7725 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7726 return;
7727 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08007728 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729 " context at %s:%d\n", file, line);
7730 printk("in_atomic():%d, irqs_disabled():%d\n",
7731 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08007732 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08007733 if (irqs_disabled())
7734 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007735 dump_stack();
7736 }
7737#endif
7738}
7739EXPORT_SYMBOL(__might_sleep);
7740#endif
7741
7742#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007743static void normalize_task(struct rq *rq, struct task_struct *p)
7744{
7745 int on_rq;
7746 update_rq_clock(rq);
7747 on_rq = p->se.on_rq;
7748 if (on_rq)
7749 deactivate_task(rq, p, 0);
7750 __setscheduler(rq, p, SCHED_NORMAL, 0);
7751 if (on_rq) {
7752 activate_task(rq, p, 0);
7753 resched_task(rq->curr);
7754 }
7755}
7756
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757void normalize_rt_tasks(void)
7758{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007759 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007761 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007763 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007764 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007765 /*
7766 * Only normalize user tasks:
7767 */
7768 if (!p->mm)
7769 continue;
7770
Ingo Molnardd41f592007-07-09 18:51:59 +02007771 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007772#ifdef CONFIG_SCHEDSTATS
7773 p->se.wait_start = 0;
7774 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007775 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007776#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007777 task_rq(p)->clock = 0;
7778
7779 if (!rt_task(p)) {
7780 /*
7781 * Renice negative nice level userspace
7782 * tasks back to 0:
7783 */
7784 if (TASK_NICE(p) < 0 && p->mm)
7785 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007789 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007790 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791
Ingo Molnar178be792007-10-15 17:00:18 +02007792 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007793
Ingo Molnarb29739f2006-06-27 02:54:51 -07007794 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007795 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007796 } while_each_thread(g, p);
7797
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007798 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799}
7800
7801#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007802
7803#ifdef CONFIG_IA64
7804/*
7805 * These functions are only useful for the IA64 MCA handling.
7806 *
7807 * They can only be called when the whole system has been
7808 * stopped - every CPU needs to be quiescent, and no scheduling
7809 * activity can take place. Using them for anything else would
7810 * be a serious bug, and as a result, they aren't even visible
7811 * under any other configuration.
7812 */
7813
7814/**
7815 * curr_task - return the current task for a given cpu.
7816 * @cpu: the processor in question.
7817 *
7818 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7819 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007820struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007821{
7822 return cpu_curr(cpu);
7823}
7824
7825/**
7826 * set_curr_task - set the current task for a given cpu.
7827 * @cpu: the processor in question.
7828 * @p: the task pointer to set.
7829 *
7830 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007831 * are serviced on a separate stack. It allows the architecture to switch the
7832 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007833 * must be called with all CPU's synchronized, and interrupts disabled, the
7834 * and caller must save the original value of the current task (see
7835 * curr_task() above) and restore that value before reenabling interrupts and
7836 * re-starting the system.
7837 *
7838 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7839 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007840void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007841{
7842 cpu_curr(cpu) = p;
7843}
7844
7845#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007846
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007847#ifdef CONFIG_FAIR_GROUP_SCHED
7848static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007849{
7850 int i;
7851
7852 for_each_possible_cpu(i) {
7853 if (tg->cfs_rq)
7854 kfree(tg->cfs_rq[i]);
7855 if (tg->se)
7856 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007857 }
7858
7859 kfree(tg->cfs_rq);
7860 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007861}
7862
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007863static
7864int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007865{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007866 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007867 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007868 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007869 int i;
7870
Mike Travis434d53b2008-04-04 18:11:04 -07007871 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007872 if (!tg->cfs_rq)
7873 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07007874 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007875 if (!tg->se)
7876 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007877
7878 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007879
7880 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007881 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007882
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007883 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
7884 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007885 if (!cfs_rq)
7886 goto err;
7887
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007888 se = kmalloc_node(sizeof(struct sched_entity),
7889 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007890 if (!se)
7891 goto err;
7892
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007893 parent_se = parent ? parent->se[i] : NULL;
7894 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007895 }
7896
7897 return 1;
7898
7899 err:
7900 return 0;
7901}
7902
7903static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7904{
7905 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
7906 &cpu_rq(cpu)->leaf_cfs_rq_list);
7907}
7908
7909static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7910{
7911 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
7912}
7913#else
7914static inline void free_fair_sched_group(struct task_group *tg)
7915{
7916}
7917
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007918static inline
7919int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007920{
7921 return 1;
7922}
7923
7924static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7925{
7926}
7927
7928static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7929{
7930}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007931#endif
7932
7933#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007934static void free_rt_sched_group(struct task_group *tg)
7935{
7936 int i;
7937
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007938 destroy_rt_bandwidth(&tg->rt_bandwidth);
7939
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007940 for_each_possible_cpu(i) {
7941 if (tg->rt_rq)
7942 kfree(tg->rt_rq[i]);
7943 if (tg->rt_se)
7944 kfree(tg->rt_se[i]);
7945 }
7946
7947 kfree(tg->rt_rq);
7948 kfree(tg->rt_se);
7949}
7950
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007951static
7952int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007953{
7954 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007955 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007956 struct rq *rq;
7957 int i;
7958
Mike Travis434d53b2008-04-04 18:11:04 -07007959 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007960 if (!tg->rt_rq)
7961 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07007962 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007963 if (!tg->rt_se)
7964 goto err;
7965
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007966 init_rt_bandwidth(&tg->rt_bandwidth,
7967 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007968
7969 for_each_possible_cpu(i) {
7970 rq = cpu_rq(i);
7971
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007972 rt_rq = kmalloc_node(sizeof(struct rt_rq),
7973 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7974 if (!rt_rq)
7975 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007976
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007977 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
7978 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7979 if (!rt_se)
7980 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007981
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007982 parent_se = parent ? parent->rt_se[i] : NULL;
7983 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007984 }
7985
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007986 return 1;
7987
7988 err:
7989 return 0;
7990}
7991
7992static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7993{
7994 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
7995 &cpu_rq(cpu)->leaf_rt_rq_list);
7996}
7997
7998static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7999{
8000 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8001}
8002#else
8003static inline void free_rt_sched_group(struct task_group *tg)
8004{
8005}
8006
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008007static inline
8008int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008009{
8010 return 1;
8011}
8012
8013static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8014{
8015}
8016
8017static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8018{
8019}
8020#endif
8021
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008022#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008023static void free_sched_group(struct task_group *tg)
8024{
8025 free_fair_sched_group(tg);
8026 free_rt_sched_group(tg);
8027 kfree(tg);
8028}
8029
8030/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008031struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008032{
8033 struct task_group *tg;
8034 unsigned long flags;
8035 int i;
8036
8037 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8038 if (!tg)
8039 return ERR_PTR(-ENOMEM);
8040
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008041 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008042 goto err;
8043
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008044 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008045 goto err;
8046
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008047 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008048 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008049 register_fair_sched_group(tg, i);
8050 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008051 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008052 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008053
8054 WARN_ON(!parent); /* root should already exist */
8055
8056 tg->parent = parent;
8057 list_add_rcu(&tg->siblings, &parent->children);
8058 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008059 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008060
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008061 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008062
8063err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008064 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008065 return ERR_PTR(-ENOMEM);
8066}
8067
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008068/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008069static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008070{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008071 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008072 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008073}
8074
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008075/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008076void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008077{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008078 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008079 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008080
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008081 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008082 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008083 unregister_fair_sched_group(tg, i);
8084 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008085 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008086 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008087 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008088 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008089
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008090 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008091 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008092}
8093
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008094/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008095 * The caller of this function should have put the task in its new group
8096 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8097 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008098 */
8099void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008100{
8101 int on_rq, running;
8102 unsigned long flags;
8103 struct rq *rq;
8104
8105 rq = task_rq_lock(tsk, &flags);
8106
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008107 update_rq_clock(rq);
8108
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008109 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008110 on_rq = tsk->se.on_rq;
8111
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008112 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008113 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008114 if (unlikely(running))
8115 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008116
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008117 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008118
Peter Zijlstra810b3812008-02-29 15:21:01 -05008119#ifdef CONFIG_FAIR_GROUP_SCHED
8120 if (tsk->sched_class->moved_group)
8121 tsk->sched_class->moved_group(tsk);
8122#endif
8123
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008124 if (unlikely(running))
8125 tsk->sched_class->set_curr_task(rq);
8126 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008127 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008128
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008129 task_rq_unlock(rq, &flags);
8130}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008131#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008132
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008133#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008134static void set_se_shares(struct sched_entity *se, unsigned long shares)
8135{
8136 struct cfs_rq *cfs_rq = se->cfs_rq;
8137 struct rq *rq = cfs_rq->rq;
8138 int on_rq;
8139
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008140 spin_lock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008141
8142 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008143 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008144 dequeue_entity(cfs_rq, se, 0);
8145
8146 se->load.weight = shares;
8147 se->load.inv_weight = div64_64((1ULL<<32), shares);
8148
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008149 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008150 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008151
8152 spin_unlock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008153}
8154
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008155static DEFINE_MUTEX(shares_mutex);
8156
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008157int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008158{
8159 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008160 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008161
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008162 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008163 * We can't change the weight of the root cgroup.
8164 */
8165 if (!tg->se[0])
8166 return -EINVAL;
8167
8168 /*
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008169 * A weight of 0 or 1 can cause arithmetics problems.
8170 * (The default weight is 1024 - so there's no practical
8171 * limitation from this.)
8172 */
8173 if (shares < 2)
8174 shares = 2;
8175
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008176 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008177 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008178 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008179
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008180 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008181 for_each_possible_cpu(i)
8182 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008183 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008184 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008185
8186 /* wait for any ongoing reference to this group to finish */
8187 synchronize_sched();
8188
8189 /*
8190 * Now we are free to modify the group's share on each cpu
8191 * w/o tripping rebalance_share or load_balance_fair.
8192 */
8193 tg->shares = shares;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008194 for_each_possible_cpu(i)
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008195 set_se_shares(tg->se[i], shares);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008196
8197 /*
8198 * Enable load balance activity on this group, by inserting it back on
8199 * each cpu's rq->leaf_cfs_rq_list.
8200 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008201 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008202 for_each_possible_cpu(i)
8203 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008204 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008205 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008206done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008207 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008208 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008209}
8210
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008211unsigned long sched_group_shares(struct task_group *tg)
8212{
8213 return tg->shares;
8214}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008215#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008216
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008217#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008218/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008219 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008220 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008221static DEFINE_MUTEX(rt_constraints_mutex);
8222
8223static unsigned long to_ratio(u64 period, u64 runtime)
8224{
8225 if (runtime == RUNTIME_INF)
8226 return 1ULL << 16;
8227
Peter Zijlstra2692a242008-02-27 12:00:46 +01008228 return div64_64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008229}
8230
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008231#ifdef CONFIG_CGROUP_SCHED
8232static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8233{
8234 struct task_group *tgi, *parent = tg->parent;
8235 unsigned long total = 0;
8236
8237 if (!parent) {
8238 if (global_rt_period() < period)
8239 return 0;
8240
8241 return to_ratio(period, runtime) <
8242 to_ratio(global_rt_period(), global_rt_runtime());
8243 }
8244
8245 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8246 return 0;
8247
8248 rcu_read_lock();
8249 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8250 if (tgi == tg)
8251 continue;
8252
8253 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8254 tgi->rt_bandwidth.rt_runtime);
8255 }
8256 rcu_read_unlock();
8257
8258 return total + to_ratio(period, runtime) <
8259 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8260 parent->rt_bandwidth.rt_runtime);
8261}
8262#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008263static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008264{
8265 struct task_group *tgi;
8266 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008267 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008268 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008269
8270 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008271 list_for_each_entry_rcu(tgi, &task_groups, list) {
8272 if (tgi == tg)
8273 continue;
8274
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008275 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8276 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008277 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008278 rcu_read_unlock();
8279
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008280 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008281}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008282#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008283
Dhaval Giani521f1a242008-02-28 15:21:56 +05308284/* Must be called with tasklist_lock held */
8285static inline int tg_has_rt_tasks(struct task_group *tg)
8286{
8287 struct task_struct *g, *p;
8288 do_each_thread(g, p) {
8289 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8290 return 1;
8291 } while_each_thread(g, p);
8292 return 0;
8293}
8294
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008295static int tg_set_bandwidth(struct task_group *tg,
8296 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008297{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008298 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008299
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008300 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308301 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008302 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308303 err = -EBUSY;
8304 goto unlock;
8305 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008306 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8307 err = -EINVAL;
8308 goto unlock;
8309 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008310
8311 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008312 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8313 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008314
8315 for_each_possible_cpu(i) {
8316 struct rt_rq *rt_rq = tg->rt_rq[i];
8317
8318 spin_lock(&rt_rq->rt_runtime_lock);
8319 rt_rq->rt_runtime = rt_runtime;
8320 spin_unlock(&rt_rq->rt_runtime_lock);
8321 }
8322 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008323 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308324 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008325 mutex_unlock(&rt_constraints_mutex);
8326
8327 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008328}
8329
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008330int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8331{
8332 u64 rt_runtime, rt_period;
8333
8334 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8335 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8336 if (rt_runtime_us < 0)
8337 rt_runtime = RUNTIME_INF;
8338
8339 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8340}
8341
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008342long sched_group_rt_runtime(struct task_group *tg)
8343{
8344 u64 rt_runtime_us;
8345
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008346 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008347 return -1;
8348
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008349 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008350 do_div(rt_runtime_us, NSEC_PER_USEC);
8351 return rt_runtime_us;
8352}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008353
8354int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8355{
8356 u64 rt_runtime, rt_period;
8357
8358 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8359 rt_runtime = tg->rt_bandwidth.rt_runtime;
8360
8361 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8362}
8363
8364long sched_group_rt_period(struct task_group *tg)
8365{
8366 u64 rt_period_us;
8367
8368 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8369 do_div(rt_period_us, NSEC_PER_USEC);
8370 return rt_period_us;
8371}
8372
8373static int sched_rt_global_constraints(void)
8374{
8375 int ret = 0;
8376
8377 mutex_lock(&rt_constraints_mutex);
8378 if (!__rt_schedulable(NULL, 1, 0))
8379 ret = -EINVAL;
8380 mutex_unlock(&rt_constraints_mutex);
8381
8382 return ret;
8383}
8384#else
8385static int sched_rt_global_constraints(void)
8386{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008387 unsigned long flags;
8388 int i;
8389
8390 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
8391 for_each_possible_cpu(i) {
8392 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8393
8394 spin_lock(&rt_rq->rt_runtime_lock);
8395 rt_rq->rt_runtime = global_rt_runtime();
8396 spin_unlock(&rt_rq->rt_runtime_lock);
8397 }
8398 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
8399
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008400 return 0;
8401}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008402#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008403
8404int sched_rt_handler(struct ctl_table *table, int write,
8405 struct file *filp, void __user *buffer, size_t *lenp,
8406 loff_t *ppos)
8407{
8408 int ret;
8409 int old_period, old_runtime;
8410 static DEFINE_MUTEX(mutex);
8411
8412 mutex_lock(&mutex);
8413 old_period = sysctl_sched_rt_period;
8414 old_runtime = sysctl_sched_rt_runtime;
8415
8416 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8417
8418 if (!ret && write) {
8419 ret = sched_rt_global_constraints();
8420 if (ret) {
8421 sysctl_sched_rt_period = old_period;
8422 sysctl_sched_rt_runtime = old_runtime;
8423 } else {
8424 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8425 def_rt_bandwidth.rt_period =
8426 ns_to_ktime(global_rt_period());
8427 }
8428 }
8429 mutex_unlock(&mutex);
8430
8431 return ret;
8432}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008433
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008434#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008435
8436/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008437static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008438{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008439 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8440 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008441}
8442
8443static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008444cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008445{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008446 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008447
Paul Menage2b01dfe2007-10-24 18:23:50 +02008448 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008449 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008450 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008451 return &init_task_group.css;
8452 }
8453
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008454 parent = cgroup_tg(cgrp->parent);
8455 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008456 if (IS_ERR(tg))
8457 return ERR_PTR(-ENOMEM);
8458
8459 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008460 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008461
8462 return &tg->css;
8463}
8464
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008465static void
8466cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008467{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008468 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008469
8470 sched_destroy_group(tg);
8471}
8472
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008473static int
8474cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8475 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008476{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008477#ifdef CONFIG_RT_GROUP_SCHED
8478 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008479 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008480 return -EINVAL;
8481#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008482 /* We don't support RT-tasks being in separate groups */
8483 if (tsk->sched_class != &fair_sched_class)
8484 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008485#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008486
8487 return 0;
8488}
8489
8490static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008491cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008492 struct cgroup *old_cont, struct task_struct *tsk)
8493{
8494 sched_move_task(tsk);
8495}
8496
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008497#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menage2b01dfe2007-10-24 18:23:50 +02008498static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8499 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008500{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008501 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008502}
8503
Paul Menage2b01dfe2007-10-24 18:23:50 +02008504static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008505{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008506 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008507
8508 return (u64) tg->shares;
8509}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008510#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008511
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008512#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008513static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008514 struct file *file,
8515 const char __user *userbuf,
8516 size_t nbytes, loff_t *unused_ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008517{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008518 char buffer[64];
8519 int retval = 0;
8520 s64 val;
8521 char *end;
8522
8523 if (!nbytes)
8524 return -EINVAL;
8525 if (nbytes >= sizeof(buffer))
8526 return -E2BIG;
8527 if (copy_from_user(buffer, userbuf, nbytes))
8528 return -EFAULT;
8529
8530 buffer[nbytes] = 0; /* nul-terminate */
8531
8532 /* strip newline if necessary */
8533 if (nbytes && (buffer[nbytes-1] == '\n'))
8534 buffer[nbytes-1] = 0;
8535 val = simple_strtoll(buffer, &end, 0);
8536 if (*end)
8537 return -EINVAL;
8538
8539 /* Pass to subsystem */
8540 retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
8541 if (!retval)
8542 retval = nbytes;
8543 return retval;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008544}
8545
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008546static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
8547 struct file *file,
8548 char __user *buf, size_t nbytes,
8549 loff_t *ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008550{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008551 char tmp[64];
8552 long val = sched_group_rt_runtime(cgroup_tg(cgrp));
8553 int len = sprintf(tmp, "%ld\n", val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008554
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008555 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008556}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008557
8558static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8559 u64 rt_period_us)
8560{
8561 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
8562}
8563
8564static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
8565{
8566 return sched_group_rt_period(cgroup_tg(cgrp));
8567}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008568#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008569
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008570static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008571#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008572 {
8573 .name = "shares",
8574 .read_uint = cpu_shares_read_uint,
8575 .write_uint = cpu_shares_write_uint,
8576 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008577#endif
8578#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008579 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008580 .name = "rt_runtime_us",
8581 .read = cpu_rt_runtime_read,
8582 .write = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008583 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008584 {
8585 .name = "rt_period_us",
8586 .read_uint = cpu_rt_period_read_uint,
8587 .write_uint = cpu_rt_period_write_uint,
8588 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008589#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008590};
8591
8592static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8593{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008594 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008595}
8596
8597struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008598 .name = "cpu",
8599 .create = cpu_cgroup_create,
8600 .destroy = cpu_cgroup_destroy,
8601 .can_attach = cpu_cgroup_can_attach,
8602 .attach = cpu_cgroup_attach,
8603 .populate = cpu_cgroup_populate,
8604 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008605 .early_init = 1,
8606};
8607
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008608#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008609
8610#ifdef CONFIG_CGROUP_CPUACCT
8611
8612/*
8613 * CPU accounting code for task groups.
8614 *
8615 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8616 * (balbir@in.ibm.com).
8617 */
8618
8619/* track cpu usage of a group of tasks */
8620struct cpuacct {
8621 struct cgroup_subsys_state css;
8622 /* cpuusage holds pointer to a u64-type object on every cpu */
8623 u64 *cpuusage;
8624};
8625
8626struct cgroup_subsys cpuacct_subsys;
8627
8628/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308629static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008630{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308631 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008632 struct cpuacct, css);
8633}
8634
8635/* return cpu accounting group to which this task belongs */
8636static inline struct cpuacct *task_ca(struct task_struct *tsk)
8637{
8638 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8639 struct cpuacct, css);
8640}
8641
8642/* create a new cpu accounting group */
8643static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05308644 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008645{
8646 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
8647
8648 if (!ca)
8649 return ERR_PTR(-ENOMEM);
8650
8651 ca->cpuusage = alloc_percpu(u64);
8652 if (!ca->cpuusage) {
8653 kfree(ca);
8654 return ERR_PTR(-ENOMEM);
8655 }
8656
8657 return &ca->css;
8658}
8659
8660/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008661static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05308662cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008663{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308664 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008665
8666 free_percpu(ca->cpuusage);
8667 kfree(ca);
8668}
8669
8670/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308671static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008672{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308673 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008674 u64 totalcpuusage = 0;
8675 int i;
8676
8677 for_each_possible_cpu(i) {
8678 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
8679
8680 /*
8681 * Take rq->lock to make 64-bit addition safe on 32-bit
8682 * platforms.
8683 */
8684 spin_lock_irq(&cpu_rq(i)->lock);
8685 totalcpuusage += *cpuusage;
8686 spin_unlock_irq(&cpu_rq(i)->lock);
8687 }
8688
8689 return totalcpuusage;
8690}
8691
Dhaval Giani0297b802008-02-29 10:02:44 +05308692static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
8693 u64 reset)
8694{
8695 struct cpuacct *ca = cgroup_ca(cgrp);
8696 int err = 0;
8697 int i;
8698
8699 if (reset) {
8700 err = -EINVAL;
8701 goto out;
8702 }
8703
8704 for_each_possible_cpu(i) {
8705 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
8706
8707 spin_lock_irq(&cpu_rq(i)->lock);
8708 *cpuusage = 0;
8709 spin_unlock_irq(&cpu_rq(i)->lock);
8710 }
8711out:
8712 return err;
8713}
8714
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008715static struct cftype files[] = {
8716 {
8717 .name = "usage",
8718 .read_uint = cpuusage_read,
Dhaval Giani0297b802008-02-29 10:02:44 +05308719 .write_uint = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008720 },
8721};
8722
Dhaval Giani32cd7562008-02-29 10:02:43 +05308723static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008724{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308725 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008726}
8727
8728/*
8729 * charge this task's execution time to its accounting group.
8730 *
8731 * called with rq->lock held.
8732 */
8733static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
8734{
8735 struct cpuacct *ca;
8736
8737 if (!cpuacct_subsys.active)
8738 return;
8739
8740 ca = task_ca(tsk);
8741 if (ca) {
8742 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
8743
8744 *cpuusage += cputime;
8745 }
8746}
8747
8748struct cgroup_subsys cpuacct_subsys = {
8749 .name = "cpuacct",
8750 .create = cpuacct_create,
8751 .destroy = cpuacct_destroy,
8752 .populate = cpuacct_populate,
8753 .subsys_id = cpuacct_subsys_id,
8754};
8755#endif /* CONFIG_CGROUP_CPUACCT */