blob: 9457106b18af066182da7476c86125dbad191ddc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/sched.c
3 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
35#include <linux/smp_lock.h>
36#include <asm/mmu_context.h>
37#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/completion.h>
40#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070041#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
57#include <linux/kthread.h>
58#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020059#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/syscalls.h>
61#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070062#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080063#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070064#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070065#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020066#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020067#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010068#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070069#include <linux/tick.h>
Mike Travis434d53b2008-04-04 18:11:04 -070070#include <linux/bootmem.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020071#include <linux/debugfs.h>
72#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Eric Dumazet5517d862007-05-08 00:32:57 -070074#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020075#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080078 * Scheduler clock - returns current time in nanosec units.
79 * This is default implementation.
80 * Architectures and sub-architectures can override this.
81 */
82unsigned long long __attribute__((weak)) sched_clock(void)
83{
Eric Dumazetd6322fa2007-11-09 22:39:38 +010084 return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080085}
86
87/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * Convert user-nice values [ -20 ... 0 ... 19 ]
89 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
90 * and back.
91 */
92#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
93#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
94#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
95
96/*
97 * 'User priority' is the nice value converted to something we
98 * can work with better when scaling various scheduler parameters,
99 * it's a [ 0 ... 39 ] range.
100 */
101#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
102#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
103#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
104
105/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100106 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100108#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200110#define NICE_0_LOAD SCHED_LOAD_SCALE
111#define NICE_0_SHIFT SCHED_LOAD_SHIFT
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * These are the 'tuning knobs' of the scheduler:
115 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200116 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * Timeslices get refilled after they expire.
118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700120
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200121/*
122 * single value that denotes runtime == period, ie unlimited time.
123 */
124#define RUNTIME_INF ((u64)~0ULL)
125
Eric Dumazet5517d862007-05-08 00:32:57 -0700126#ifdef CONFIG_SMP
127/*
128 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
129 * Since cpu_power is a 'constant', we can use a reciprocal divide.
130 */
131static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
132{
133 return reciprocal_divide(load, sg->reciprocal_cpu_power);
134}
135
136/*
137 * Each time a sched group cpu_power is changed,
138 * we must compute its reciprocal value
139 */
140static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
141{
142 sg->__cpu_power += val;
143 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
144}
145#endif
146
Ingo Molnare05606d2007-07-09 18:51:59 +0200147static inline int rt_policy(int policy)
148{
149 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
150 return 1;
151 return 0;
152}
153
154static inline int task_has_rt_policy(struct task_struct *p)
155{
156 return rt_policy(p->policy);
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200160 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200162struct rt_prio_array {
163 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
164 struct list_head queue[MAX_RT_PRIO];
165};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200167struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100168 /* nests inside the rq lock: */
169 spinlock_t rt_runtime_lock;
170 ktime_t rt_period;
171 u64 rt_runtime;
172 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200173};
174
175static struct rt_bandwidth def_rt_bandwidth;
176
177static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
178
179static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
180{
181 struct rt_bandwidth *rt_b =
182 container_of(timer, struct rt_bandwidth, rt_period_timer);
183 ktime_t now;
184 int overrun;
185 int idle = 0;
186
187 for (;;) {
188 now = hrtimer_cb_get_time(timer);
189 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
190
191 if (!overrun)
192 break;
193
194 idle = do_sched_rt_period_timer(rt_b, overrun);
195 }
196
197 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
198}
199
200static
201void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
202{
203 rt_b->rt_period = ns_to_ktime(period);
204 rt_b->rt_runtime = runtime;
205
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200206 spin_lock_init(&rt_b->rt_runtime_lock);
207
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200208 hrtimer_init(&rt_b->rt_period_timer,
209 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
210 rt_b->rt_period_timer.function = sched_rt_period_timer;
211 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
212}
213
214static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
215{
216 ktime_t now;
217
218 if (rt_b->rt_runtime == RUNTIME_INF)
219 return;
220
221 if (hrtimer_active(&rt_b->rt_period_timer))
222 return;
223
224 spin_lock(&rt_b->rt_runtime_lock);
225 for (;;) {
226 if (hrtimer_active(&rt_b->rt_period_timer))
227 break;
228
229 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
230 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
231 hrtimer_start(&rt_b->rt_period_timer,
232 rt_b->rt_period_timer.expires,
233 HRTIMER_MODE_ABS);
234 }
235 spin_unlock(&rt_b->rt_runtime_lock);
236}
237
238#ifdef CONFIG_RT_GROUP_SCHED
239static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
240{
241 hrtimer_cancel(&rt_b->rt_period_timer);
242}
243#endif
244
Heiko Carstens712555e2008-04-28 11:33:07 +0200245/*
246 * sched_domains_mutex serializes calls to arch_init_sched_domains,
247 * detach_destroy_domains and partition_sched_domains.
248 */
249static DEFINE_MUTEX(sched_domains_mutex);
250
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100251#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200252
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700253#include <linux/cgroup.h>
254
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200255struct cfs_rq;
256
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100257static LIST_HEAD(task_groups);
258
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200259/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200260struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100261#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700262 struct cgroup_subsys_state css;
263#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100264
265#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200266 /* schedulable entities of this group on each cpu */
267 struct sched_entity **se;
268 /* runqueue "owned" by this group on each cpu */
269 struct cfs_rq **cfs_rq;
270 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100271#endif
272
273#ifdef CONFIG_RT_GROUP_SCHED
274 struct sched_rt_entity **rt_se;
275 struct rt_rq **rt_rq;
276
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200277 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100278#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100279
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100280 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100281 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200282
283 struct task_group *parent;
284 struct list_head siblings;
285 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200286};
287
Dhaval Giani354d60c2008-04-19 19:44:59 +0200288#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200289
290/*
291 * Root task group.
292 * Every UID task group (including init_task_group aka UID-0) will
293 * be a child to this group.
294 */
295struct task_group root_task_group;
296
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100297#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200298/* Default task group's sched entity on each cpu */
299static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
300/* Default task group's cfs_rq on each cpu */
301static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100302#endif
303
304#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100305static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
306static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100307#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200308#else
309#define root_task_group init_task_group
Dhaval Giani354d60c2008-04-19 19:44:59 +0200310#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100311
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100312/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100313 * a task group's cpu shares.
314 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100315static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100316
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100317#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100318#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100319# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200320#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100321# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200322#endif
323
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800324/*
325 * A weight of 0, 1 or ULONG_MAX can cause arithmetics problems.
326 * (The default weight is 1024 - so there's no practical
327 * limitation from this.)
328 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200329#define MIN_SHARES 2
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800330#define MAX_SHARES (ULONG_MAX - 1)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200331
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100332static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100333#endif
334
335/* Default task group.
336 * Every task in system belong to this group at bootup.
337 */
Mike Travis434d53b2008-04-04 18:11:04 -0700338struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200339
340/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200341static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200342{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200343 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200344
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100345#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200346 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100347#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700348 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
349 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200350#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100351 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200352#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200353 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200354}
355
356/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100357static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200358{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100359#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100360 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
361 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100362#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100363
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100364#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100365 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
366 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100367#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200368}
369
370#else
371
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100372static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200373
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100374#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200375
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200376/* CFS-related fields in a runqueue */
377struct cfs_rq {
378 struct load_weight load;
379 unsigned long nr_running;
380
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200381 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200382 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200383
384 struct rb_root tasks_timeline;
385 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200386
387 struct list_head tasks;
388 struct list_head *balance_iterator;
389
390 /*
391 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200392 * It is set to NULL otherwise (i.e when none are currently running).
393 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100394 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200395
396 unsigned long nr_spread_over;
397
Ingo Molnar62160e3f2007-10-15 17:00:03 +0200398#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200399 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
400
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100401 /*
402 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200403 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
404 * (like users, containers etc.)
405 *
406 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
407 * list is used during load balance.
408 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100409 struct list_head leaf_cfs_rq_list;
410 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200411
412#ifdef CONFIG_SMP
413 unsigned long task_weight;
414 unsigned long shares;
415 /*
416 * We need space to build a sched_domain wide view of the full task
417 * group tree, in order to avoid depending on dynamic memory allocation
418 * during the load balancing we place this in the per cpu task group
419 * hierarchy. This limits the load balancing to one instance per cpu,
420 * but more should not be needed anyway.
421 */
422 struct aggregate_struct {
423 /*
424 * load = weight(cpus) * f(tg)
425 *
426 * Where f(tg) is the recursive weight fraction assigned to
427 * this group.
428 */
429 unsigned long load;
430
431 /*
432 * part of the group weight distributed to this span.
433 */
434 unsigned long shares;
435
436 /*
437 * The sum of all runqueue weights within this span.
438 */
439 unsigned long rq_weight;
440
441 /*
442 * Weight contributed by tasks; this is the part we can
443 * influence by moving tasks around.
444 */
445 unsigned long task_weight;
446 } aggregate;
447#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200448#endif
449};
450
451/* Real-Time classes' related field in a runqueue: */
452struct rt_rq {
453 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100454 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100455#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100456 int highest_prio; /* highest queued rt task prio */
457#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100458#ifdef CONFIG_SMP
Gregory Haskins73fe6aae2008-01-25 21:08:07 +0100459 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc12008-01-25 21:08:12 +0100460 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100461#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100462 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100463 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200464 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100465 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200466 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100467
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100468#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100469 unsigned long rt_nr_boosted;
470
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100471 struct rq *rq;
472 struct list_head leaf_rt_rq_list;
473 struct task_group *tg;
474 struct sched_rt_entity *rt_se;
475#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200476};
477
Gregory Haskins57d885f2008-01-25 21:08:18 +0100478#ifdef CONFIG_SMP
479
480/*
481 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100482 * variables. Each exclusive cpuset essentially defines an island domain by
483 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100484 * exclusive cpuset is created, we also create and attach a new root-domain
485 * object.
486 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100487 */
488struct root_domain {
489 atomic_t refcount;
490 cpumask_t span;
491 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100492
Ingo Molnar0eab9142008-01-25 21:08:19 +0100493 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100494 * The "RT overload" flag: it gets set if a CPU has more than
495 * one runnable RT task.
496 */
497 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100498 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100499};
500
Gregory Haskinsdc938522008-01-25 21:08:26 +0100501/*
502 * By default the system creates a single root-domain with all cpus as
503 * members (mimicking the global state we have today).
504 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100505static struct root_domain def_root_domain;
506
507#endif
508
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200509/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * This is the main, per-CPU runqueue data structure.
511 *
512 * Locking rule: those places that want to lock multiple runqueues
513 * (such as the load balancing or the thread migration code), lock
514 * acquire operations must be ordered by ascending &runqueue.
515 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700516struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200517 /* runqueue lock: */
518 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /*
521 * nr_running and cpu_load should be in the same cacheline because
522 * remote CPUs use both these fields when doing load calculation.
523 */
524 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200525 #define CPU_LOAD_IDX_MAX 5
526 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700527 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700528#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200529 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700530 unsigned char in_nohz_recently;
531#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200532 /* capture load from *all* tasks on this cpu: */
533 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200534 unsigned long nr_load_updates;
535 u64 nr_switches;
536
537 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100538 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100539
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200540#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200541 /* list of leaf cfs_rq on this cpu: */
542 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100543#endif
544#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100545 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /*
549 * This is part of a global counter where only the total sum
550 * over all CPUs matters. A task can increase this counter on
551 * one CPU and if it got migrated afterwards it may decrease
552 * it on another CPU. Always updated under the runqueue lock:
553 */
554 unsigned long nr_uninterruptible;
555
Ingo Molnar36c8b582006-07-03 00:25:41 -0700556 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800557 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200559
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200560 u64 clock, prev_clock_raw;
561 s64 clock_max_delta;
562
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100563 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200564 u64 idle_clock;
565 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200566 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 atomic_t nr_iowait;
569
570#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100571 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct sched_domain *sd;
573
574 /* For active balancing */
575 int active_balance;
576 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200577 /* cpu of this runqueue: */
578 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Ingo Molnar36c8b582006-07-03 00:25:41 -0700580 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 struct list_head migration_queue;
582#endif
583
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100584#ifdef CONFIG_SCHED_HRTICK
585 unsigned long hrtick_flags;
586 ktime_t hrtick_expire;
587 struct hrtimer hrtick_timer;
588#endif
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590#ifdef CONFIG_SCHEDSTATS
591 /* latency stats */
592 struct sched_info rq_sched_info;
593
594 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200595 unsigned int yld_exp_empty;
596 unsigned int yld_act_empty;
597 unsigned int yld_both_empty;
598 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200601 unsigned int sched_switch;
602 unsigned int sched_count;
603 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200606 unsigned int ttwu_count;
607 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200608
609 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200610 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700612 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613};
614
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700615static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Ingo Molnardd41f592007-07-09 18:51:59 +0200617static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
618{
619 rq->curr->sched_class->check_preempt_curr(rq, p);
620}
621
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700622static inline int cpu_of(struct rq *rq)
623{
624#ifdef CONFIG_SMP
625 return rq->cpu;
626#else
627 return 0;
628#endif
629}
630
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200631#ifdef CONFIG_NO_HZ
632static inline bool nohz_on(int cpu)
633{
634 return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE;
635}
636
637static inline u64 max_skipped_ticks(struct rq *rq)
638{
639 return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1;
640}
641
642static inline void update_last_tick_seen(struct rq *rq)
643{
644 rq->last_tick_seen = jiffies;
645}
646#else
647static inline u64 max_skipped_ticks(struct rq *rq)
648{
649 return 1;
650}
651
652static inline void update_last_tick_seen(struct rq *rq)
653{
654}
655#endif
656
Nick Piggin674311d2005-06-25 14:57:27 -0700657/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200658 * Update the per-runqueue clock, as finegrained as the platform can give
659 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200660 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200661static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200662{
663 u64 prev_raw = rq->prev_clock_raw;
664 u64 now = sched_clock();
665 s64 delta = now - prev_raw;
666 u64 clock = rq->clock;
667
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200668#ifdef CONFIG_SCHED_DEBUG
669 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
670#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200671 /*
672 * Protect against sched_clock() occasionally going backwards:
673 */
674 if (unlikely(delta < 0)) {
675 clock++;
676 rq->clock_warps++;
677 } else {
678 /*
679 * Catch too large forward jumps too:
680 */
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200681 u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC;
682 u64 max_time = rq->tick_timestamp + max_jump;
683
684 if (unlikely(clock + delta > max_time)) {
685 if (clock < max_time)
686 clock = max_time;
Ingo Molnar529c7722007-08-10 23:05:11 +0200687 else
688 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200689 rq->clock_overflows++;
690 } else {
691 if (unlikely(delta > rq->clock_max_delta))
692 rq->clock_max_delta = delta;
693 clock += delta;
694 }
695 }
696
697 rq->prev_clock_raw = now;
698 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200699}
700
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200701static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200702{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200703 if (likely(smp_processor_id() == cpu_of(rq)))
704 __update_rq_clock(rq);
705}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200706
Ingo Molnar20d315d2007-07-09 18:51:58 +0200707/*
Nick Piggin674311d2005-06-25 14:57:27 -0700708 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700709 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700710 *
711 * The domain tree of any CPU may only be accessed from within
712 * preempt-disabled sections.
713 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700714#define for_each_domain(cpu, __sd) \
715 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
718#define this_rq() (&__get_cpu_var(runqueues))
719#define task_rq(p) cpu_rq(task_cpu(p))
720#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
721
Ingo Molnare436d802007-07-19 21:28:35 +0200722/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200723 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
724 */
725#ifdef CONFIG_SCHED_DEBUG
726# define const_debug __read_mostly
727#else
728# define const_debug static const
729#endif
730
731/*
732 * Debugging: various feature bits
733 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200734
735#define SCHED_FEAT(name, enabled) \
736 __SCHED_FEAT_##name ,
737
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200738enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200739#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200740};
741
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200742#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200743
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200744#define SCHED_FEAT(name, enabled) \
745 (1UL << __SCHED_FEAT_##name) * enabled |
746
747const_debug unsigned int sysctl_sched_features =
748#include "sched_features.h"
749 0;
750
751#undef SCHED_FEAT
752
753#ifdef CONFIG_SCHED_DEBUG
754#define SCHED_FEAT(name, enabled) \
755 #name ,
756
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700757static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200758#include "sched_features.h"
759 NULL
760};
761
762#undef SCHED_FEAT
763
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700764static int sched_feat_open(struct inode *inode, struct file *filp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200765{
766 filp->private_data = inode->i_private;
767 return 0;
768}
769
770static ssize_t
771sched_feat_read(struct file *filp, char __user *ubuf,
772 size_t cnt, loff_t *ppos)
773{
774 char *buf;
775 int r = 0;
776 int len = 0;
777 int i;
778
779 for (i = 0; sched_feat_names[i]; i++) {
780 len += strlen(sched_feat_names[i]);
781 len += 4;
782 }
783
784 buf = kmalloc(len + 2, GFP_KERNEL);
785 if (!buf)
786 return -ENOMEM;
787
788 for (i = 0; sched_feat_names[i]; i++) {
789 if (sysctl_sched_features & (1UL << i))
790 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
791 else
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200792 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200793 }
794
795 r += sprintf(buf + r, "\n");
796 WARN_ON(r >= len + 2);
797
798 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
799
800 kfree(buf);
801
802 return r;
803}
804
805static ssize_t
806sched_feat_write(struct file *filp, const char __user *ubuf,
807 size_t cnt, loff_t *ppos)
808{
809 char buf[64];
810 char *cmp = buf;
811 int neg = 0;
812 int i;
813
814 if (cnt > 63)
815 cnt = 63;
816
817 if (copy_from_user(&buf, ubuf, cnt))
818 return -EFAULT;
819
820 buf[cnt] = 0;
821
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200822 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200823 neg = 1;
824 cmp += 3;
825 }
826
827 for (i = 0; sched_feat_names[i]; i++) {
828 int len = strlen(sched_feat_names[i]);
829
830 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
831 if (neg)
832 sysctl_sched_features &= ~(1UL << i);
833 else
834 sysctl_sched_features |= (1UL << i);
835 break;
836 }
837 }
838
839 if (!sched_feat_names[i])
840 return -EINVAL;
841
842 filp->f_pos += cnt;
843
844 return cnt;
845}
846
847static struct file_operations sched_feat_fops = {
848 .open = sched_feat_open,
849 .read = sched_feat_read,
850 .write = sched_feat_write,
851};
852
853static __init int sched_init_debug(void)
854{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200855 debugfs_create_file("sched_features", 0644, NULL, NULL,
856 &sched_feat_fops);
857
858 return 0;
859}
860late_initcall(sched_init_debug);
861
862#endif
863
864#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200865
866/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100867 * Number of tasks to iterate in a single balance run.
868 * Limited because this is done with IRQs disabled.
869 */
870const_debug unsigned int sysctl_sched_nr_migrate = 32;
871
872/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100873 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100874 * default: 1s
875 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100876unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100877
Ingo Molnar6892b752008-02-13 14:02:36 +0100878static __read_mostly int scheduler_running;
879
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100880/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100881 * part of the period that we allow rt tasks to run in us.
882 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100883 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100884int sysctl_sched_rt_runtime = 950000;
885
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200886static inline u64 global_rt_period(void)
887{
888 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
889}
890
891static inline u64 global_rt_runtime(void)
892{
893 if (sysctl_sched_rt_period < 0)
894 return RUNTIME_INF;
895
896 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
897}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100898
Ingo Molnar690229a2008-04-23 09:31:35 +0200899unsigned long long time_sync_thresh = 100000;
Ingo Molnar27ec4402008-02-28 21:00:21 +0100900
901static DEFINE_PER_CPU(unsigned long long, time_offset);
902static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
903
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100904/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100905 * Global lock which we take every now and then to synchronize
906 * the CPUs time. This method is not warp-safe, but it's good
907 * enough to synchronize slowly diverging time sources and thus
908 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200909 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100910static DEFINE_SPINLOCK(time_sync_lock);
911static unsigned long long prev_global_time;
912
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200913static unsigned long long __sync_cpu_clock(unsigned long long time, int cpu)
Ingo Molnar27ec4402008-02-28 21:00:21 +0100914{
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200915 /*
916 * We want this inlined, to not get tracer function calls
917 * in this critical section:
918 */
919 spin_acquire(&time_sync_lock.dep_map, 0, 0, _THIS_IP_);
920 __raw_spin_lock(&time_sync_lock.raw_lock);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100921
922 if (time < prev_global_time) {
923 per_cpu(time_offset, cpu) += prev_global_time - time;
924 time = prev_global_time;
925 } else {
926 prev_global_time = time;
927 }
928
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200929 __raw_spin_unlock(&time_sync_lock.raw_lock);
930 spin_release(&time_sync_lock.dep_map, 1, _THIS_IP_);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100931
932 return time;
933}
934
935static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200936{
Ingo Molnare436d802007-07-19 21:28:35 +0200937 unsigned long long now;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200938 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200939
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100940 /*
941 * Only call sched_clock() if the scheduler has already been
942 * initialized (some code might call cpu_clock() very early):
943 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100944 if (unlikely(!scheduler_running))
945 return 0;
946
Ingo Molnar6892b752008-02-13 14:02:36 +0100947 rq = cpu_rq(cpu);
948 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200949 now = rq->clock;
Ingo Molnare436d802007-07-19 21:28:35 +0200950
951 return now;
952}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100953
954/*
955 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
956 * clock constructed from sched_clock():
957 */
958unsigned long long cpu_clock(int cpu)
959{
960 unsigned long long prev_cpu_time, time, delta_time;
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200961 unsigned long flags;
Ingo Molnar27ec4402008-02-28 21:00:21 +0100962
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200963 local_irq_save(flags);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100964 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
965 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
966 delta_time = time-prev_cpu_time;
967
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200968 if (unlikely(delta_time > time_sync_thresh)) {
Ingo Molnar27ec4402008-02-28 21:00:21 +0100969 time = __sync_cpu_clock(time, cpu);
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200970 per_cpu(prev_cpu_time, cpu) = time;
971 }
972 local_irq_restore(flags);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100973
974 return time;
975}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200976EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700979# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700981#ifndef finish_arch_switch
982# define finish_arch_switch(prev) do { } while (0)
983#endif
984
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100985static inline int task_current(struct rq *rq, struct task_struct *p)
986{
987 return rq->curr == p;
988}
989
Nick Piggin4866cde2005-06-25 14:57:23 -0700990#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700991static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700992{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100993 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700994}
995
Ingo Molnar70b97a72006-07-03 00:25:42 -0700996static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700997{
998}
999
Ingo Molnar70b97a72006-07-03 00:25:42 -07001000static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -07001001{
Ingo Molnarda04c032005-09-13 11:17:59 +02001002#ifdef CONFIG_DEBUG_SPINLOCK
1003 /* this is a valid case when another task releases the spinlock */
1004 rq->lock.owner = current;
1005#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07001006 /*
1007 * If we are tracking spinlock dependencies then we have to
1008 * fix up the runqueue lock - which gets 'carried over' from
1009 * prev into current:
1010 */
1011 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1012
Nick Piggin4866cde2005-06-25 14:57:23 -07001013 spin_unlock_irq(&rq->lock);
1014}
1015
1016#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001017static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -07001018{
1019#ifdef CONFIG_SMP
1020 return p->oncpu;
1021#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01001022 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -07001023#endif
1024}
1025
Ingo Molnar70b97a72006-07-03 00:25:42 -07001026static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07001027{
1028#ifdef CONFIG_SMP
1029 /*
1030 * We can optimise this out completely for !SMP, because the
1031 * SMP rebalancing from interrupt is the only thing that cares
1032 * here.
1033 */
1034 next->oncpu = 1;
1035#endif
1036#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1037 spin_unlock_irq(&rq->lock);
1038#else
1039 spin_unlock(&rq->lock);
1040#endif
1041}
1042
Ingo Molnar70b97a72006-07-03 00:25:42 -07001043static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -07001044{
1045#ifdef CONFIG_SMP
1046 /*
1047 * After ->oncpu is cleared, the task can be moved to a different CPU.
1048 * We must ensure this doesn't happen until the switch is completely
1049 * finished.
1050 */
1051 smp_wmb();
1052 prev->oncpu = 0;
1053#endif
1054#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1055 local_irq_enable();
1056#endif
1057}
1058#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001061 * __task_rq_lock - lock the runqueue a given task resides on.
1062 * Must be called interrupts disabled.
1063 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001064static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001065 __acquires(rq->lock)
1066{
Andi Kleen3a5c3592007-10-15 17:00:14 +02001067 for (;;) {
1068 struct rq *rq = task_rq(p);
1069 spin_lock(&rq->lock);
1070 if (likely(rq == task_rq(p)))
1071 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -07001072 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001073 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07001074}
1075
1076/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001078 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 * explicitly disabling preemption.
1080 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001081static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 __acquires(rq->lock)
1083{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001084 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Andi Kleen3a5c3592007-10-15 17:00:14 +02001086 for (;;) {
1087 local_irq_save(*flags);
1088 rq = task_rq(p);
1089 spin_lock(&rq->lock);
1090 if (likely(rq == task_rq(p)))
1091 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
Alexey Dobriyana9957442007-10-15 17:00:13 +02001096static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001097 __releases(rq->lock)
1098{
1099 spin_unlock(&rq->lock);
1100}
1101
Ingo Molnar70b97a72006-07-03 00:25:42 -07001102static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 __releases(rq->lock)
1104{
1105 spin_unlock_irqrestore(&rq->lock, *flags);
1106}
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001109 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001111static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 __acquires(rq->lock)
1113{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001114 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 local_irq_disable();
1117 rq = this_rq();
1118 spin_lock(&rq->lock);
1119
1120 return rq;
1121}
1122
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001123/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001124 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001125 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001126void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001127{
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001128 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001129
Andrew Mortond478c2c2008-04-26 11:30:34 -07001130 WARN_ON(!irqs_disabled());
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001131 spin_lock(&rq->lock);
1132 __update_rq_clock(rq);
1133 spin_unlock(&rq->lock);
1134 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001135}
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001136EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
1137
1138/*
1139 * We just idled delta nanoseconds (called with irqs disabled):
1140 */
1141void sched_clock_idle_wakeup_event(u64 delta_ns)
1142{
1143 struct rq *rq = cpu_rq(smp_processor_id());
1144 u64 now = sched_clock();
1145
Andrew Mortond478c2c2008-04-26 11:30:34 -07001146 WARN_ON(!irqs_disabled());
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001147 rq->idle_clock += delta_ns;
1148 /*
1149 * Override the previous timestamp and ignore all
1150 * sched_clock() deltas that occured while we idled,
1151 * and use the PM-provided delta_ns to advance the
1152 * rq clock:
1153 */
1154 spin_lock(&rq->lock);
1155 rq->prev_clock_raw = now;
1156 rq->clock += delta_ns;
1157 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +01001158 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001159}
1160EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001161
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001162static void __resched_task(struct task_struct *p, int tif_bit);
1163
1164static inline void resched_task(struct task_struct *p)
1165{
1166 __resched_task(p, TIF_NEED_RESCHED);
1167}
1168
1169#ifdef CONFIG_SCHED_HRTICK
1170/*
1171 * Use HR-timers to deliver accurate preemption points.
1172 *
1173 * Its all a bit involved since we cannot program an hrt while holding the
1174 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1175 * reschedule event.
1176 *
1177 * When we get rescheduled we reprogram the hrtick_timer outside of the
1178 * rq->lock.
1179 */
1180static inline void resched_hrt(struct task_struct *p)
1181{
1182 __resched_task(p, TIF_HRTICK_RESCHED);
1183}
1184
1185static inline void resched_rq(struct rq *rq)
1186{
1187 unsigned long flags;
1188
1189 spin_lock_irqsave(&rq->lock, flags);
1190 resched_task(rq->curr);
1191 spin_unlock_irqrestore(&rq->lock, flags);
1192}
1193
1194enum {
1195 HRTICK_SET, /* re-programm hrtick_timer */
1196 HRTICK_RESET, /* not a new slice */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001197 HRTICK_BLOCK, /* stop hrtick operations */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001198};
1199
1200/*
1201 * Use hrtick when:
1202 * - enabled by features
1203 * - hrtimer is actually high res
1204 */
1205static inline int hrtick_enabled(struct rq *rq)
1206{
1207 if (!sched_feat(HRTICK))
1208 return 0;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001209 if (unlikely(test_bit(HRTICK_BLOCK, &rq->hrtick_flags)))
1210 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001211 return hrtimer_is_hres_active(&rq->hrtick_timer);
1212}
1213
1214/*
1215 * Called to set the hrtick timer state.
1216 *
1217 * called with rq->lock held and irqs disabled
1218 */
1219static void hrtick_start(struct rq *rq, u64 delay, int reset)
1220{
1221 assert_spin_locked(&rq->lock);
1222
1223 /*
1224 * preempt at: now + delay
1225 */
1226 rq->hrtick_expire =
1227 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1228 /*
1229 * indicate we need to program the timer
1230 */
1231 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1232 if (reset)
1233 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1234
1235 /*
1236 * New slices are called from the schedule path and don't need a
1237 * forced reschedule.
1238 */
1239 if (reset)
1240 resched_hrt(rq->curr);
1241}
1242
1243static void hrtick_clear(struct rq *rq)
1244{
1245 if (hrtimer_active(&rq->hrtick_timer))
1246 hrtimer_cancel(&rq->hrtick_timer);
1247}
1248
1249/*
1250 * Update the timer from the possible pending state.
1251 */
1252static void hrtick_set(struct rq *rq)
1253{
1254 ktime_t time;
1255 int set, reset;
1256 unsigned long flags;
1257
1258 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1259
1260 spin_lock_irqsave(&rq->lock, flags);
1261 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1262 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1263 time = rq->hrtick_expire;
1264 clear_thread_flag(TIF_HRTICK_RESCHED);
1265 spin_unlock_irqrestore(&rq->lock, flags);
1266
1267 if (set) {
1268 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1269 if (reset && !hrtimer_active(&rq->hrtick_timer))
1270 resched_rq(rq);
1271 } else
1272 hrtick_clear(rq);
1273}
1274
1275/*
1276 * High-resolution timer tick.
1277 * Runs from hardirq context with interrupts disabled.
1278 */
1279static enum hrtimer_restart hrtick(struct hrtimer *timer)
1280{
1281 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1282
1283 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1284
1285 spin_lock(&rq->lock);
1286 __update_rq_clock(rq);
1287 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1288 spin_unlock(&rq->lock);
1289
1290 return HRTIMER_NORESTART;
1291}
1292
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001293static void hotplug_hrtick_disable(int cpu)
1294{
1295 struct rq *rq = cpu_rq(cpu);
1296 unsigned long flags;
1297
1298 spin_lock_irqsave(&rq->lock, flags);
1299 rq->hrtick_flags = 0;
1300 __set_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1301 spin_unlock_irqrestore(&rq->lock, flags);
1302
1303 hrtick_clear(rq);
1304}
1305
1306static void hotplug_hrtick_enable(int cpu)
1307{
1308 struct rq *rq = cpu_rq(cpu);
1309 unsigned long flags;
1310
1311 spin_lock_irqsave(&rq->lock, flags);
1312 __clear_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1313 spin_unlock_irqrestore(&rq->lock, flags);
1314}
1315
1316static int
1317hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1318{
1319 int cpu = (int)(long)hcpu;
1320
1321 switch (action) {
1322 case CPU_UP_CANCELED:
1323 case CPU_UP_CANCELED_FROZEN:
1324 case CPU_DOWN_PREPARE:
1325 case CPU_DOWN_PREPARE_FROZEN:
1326 case CPU_DEAD:
1327 case CPU_DEAD_FROZEN:
1328 hotplug_hrtick_disable(cpu);
1329 return NOTIFY_OK;
1330
1331 case CPU_UP_PREPARE:
1332 case CPU_UP_PREPARE_FROZEN:
1333 case CPU_DOWN_FAILED:
1334 case CPU_DOWN_FAILED_FROZEN:
1335 case CPU_ONLINE:
1336 case CPU_ONLINE_FROZEN:
1337 hotplug_hrtick_enable(cpu);
1338 return NOTIFY_OK;
1339 }
1340
1341 return NOTIFY_DONE;
1342}
1343
1344static void init_hrtick(void)
1345{
1346 hotcpu_notifier(hotplug_hrtick, 0);
1347}
1348
1349static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001350{
1351 rq->hrtick_flags = 0;
1352 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1353 rq->hrtick_timer.function = hrtick;
1354 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1355}
1356
1357void hrtick_resched(void)
1358{
1359 struct rq *rq;
1360 unsigned long flags;
1361
1362 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1363 return;
1364
1365 local_irq_save(flags);
1366 rq = cpu_rq(smp_processor_id());
1367 hrtick_set(rq);
1368 local_irq_restore(flags);
1369}
1370#else
1371static inline void hrtick_clear(struct rq *rq)
1372{
1373}
1374
1375static inline void hrtick_set(struct rq *rq)
1376{
1377}
1378
1379static inline void init_rq_hrtick(struct rq *rq)
1380{
1381}
1382
1383void hrtick_resched(void)
1384{
1385}
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001386
1387static inline void init_hrtick(void)
1388{
1389}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001390#endif
1391
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001392/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001393 * resched_task - mark a task 'to be rescheduled now'.
1394 *
1395 * On UP this means the setting of the need_resched flag, on SMP it
1396 * might also involve a cross-CPU call to trigger the scheduler on
1397 * the target CPU.
1398 */
1399#ifdef CONFIG_SMP
1400
1401#ifndef tsk_is_polling
1402#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1403#endif
1404
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001405static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001406{
1407 int cpu;
1408
1409 assert_spin_locked(&task_rq(p)->lock);
1410
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001411 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001412 return;
1413
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001414 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001415
1416 cpu = task_cpu(p);
1417 if (cpu == smp_processor_id())
1418 return;
1419
1420 /* NEED_RESCHED must be visible before we test polling */
1421 smp_mb();
1422 if (!tsk_is_polling(p))
1423 smp_send_reschedule(cpu);
1424}
1425
1426static void resched_cpu(int cpu)
1427{
1428 struct rq *rq = cpu_rq(cpu);
1429 unsigned long flags;
1430
1431 if (!spin_trylock_irqsave(&rq->lock, flags))
1432 return;
1433 resched_task(cpu_curr(cpu));
1434 spin_unlock_irqrestore(&rq->lock, flags);
1435}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001436
1437#ifdef CONFIG_NO_HZ
1438/*
1439 * When add_timer_on() enqueues a timer into the timer wheel of an
1440 * idle CPU then this timer might expire before the next timer event
1441 * which is scheduled to wake up that CPU. In case of a completely
1442 * idle system the next event might even be infinite time into the
1443 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1444 * leaves the inner idle loop so the newly added timer is taken into
1445 * account when the CPU goes back to idle and evaluates the timer
1446 * wheel for the next timer event.
1447 */
1448void wake_up_idle_cpu(int cpu)
1449{
1450 struct rq *rq = cpu_rq(cpu);
1451
1452 if (cpu == smp_processor_id())
1453 return;
1454
1455 /*
1456 * This is safe, as this function is called with the timer
1457 * wheel base lock of (cpu) held. When the CPU is on the way
1458 * to idle and has not yet set rq->curr to idle then it will
1459 * be serialized on the timer wheel base lock and take the new
1460 * timer into account automatically.
1461 */
1462 if (rq->curr != rq->idle)
1463 return;
1464
1465 /*
1466 * We can set TIF_RESCHED on the idle task of the other CPU
1467 * lockless. The worst case is that the other CPU runs the
1468 * idle task through an additional NOOP schedule()
1469 */
1470 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1471
1472 /* NEED_RESCHED must be visible before we test polling */
1473 smp_mb();
1474 if (!tsk_is_polling(rq->idle))
1475 smp_send_reschedule(cpu);
1476}
1477#endif
1478
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001479#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001480static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001481{
1482 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001483 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001484}
1485#endif
1486
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001487#if BITS_PER_LONG == 32
1488# define WMULT_CONST (~0UL)
1489#else
1490# define WMULT_CONST (1UL << 32)
1491#endif
1492
1493#define WMULT_SHIFT 32
1494
Ingo Molnar194081e2007-08-09 11:16:51 +02001495/*
1496 * Shift right and round:
1497 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001498#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001499
Peter Zijlstra8f1bc3852008-04-19 19:45:00 +02001500/*
1501 * delta *= weight / lw
1502 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001503static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001504calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1505 struct load_weight *lw)
1506{
1507 u64 tmp;
1508
Peter Zijlstrae05510d2008-05-05 23:56:17 +02001509 if (!lw->inv_weight)
1510 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)/(lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001511
1512 tmp = (u64)delta_exec * weight;
1513 /*
1514 * Check whether we'd overflow the 64-bit multiplication:
1515 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001516 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001517 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001518 WMULT_SHIFT/2);
1519 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001520 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001521
Ingo Molnarecf691d2007-08-02 17:41:40 +02001522 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001523}
1524
Ingo Molnar10919852007-10-15 17:00:04 +02001525static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001526{
1527 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001528 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001529}
1530
Ingo Molnar10919852007-10-15 17:00:04 +02001531static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001532{
1533 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001534 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001538 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1539 * of tasks with abnormal "nice" values across CPUs the contribution that
1540 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001541 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001542 * scaled version of the new time slice allocation that they receive on time
1543 * slice expiry etc.
1544 */
1545
Ingo Molnardd41f592007-07-09 18:51:59 +02001546#define WEIGHT_IDLEPRIO 2
1547#define WMULT_IDLEPRIO (1 << 31)
1548
1549/*
1550 * Nice levels are multiplicative, with a gentle 10% change for every
1551 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1552 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1553 * that remained on nice 0.
1554 *
1555 * The "10% effect" is relative and cumulative: from _any_ nice level,
1556 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee62007-07-16 09:46:30 +02001557 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1558 * If a task goes up by ~10% and another task goes down by ~10% then
1559 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001560 */
1561static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001562 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1563 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1564 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1565 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1566 /* 0 */ 1024, 820, 655, 526, 423,
1567 /* 5 */ 335, 272, 215, 172, 137,
1568 /* 10 */ 110, 87, 70, 56, 45,
1569 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001570};
1571
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001572/*
1573 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1574 *
1575 * In cases where the weight does not change often, we can use the
1576 * precalculated inverse to speed up arithmetics by turning divisions
1577 * into multiplications:
1578 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001579static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001580 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1581 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1582 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1583 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1584 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1585 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1586 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1587 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001588};
Peter Williams2dd73a42006-06-27 02:54:34 -07001589
Ingo Molnardd41f592007-07-09 18:51:59 +02001590static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1591
1592/*
1593 * runqueue iterator, to support SMP load-balancing between different
1594 * scheduling classes, without having to expose their internal data
1595 * structures to the load-balancing proper:
1596 */
1597struct rq_iterator {
1598 void *arg;
1599 struct task_struct *(*start)(void *);
1600 struct task_struct *(*next)(void *);
1601};
1602
Peter Williamse1d14842007-10-24 18:23:51 +02001603#ifdef CONFIG_SMP
1604static unsigned long
1605balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1606 unsigned long max_load_move, struct sched_domain *sd,
1607 enum cpu_idle_type idle, int *all_pinned,
1608 int *this_best_prio, struct rq_iterator *iterator);
1609
1610static int
1611iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1612 struct sched_domain *sd, enum cpu_idle_type idle,
1613 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001614#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001615
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001616#ifdef CONFIG_CGROUP_CPUACCT
1617static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1618#else
1619static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1620#endif
1621
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001622static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1623{
1624 update_load_add(&rq->load, load);
1625}
1626
1627static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1628{
1629 update_load_sub(&rq->load, load);
1630}
1631
Gregory Haskinse7693a32008-01-25 21:08:09 +01001632#ifdef CONFIG_SMP
1633static unsigned long source_load(int cpu, int type);
1634static unsigned long target_load(int cpu, int type);
1635static unsigned long cpu_avg_load_per_task(int cpu);
1636static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001637
1638#ifdef CONFIG_FAIR_GROUP_SCHED
1639
1640/*
1641 * Group load balancing.
1642 *
1643 * We calculate a few balance domain wide aggregate numbers; load and weight.
1644 * Given the pictures below, and assuming each item has equal weight:
1645 *
1646 * root 1 - thread
1647 * / | \ A - group
1648 * A 1 B
1649 * /|\ / \
1650 * C 2 D 3 4
1651 * | |
1652 * 5 6
1653 *
1654 * load:
1655 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1656 * which equals 1/9-th of the total load.
1657 *
1658 * shares:
1659 * The weight of this group on the selected cpus.
1660 *
1661 * rq_weight:
1662 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1663 * B would get 2.
1664 *
1665 * task_weight:
1666 * Part of the rq_weight contributed by tasks; all groups except B would
1667 * get 1, B gets 2.
1668 */
1669
1670static inline struct aggregate_struct *
1671aggregate(struct task_group *tg, struct sched_domain *sd)
1672{
1673 return &tg->cfs_rq[sd->first_cpu]->aggregate;
1674}
1675
1676typedef void (*aggregate_func)(struct task_group *, struct sched_domain *);
1677
1678/*
1679 * Iterate the full tree, calling @down when first entering a node and @up when
1680 * leaving it for the final time.
1681 */
1682static
1683void aggregate_walk_tree(aggregate_func down, aggregate_func up,
1684 struct sched_domain *sd)
1685{
1686 struct task_group *parent, *child;
1687
1688 rcu_read_lock();
1689 parent = &root_task_group;
1690down:
1691 (*down)(parent, sd);
1692 list_for_each_entry_rcu(child, &parent->children, siblings) {
1693 parent = child;
1694 goto down;
1695
1696up:
1697 continue;
1698 }
1699 (*up)(parent, sd);
1700
1701 child = parent;
1702 parent = parent->parent;
1703 if (parent)
1704 goto up;
1705 rcu_read_unlock();
1706}
1707
1708/*
1709 * Calculate the aggregate runqueue weight.
1710 */
1711static
1712void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd)
1713{
1714 unsigned long rq_weight = 0;
1715 unsigned long task_weight = 0;
1716 int i;
1717
1718 for_each_cpu_mask(i, sd->span) {
1719 rq_weight += tg->cfs_rq[i]->load.weight;
1720 task_weight += tg->cfs_rq[i]->task_weight;
1721 }
1722
1723 aggregate(tg, sd)->rq_weight = rq_weight;
1724 aggregate(tg, sd)->task_weight = task_weight;
1725}
1726
1727/*
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001728 * Compute the weight of this group on the given cpus.
1729 */
1730static
1731void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd)
1732{
1733 unsigned long shares = 0;
1734 int i;
1735
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001736 for_each_cpu_mask(i, sd->span)
1737 shares += tg->cfs_rq[i]->shares;
1738
Peter Zijlstra3f5087a2008-04-25 00:25:08 +02001739 if ((!shares && aggregate(tg, sd)->rq_weight) || shares > tg->shares)
1740 shares = tg->shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001741
1742 aggregate(tg, sd)->shares = shares;
1743}
1744
1745/*
1746 * Compute the load fraction assigned to this group, relies on the aggregate
1747 * weight and this group's parent's load, i.e. top-down.
1748 */
1749static
1750void aggregate_group_load(struct task_group *tg, struct sched_domain *sd)
1751{
1752 unsigned long load;
1753
1754 if (!tg->parent) {
1755 int i;
1756
1757 load = 0;
1758 for_each_cpu_mask(i, sd->span)
1759 load += cpu_rq(i)->load.weight;
1760
1761 } else {
1762 load = aggregate(tg->parent, sd)->load;
1763
1764 /*
1765 * shares is our weight in the parent's rq so
1766 * shares/parent->rq_weight gives our fraction of the load
1767 */
1768 load *= aggregate(tg, sd)->shares;
1769 load /= aggregate(tg->parent, sd)->rq_weight + 1;
1770 }
1771
1772 aggregate(tg, sd)->load = load;
1773}
1774
1775static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1776
1777/*
1778 * Calculate and set the cpu's group shares.
1779 */
1780static void
1781__update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,
1782 int tcpu)
1783{
1784 int boost = 0;
1785 unsigned long shares;
1786 unsigned long rq_weight;
1787
1788 if (!tg->se[tcpu])
1789 return;
1790
1791 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1792
1793 /*
1794 * If there are currently no tasks on the cpu pretend there is one of
1795 * average load so that when a new task gets to run here it will not
1796 * get delayed by group starvation.
1797 */
1798 if (!rq_weight) {
1799 boost = 1;
1800 rq_weight = NICE_0_LOAD;
1801 }
1802
1803 /*
1804 * \Sum shares * rq_weight
1805 * shares = -----------------------
1806 * \Sum rq_weight
1807 *
1808 */
1809 shares = aggregate(tg, sd)->shares * rq_weight;
1810 shares /= aggregate(tg, sd)->rq_weight + 1;
1811
1812 /*
1813 * record the actual number of shares, not the boosted amount.
1814 */
1815 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1816
1817 if (shares < MIN_SHARES)
1818 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08001819 else if (shares > MAX_SHARES)
1820 shares = MAX_SHARES;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001821
1822 __set_se_shares(tg->se[tcpu], shares);
1823}
1824
1825/*
1826 * Re-adjust the weights on the cpu the task came from and on the cpu the
1827 * task went to.
1828 */
1829static void
1830__move_group_shares(struct task_group *tg, struct sched_domain *sd,
1831 int scpu, int dcpu)
1832{
1833 unsigned long shares;
1834
1835 shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1836
1837 __update_group_shares_cpu(tg, sd, scpu);
1838 __update_group_shares_cpu(tg, sd, dcpu);
1839
1840 /*
1841 * ensure we never loose shares due to rounding errors in the
1842 * above redistribution.
1843 */
1844 shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1845 if (shares)
1846 tg->cfs_rq[dcpu]->shares += shares;
1847}
1848
1849/*
1850 * Because changing a group's shares changes the weight of the super-group
1851 * we need to walk up the tree and change all shares until we hit the root.
1852 */
1853static void
1854move_group_shares(struct task_group *tg, struct sched_domain *sd,
1855 int scpu, int dcpu)
1856{
1857 while (tg) {
1858 __move_group_shares(tg, sd, scpu, dcpu);
1859 tg = tg->parent;
1860 }
1861}
1862
1863static
1864void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd)
1865{
1866 unsigned long shares = aggregate(tg, sd)->shares;
1867 int i;
1868
1869 for_each_cpu_mask(i, sd->span) {
1870 struct rq *rq = cpu_rq(i);
1871 unsigned long flags;
1872
1873 spin_lock_irqsave(&rq->lock, flags);
1874 __update_group_shares_cpu(tg, sd, i);
1875 spin_unlock_irqrestore(&rq->lock, flags);
1876 }
1877
1878 aggregate_group_shares(tg, sd);
1879
1880 /*
1881 * ensure we never loose shares due to rounding errors in the
1882 * above redistribution.
1883 */
1884 shares -= aggregate(tg, sd)->shares;
1885 if (shares) {
1886 tg->cfs_rq[sd->first_cpu]->shares += shares;
1887 aggregate(tg, sd)->shares += shares;
1888 }
1889}
1890
1891/*
1892 * Calculate the accumulative weight and recursive load of each task group
1893 * while walking down the tree.
1894 */
1895static
1896void aggregate_get_down(struct task_group *tg, struct sched_domain *sd)
1897{
1898 aggregate_group_weight(tg, sd);
1899 aggregate_group_shares(tg, sd);
1900 aggregate_group_load(tg, sd);
1901}
1902
1903/*
1904 * Rebalance the cpu shares while walking back up the tree.
1905 */
1906static
1907void aggregate_get_up(struct task_group *tg, struct sched_domain *sd)
1908{
1909 aggregate_group_set_shares(tg, sd);
1910}
1911
1912static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1913
1914static void __init init_aggregate(void)
1915{
1916 int i;
1917
1918 for_each_possible_cpu(i)
1919 spin_lock_init(&per_cpu(aggregate_lock, i));
1920}
1921
1922static int get_aggregate(struct sched_domain *sd)
1923{
1924 if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu)))
1925 return 0;
1926
1927 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd);
1928 return 1;
1929}
1930
1931static void put_aggregate(struct sched_domain *sd)
1932{
1933 spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu));
1934}
1935
1936static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1937{
1938 cfs_rq->shares = shares;
1939}
1940
1941#else
1942
1943static inline void init_aggregate(void)
1944{
1945}
1946
1947static inline int get_aggregate(struct sched_domain *sd)
1948{
1949 return 0;
1950}
1951
1952static inline void put_aggregate(struct sched_domain *sd)
1953{
1954}
1955#endif
1956
1957#else /* CONFIG_SMP */
1958
1959#ifdef CONFIG_FAIR_GROUP_SCHED
1960static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1961{
1962}
1963#endif
1964
Gregory Haskinse7693a32008-01-25 21:08:09 +01001965#endif /* CONFIG_SMP */
1966
Ingo Molnardd41f592007-07-09 18:51:59 +02001967#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001968#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001969#include "sched_fair.c"
1970#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001971#ifdef CONFIG_SCHED_DEBUG
1972# include "sched_debug.c"
1973#endif
1974
1975#define sched_class_highest (&rt_sched_class)
1976
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001977static void inc_nr_running(struct rq *rq)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001978{
1979 rq->nr_running++;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001980}
1981
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001982static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001983{
1984 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001985}
1986
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001987static void set_load_weight(struct task_struct *p)
1988{
1989 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001990 p->se.load.weight = prio_to_weight[0] * 2;
1991 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1992 return;
1993 }
1994
1995 /*
1996 * SCHED_IDLE tasks get minimal weight:
1997 */
1998 if (p->policy == SCHED_IDLE) {
1999 p->se.load.weight = WEIGHT_IDLEPRIO;
2000 p->se.load.inv_weight = WMULT_IDLEPRIO;
2001 return;
2002 }
2003
2004 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
2005 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02002006}
2007
Ingo Molnar8159f872007-08-09 11:16:49 +02002008static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02002009{
2010 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02002011 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02002012 p->se.on_rq = 1;
2013}
2014
Ingo Molnar69be72c2007-08-09 11:16:49 +02002015static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02002016{
Ingo Molnarf02231e2007-08-09 11:16:48 +02002017 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02002018 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02002019}
2020
2021/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002022 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02002023 */
Ingo Molnar14531182007-07-09 18:51:59 +02002024static inline int __normal_prio(struct task_struct *p)
2025{
Ingo Molnardd41f592007-07-09 18:51:59 +02002026 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02002027}
2028
2029/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07002030 * Calculate the expected normal priority: i.e. priority
2031 * without taking RT-inheritance into account. Might be
2032 * boosted by interactivity modifiers. Changes upon fork,
2033 * setprio syscalls, and whenever the interactivity
2034 * estimator recalculates.
2035 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002036static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07002037{
2038 int prio;
2039
Ingo Molnare05606d2007-07-09 18:51:59 +02002040 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07002041 prio = MAX_RT_PRIO-1 - p->rt_priority;
2042 else
2043 prio = __normal_prio(p);
2044 return prio;
2045}
2046
2047/*
2048 * Calculate the current priority, i.e. the priority
2049 * taken into account by the scheduler. This value might
2050 * be boosted by RT tasks, or might be boosted by
2051 * interactivity modifiers. Will be RT if the task got
2052 * RT-boosted. If not then it returns p->normal_prio.
2053 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002054static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07002055{
2056 p->normal_prio = normal_prio(p);
2057 /*
2058 * If we are RT tasks or we were boosted to RT priority,
2059 * keep the priority unchanged. Otherwise, update priority
2060 * to the normal priority:
2061 */
2062 if (!rt_prio(p->prio))
2063 return p->normal_prio;
2064 return p->prio;
2065}
2066
2067/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002068 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002070static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002072 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02002073 rq->nr_uninterruptible--;
2074
Ingo Molnar8159f872007-08-09 11:16:49 +02002075 enqueue_task(rq, p, wakeup);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002076 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077}
2078
2079/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 * deactivate_task - remove a task from the runqueue.
2081 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002082static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002084 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02002085 rq->nr_uninterruptible++;
2086
Ingo Molnar69be72c2007-08-09 11:16:49 +02002087 dequeue_task(rq, p, sleep);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002088 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091/**
2092 * task_curr - is this task currently executing on a CPU?
2093 * @p: the task in question.
2094 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002095inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
2097 return cpu_curr(task_cpu(p)) == p;
2098}
2099
Peter Williams2dd73a42006-06-27 02:54:34 -07002100/* Used instead of source_load when we know the type == 0 */
2101unsigned long weighted_cpuload(const int cpu)
2102{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002103 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002104}
2105
2106static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
2107{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01002108 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002109#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01002110 /*
2111 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2112 * successfuly executed on another CPU. We must ensure that updates of
2113 * per-task data have been completed by this moment.
2114 */
2115 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02002116 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02002117#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07002118}
2119
Steven Rostedtcb469842008-01-25 21:08:22 +01002120static inline void check_class_changed(struct rq *rq, struct task_struct *p,
2121 const struct sched_class *prev_class,
2122 int oldprio, int running)
2123{
2124 if (prev_class != p->sched_class) {
2125 if (prev_class->switched_from)
2126 prev_class->switched_from(rq, p, running);
2127 p->sched_class->switched_to(rq, p, running);
2128 } else
2129 p->sched_class->prio_changed(rq, p, oldprio, running);
2130}
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02002133
Ingo Molnarcc367732007-10-15 17:00:18 +02002134/*
2135 * Is this task likely cache-hot:
2136 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002137static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002138task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2139{
2140 s64 delta;
2141
Ingo Molnarf540a602008-03-15 17:10:34 +01002142 /*
2143 * Buddy candidates are cache hot:
2144 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01002145 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01002146 return 1;
2147
Ingo Molnarcc367732007-10-15 17:00:18 +02002148 if (p->sched_class != &fair_sched_class)
2149 return 0;
2150
Ingo Molnar6bc16652007-10-15 17:00:18 +02002151 if (sysctl_sched_migration_cost == -1)
2152 return 1;
2153 if (sysctl_sched_migration_cost == 0)
2154 return 0;
2155
Ingo Molnarcc367732007-10-15 17:00:18 +02002156 delta = now - p->se.exec_start;
2157
2158 return delta < (s64)sysctl_sched_migration_cost;
2159}
2160
2161
Ingo Molnardd41f592007-07-09 18:51:59 +02002162void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002163{
Ingo Molnardd41f592007-07-09 18:51:59 +02002164 int old_cpu = task_cpu(p);
2165 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002166 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
2167 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02002168 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002169
2170 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002171
2172#ifdef CONFIG_SCHEDSTATS
2173 if (p->se.wait_start)
2174 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002175 if (p->se.sleep_start)
2176 p->se.sleep_start -= clock_offset;
2177 if (p->se.block_start)
2178 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02002179 if (old_cpu != new_cpu) {
2180 schedstat_inc(p, se.nr_migrations);
2181 if (task_hot(p, old_rq->clock, NULL))
2182 schedstat_inc(p, se.nr_forced2_migrations);
2183 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002184#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002185 p->se.vruntime -= old_cfsrq->min_vruntime -
2186 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02002187
2188 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002189}
2190
Ingo Molnar70b97a72006-07-03 00:25:42 -07002191struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Ingo Molnar36c8b582006-07-03 00:25:41 -07002194 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 int dest_cpu;
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002198};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200/*
2201 * The task's runqueue lock must be held.
2202 * Returns true if you have to wait for migration thread.
2203 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002204static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002205migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002207 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 /*
2210 * If the task is not on a runqueue (and not running), then
2211 * it is sufficient to simply update the task's cpu field.
2212 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002213 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 set_task_cpu(p, dest_cpu);
2215 return 0;
2216 }
2217
2218 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 req->task = p;
2220 req->dest_cpu = dest_cpu;
2221 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return 1;
2224}
2225
2226/*
2227 * wait_task_inactive - wait for a thread to unschedule.
2228 *
2229 * The caller must ensure that the task *will* unschedule sometime soon,
2230 * else this function might spin for a *long* time. This function can't
2231 * be called with interrupts off, or it may introduce deadlock with
2232 * smp_call_function() if an IPI is sent by the same process we are
2233 * waiting to become inactive.
2234 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002235void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
2237 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002238 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002239 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Andi Kleen3a5c3592007-10-15 17:00:14 +02002241 for (;;) {
2242 /*
2243 * We do the initial early heuristics without holding
2244 * any task-queue locks at all. We'll only try to get
2245 * the runqueue lock when things look like they will
2246 * work out!
2247 */
2248 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002249
Andi Kleen3a5c3592007-10-15 17:00:14 +02002250 /*
2251 * If the task is actively running on another CPU
2252 * still, just relax and busy-wait without holding
2253 * any locks.
2254 *
2255 * NOTE! Since we don't hold any locks, it's not
2256 * even sure that "rq" stays as the right runqueue!
2257 * But we don't care, since "task_running()" will
2258 * return false if the runqueue has changed and p
2259 * is actually now running somewhere else!
2260 */
2261 while (task_running(rq, p))
2262 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002263
Andi Kleen3a5c3592007-10-15 17:00:14 +02002264 /*
2265 * Ok, time to look more closely! We need the rq
2266 * lock now, to be *sure*. If we're wrong, we'll
2267 * just go back and repeat.
2268 */
2269 rq = task_rq_lock(p, &flags);
2270 running = task_running(rq, p);
2271 on_rq = p->se.on_rq;
2272 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002273
Andi Kleen3a5c3592007-10-15 17:00:14 +02002274 /*
2275 * Was it really running after all now that we
2276 * checked with the proper locks actually held?
2277 *
2278 * Oops. Go back and try again..
2279 */
2280 if (unlikely(running)) {
2281 cpu_relax();
2282 continue;
2283 }
2284
2285 /*
2286 * It's not enough that it's not actively running,
2287 * it must be off the runqueue _entirely_, and not
2288 * preempted!
2289 *
2290 * So if it wa still runnable (but just not actively
2291 * running right now), it's preempted, and we should
2292 * yield - it could be a while.
2293 */
2294 if (unlikely(on_rq)) {
2295 schedule_timeout_uninterruptible(1);
2296 continue;
2297 }
2298
2299 /*
2300 * Ahh, all good. It wasn't running, and it wasn't
2301 * runnable, which means that it will never become
2302 * running in the future either. We're all done!
2303 */
2304 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
2308/***
2309 * kick_process - kick a running thread to enter/exit the kernel
2310 * @p: the to-be-kicked thread
2311 *
2312 * Cause a process which is running on another CPU to enter
2313 * kernel-mode, without any delay. (to get signals handled.)
2314 *
2315 * NOTE: this function doesnt have to take the runqueue lock,
2316 * because all it wants to ensure is that the remote task enters
2317 * the kernel. If the IPI races and the task has been migrated
2318 * to another CPU then no harm is done and the purpose has been
2319 * achieved as well.
2320 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002321void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
2323 int cpu;
2324
2325 preempt_disable();
2326 cpu = task_cpu(p);
2327 if ((cpu != smp_processor_id()) && task_curr(p))
2328 smp_send_reschedule(cpu);
2329 preempt_enable();
2330}
2331
2332/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002333 * Return a low guess at the load of a migration-source cpu weighted
2334 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 *
2336 * We want to under-estimate the load of migration sources, to
2337 * balance conservatively.
2338 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002339static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002340{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002341 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002342 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002343
Peter Williams2dd73a42006-06-27 02:54:34 -07002344 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002345 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002346
Ingo Molnardd41f592007-07-09 18:51:59 +02002347 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
2350/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002351 * Return a high guess at the load of a migration-target cpu weighted
2352 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002354static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002355{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002356 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002357 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002358
Peter Williams2dd73a42006-06-27 02:54:34 -07002359 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002360 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002361
Ingo Molnardd41f592007-07-09 18:51:59 +02002362 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002363}
2364
2365/*
2366 * Return the average load per task on the cpu's run queue
2367 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002368static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07002369{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002370 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002371 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07002372 unsigned long n = rq->nr_running;
2373
Ingo Molnardd41f592007-07-09 18:51:59 +02002374 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
Nick Piggin147cbb42005-06-25 14:57:19 -07002377/*
2378 * find_idlest_group finds and returns the least busy CPU group within the
2379 * domain.
2380 */
2381static struct sched_group *
2382find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2383{
2384 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2385 unsigned long min_load = ULONG_MAX, this_load = 0;
2386 int load_idx = sd->forkexec_idx;
2387 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2388
2389 do {
2390 unsigned long load, avg_load;
2391 int local_group;
2392 int i;
2393
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002394 /* Skip over this group if it has no CPUs allowed */
2395 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002396 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002397
Nick Piggin147cbb42005-06-25 14:57:19 -07002398 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002399
2400 /* Tally up the load of all CPUs in the group */
2401 avg_load = 0;
2402
2403 for_each_cpu_mask(i, group->cpumask) {
2404 /* Bias balancing toward cpus of our domain */
2405 if (local_group)
2406 load = source_load(i, load_idx);
2407 else
2408 load = target_load(i, load_idx);
2409
2410 avg_load += load;
2411 }
2412
2413 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002414 avg_load = sg_div_cpu_power(group,
2415 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002416
2417 if (local_group) {
2418 this_load = avg_load;
2419 this = group;
2420 } else if (avg_load < min_load) {
2421 min_load = avg_load;
2422 idlest = group;
2423 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002424 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002425
2426 if (!idlest || 100*this_load < imbalance*min_load)
2427 return NULL;
2428 return idlest;
2429}
2430
2431/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002432 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002433 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002434static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002435find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2436 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002437{
2438 unsigned long load, min_load = ULONG_MAX;
2439 int idlest = -1;
2440 int i;
2441
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002442 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002443 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002444
Mike Travis7c16ec52008-04-04 18:11:11 -07002445 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002446 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002447
2448 if (load < min_load || (load == min_load && i == this_cpu)) {
2449 min_load = load;
2450 idlest = i;
2451 }
2452 }
2453
2454 return idlest;
2455}
2456
Nick Piggin476d1392005-06-25 14:57:29 -07002457/*
2458 * sched_balance_self: balance the current task (running on cpu) in domains
2459 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2460 * SD_BALANCE_EXEC.
2461 *
2462 * Balance, ie. select the least loaded group.
2463 *
2464 * Returns the target CPU number, or the same CPU if no balancing is needed.
2465 *
2466 * preempt must be disabled.
2467 */
2468static int sched_balance_self(int cpu, int flag)
2469{
2470 struct task_struct *t = current;
2471 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002472
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002473 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002474 /*
2475 * If power savings logic is enabled for a domain, stop there.
2476 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002477 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2478 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002479 if (tmp->flags & flag)
2480 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002481 }
Nick Piggin476d1392005-06-25 14:57:29 -07002482
2483 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002484 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002485 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002486 int new_cpu, weight;
2487
2488 if (!(sd->flags & flag)) {
2489 sd = sd->child;
2490 continue;
2491 }
Nick Piggin476d1392005-06-25 14:57:29 -07002492
2493 span = sd->span;
2494 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002495 if (!group) {
2496 sd = sd->child;
2497 continue;
2498 }
Nick Piggin476d1392005-06-25 14:57:29 -07002499
Mike Travis7c16ec52008-04-04 18:11:11 -07002500 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002501 if (new_cpu == -1 || new_cpu == cpu) {
2502 /* Now try balancing at a lower domain level of cpu */
2503 sd = sd->child;
2504 continue;
2505 }
Nick Piggin476d1392005-06-25 14:57:29 -07002506
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002507 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002508 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002509 sd = NULL;
2510 weight = cpus_weight(span);
2511 for_each_domain(cpu, tmp) {
2512 if (weight <= cpus_weight(tmp->span))
2513 break;
2514 if (tmp->flags & flag)
2515 sd = tmp;
2516 }
2517 /* while loop will break here if sd == NULL */
2518 }
2519
2520 return cpu;
2521}
2522
2523#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525/***
2526 * try_to_wake_up - wake up a thread
2527 * @p: the to-be-woken-up thread
2528 * @state: the mask of task states that can be woken
2529 * @sync: do a synchronous wakeup?
2530 *
2531 * Put it on the run-queue if it's not already there. The "current"
2532 * thread is always on the run-queue (except when the actual
2533 * re-schedule is in progress), and as such you're allowed to do
2534 * the simpler "current->state = TASK_RUNNING" to mark yourself
2535 * runnable without the overhead of this.
2536 *
2537 * returns failure only if the task is already active.
2538 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002539static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
Ingo Molnarcc367732007-10-15 17:00:18 +02002541 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 unsigned long flags;
2543 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002544 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Ingo Molnarb85d0662008-03-16 20:03:22 +01002546 if (!sched_feat(SYNC_WAKEUPS))
2547 sync = 0;
2548
Linus Torvalds04e2f172008-02-23 18:05:03 -08002549 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 rq = task_rq_lock(p, &flags);
2551 old_state = p->state;
2552 if (!(old_state & state))
2553 goto out;
2554
Ingo Molnardd41f592007-07-09 18:51:59 +02002555 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 goto out_running;
2557
2558 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002559 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 this_cpu = smp_processor_id();
2561
2562#ifdef CONFIG_SMP
2563 if (unlikely(task_running(rq, p)))
2564 goto out_activate;
2565
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002566 cpu = p->sched_class->select_task_rq(p, sync);
2567 if (cpu != orig_cpu) {
2568 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 task_rq_unlock(rq, &flags);
2570 /* might preempt at this point */
2571 rq = task_rq_lock(p, &flags);
2572 old_state = p->state;
2573 if (!(old_state & state))
2574 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002575 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 goto out_running;
2577
2578 this_cpu = smp_processor_id();
2579 cpu = task_cpu(p);
2580 }
2581
Gregory Haskinse7693a32008-01-25 21:08:09 +01002582#ifdef CONFIG_SCHEDSTATS
2583 schedstat_inc(rq, ttwu_count);
2584 if (cpu == this_cpu)
2585 schedstat_inc(rq, ttwu_local);
2586 else {
2587 struct sched_domain *sd;
2588 for_each_domain(this_cpu, sd) {
2589 if (cpu_isset(cpu, sd->span)) {
2590 schedstat_inc(sd, ttwu_wake_remote);
2591 break;
2592 }
2593 }
2594 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002595#endif
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597out_activate:
2598#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002599 schedstat_inc(p, se.nr_wakeups);
2600 if (sync)
2601 schedstat_inc(p, se.nr_wakeups_sync);
2602 if (orig_cpu != cpu)
2603 schedstat_inc(p, se.nr_wakeups_migrate);
2604 if (cpu == this_cpu)
2605 schedstat_inc(p, se.nr_wakeups_local);
2606 else
2607 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002608 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002609 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 success = 1;
2611
2612out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002613 check_preempt_curr(rq, p);
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002616#ifdef CONFIG_SMP
2617 if (p->sched_class->task_wake_up)
2618 p->sched_class->task_wake_up(rq, p);
2619#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620out:
2621 task_rq_unlock(rq, &flags);
2622
2623 return success;
2624}
2625
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002626int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002628 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630EXPORT_SYMBOL(wake_up_process);
2631
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002632int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
2634 return try_to_wake_up(p, state, 0);
2635}
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637/*
2638 * Perform scheduler related setup for a newly forked process p.
2639 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002640 *
2641 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002643static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Ingo Molnardd41f592007-07-09 18:51:59 +02002645 p->se.exec_start = 0;
2646 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002647 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002648 p->se.last_wakeup = 0;
2649 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002650
2651#ifdef CONFIG_SCHEDSTATS
2652 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002653 p->se.sum_sleep_runtime = 0;
2654 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002655 p->se.block_start = 0;
2656 p->se.sleep_max = 0;
2657 p->se.block_max = 0;
2658 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002659 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002660 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002661#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002662
Peter Zijlstrafa717062008-01-25 21:08:27 +01002663 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002664 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002665 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002666
Avi Kivitye107be32007-07-26 13:40:43 +02002667#ifdef CONFIG_PREEMPT_NOTIFIERS
2668 INIT_HLIST_HEAD(&p->preempt_notifiers);
2669#endif
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 /*
2672 * We mark the process as running here, but have not actually
2673 * inserted it onto the runqueue yet. This guarantees that
2674 * nobody will actually run it, and a signal or other external
2675 * event cannot wake it up and insert it on the runqueue either.
2676 */
2677 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002678}
2679
2680/*
2681 * fork()/clone()-time setup:
2682 */
2683void sched_fork(struct task_struct *p, int clone_flags)
2684{
2685 int cpu = get_cpu();
2686
2687 __sched_fork(p);
2688
2689#ifdef CONFIG_SMP
2690 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2691#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002692 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002693
2694 /*
2695 * Make sure we do not leak PI boosting priority to the child:
2696 */
2697 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002698 if (!rt_prio(p->prio))
2699 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002700
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002701#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002702 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002703 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002705#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002706 p->oncpu = 0;
2707#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002709 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002710 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002712 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
2715/*
2716 * wake_up_new_task - wake up a newly created task for the first time.
2717 *
2718 * This function will do some initial scheduler statistics housekeeping
2719 * that must be done for every newly created context, then puts the task
2720 * on the runqueue and wakes it.
2721 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002722void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
2724 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002725 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
2727 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002729 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 p->prio = effective_prio(p);
2732
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002733 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002734 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002737 * Let the scheduling class do new task startup
2738 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002740 p->sched_class->task_new(rq, p);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002741 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002743 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002744#ifdef CONFIG_SMP
2745 if (p->sched_class->task_wake_up)
2746 p->sched_class->task_wake_up(rq, p);
2747#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002748 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
Avi Kivitye107be32007-07-26 13:40:43 +02002751#ifdef CONFIG_PREEMPT_NOTIFIERS
2752
2753/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002754 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2755 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002756 */
2757void preempt_notifier_register(struct preempt_notifier *notifier)
2758{
2759 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2760}
2761EXPORT_SYMBOL_GPL(preempt_notifier_register);
2762
2763/**
2764 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002765 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002766 *
2767 * This is safe to call from within a preemption notifier.
2768 */
2769void preempt_notifier_unregister(struct preempt_notifier *notifier)
2770{
2771 hlist_del(&notifier->link);
2772}
2773EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2774
2775static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2776{
2777 struct preempt_notifier *notifier;
2778 struct hlist_node *node;
2779
2780 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2781 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2782}
2783
2784static void
2785fire_sched_out_preempt_notifiers(struct task_struct *curr,
2786 struct task_struct *next)
2787{
2788 struct preempt_notifier *notifier;
2789 struct hlist_node *node;
2790
2791 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2792 notifier->ops->sched_out(notifier, next);
2793}
2794
2795#else
2796
2797static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2798{
2799}
2800
2801static void
2802fire_sched_out_preempt_notifiers(struct task_struct *curr,
2803 struct task_struct *next)
2804{
2805}
2806
2807#endif
2808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002810 * prepare_task_switch - prepare to switch tasks
2811 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002812 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002813 * @next: the task we are going to switch to.
2814 *
2815 * This is called with the rq lock held and interrupts off. It must
2816 * be paired with a subsequent finish_task_switch after the context
2817 * switch.
2818 *
2819 * prepare_task_switch sets up locking and calls architecture specific
2820 * hooks.
2821 */
Avi Kivitye107be32007-07-26 13:40:43 +02002822static inline void
2823prepare_task_switch(struct rq *rq, struct task_struct *prev,
2824 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002825{
Avi Kivitye107be32007-07-26 13:40:43 +02002826 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002827 prepare_lock_switch(rq, next);
2828 prepare_arch_switch(next);
2829}
2830
2831/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002833 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 * @prev: the thread we just switched away from.
2835 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002836 * finish_task_switch must be called after the context switch, paired
2837 * with a prepare_task_switch call before the context switch.
2838 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2839 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 *
2841 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002842 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 * with the lock held can cause deadlocks; see schedule() for
2844 * details.)
2845 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002846static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 __releases(rq->lock)
2848{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002850 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 rq->prev_mm = NULL;
2853
2854 /*
2855 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002856 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002857 * schedule one last time. The schedule call will never return, and
2858 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002859 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 * still held, otherwise prev could be scheduled on another cpu, die
2861 * there before we look at prev->state, and then the reference would
2862 * be dropped twice.
2863 * Manfred Spraul <manfred@colorfullife.com>
2864 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002865 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002866 finish_arch_switch(prev);
2867 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002868#ifdef CONFIG_SMP
2869 if (current->sched_class->post_schedule)
2870 current->sched_class->post_schedule(rq);
2871#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002872
Avi Kivitye107be32007-07-26 13:40:43 +02002873 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 if (mm)
2875 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002876 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002877 /*
2878 * Remove function-return probe instances associated with this
2879 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002880 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002881 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
2886/**
2887 * schedule_tail - first thing a freshly forked thread must call.
2888 * @prev: the thread we just switched away from.
2889 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002890asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 __releases(rq->lock)
2892{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002893 struct rq *rq = this_rq();
2894
Nick Piggin4866cde2005-06-25 14:57:23 -07002895 finish_task_switch(rq, prev);
2896#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2897 /* In this case, finish_task_switch does not reenable preemption */
2898 preempt_enable();
2899#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002901 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
2904/*
2905 * context_switch - switch to the new MM and the new
2906 * thread's register state.
2907 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002908static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002909context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002910 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
Ingo Molnardd41f592007-07-09 18:51:59 +02002912 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Avi Kivitye107be32007-07-26 13:40:43 +02002914 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002915 mm = next->mm;
2916 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002917 /*
2918 * For paravirt, this is coupled with an exit in switch_to to
2919 * combine the page table reload and the switch backend into
2920 * one hypercall.
2921 */
2922 arch_enter_lazy_cpu_mode();
2923
Ingo Molnardd41f592007-07-09 18:51:59 +02002924 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 next->active_mm = oldmm;
2926 atomic_inc(&oldmm->mm_count);
2927 enter_lazy_tlb(oldmm, next);
2928 } else
2929 switch_mm(oldmm, mm, next);
2930
Ingo Molnardd41f592007-07-09 18:51:59 +02002931 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 rq->prev_mm = oldmm;
2934 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002935 /*
2936 * Since the runqueue lock will be released by the next
2937 * task (which is an invalid locking op but in the case
2938 * of the scheduler it's an obvious special-case), so we
2939 * do an early lockdep release here:
2940 */
2941#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002942 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002943#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 /* Here we just switch the register state and the stack. */
2946 switch_to(prev, next, prev);
2947
Ingo Molnardd41f592007-07-09 18:51:59 +02002948 barrier();
2949 /*
2950 * this_rq must be evaluated again because prev may have moved
2951 * CPUs since it called schedule(), thus the 'rq' on its stack
2952 * frame will be invalid.
2953 */
2954 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955}
2956
2957/*
2958 * nr_running, nr_uninterruptible and nr_context_switches:
2959 *
2960 * externally visible scheduler statistics: current number of runnable
2961 * threads, current number of uninterruptible-sleeping threads, total
2962 * number of context switches performed since bootup.
2963 */
2964unsigned long nr_running(void)
2965{
2966 unsigned long i, sum = 0;
2967
2968 for_each_online_cpu(i)
2969 sum += cpu_rq(i)->nr_running;
2970
2971 return sum;
2972}
2973
2974unsigned long nr_uninterruptible(void)
2975{
2976 unsigned long i, sum = 0;
2977
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002978 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 sum += cpu_rq(i)->nr_uninterruptible;
2980
2981 /*
2982 * Since we read the counters lockless, it might be slightly
2983 * inaccurate. Do not allow it to go below zero though:
2984 */
2985 if (unlikely((long)sum < 0))
2986 sum = 0;
2987
2988 return sum;
2989}
2990
2991unsigned long long nr_context_switches(void)
2992{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002993 int i;
2994 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002996 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 sum += cpu_rq(i)->nr_switches;
2998
2999 return sum;
3000}
3001
3002unsigned long nr_iowait(void)
3003{
3004 unsigned long i, sum = 0;
3005
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003006 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 sum += atomic_read(&cpu_rq(i)->nr_iowait);
3008
3009 return sum;
3010}
3011
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003012unsigned long nr_active(void)
3013{
3014 unsigned long i, running = 0, uninterruptible = 0;
3015
3016 for_each_online_cpu(i) {
3017 running += cpu_rq(i)->nr_running;
3018 uninterruptible += cpu_rq(i)->nr_uninterruptible;
3019 }
3020
3021 if (unlikely((long)uninterruptible < 0))
3022 uninterruptible = 0;
3023
3024 return running + uninterruptible;
3025}
3026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003028 * Update rq->cpu_load[] statistics. This function is usually called every
3029 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07003030 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003031static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003032{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02003033 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02003034 int i, scale;
3035
3036 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02003037
3038 /* Update our load: */
3039 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
3040 unsigned long old_load, new_load;
3041
3042 /* scale is effectively 1 << i now, and >> i divides by scale */
3043
3044 old_load = this_rq->cpu_load[i];
3045 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02003046 /*
3047 * Round up the averaging division if load is increasing. This
3048 * prevents us from getting stuck on 9 if the load is 10, for
3049 * example.
3050 */
3051 if (new_load > old_load)
3052 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003053 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3054 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003055}
3056
Ingo Molnardd41f592007-07-09 18:51:59 +02003057#ifdef CONFIG_SMP
3058
Ingo Molnar48f24c42006-07-03 00:25:40 -07003059/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 * double_rq_lock - safely lock two runqueues
3061 *
3062 * Note this does not disable interrupts like task_rq_lock,
3063 * you need to do so manually before calling.
3064 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003065static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 __acquires(rq1->lock)
3067 __acquires(rq2->lock)
3068{
Kirill Korotaev054b9102006-12-10 02:20:11 -08003069 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (rq1 == rq2) {
3071 spin_lock(&rq1->lock);
3072 __acquire(rq2->lock); /* Fake it out ;) */
3073 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003074 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 spin_lock(&rq1->lock);
3076 spin_lock(&rq2->lock);
3077 } else {
3078 spin_lock(&rq2->lock);
3079 spin_lock(&rq1->lock);
3080 }
3081 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003082 update_rq_clock(rq1);
3083 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
3086/*
3087 * double_rq_unlock - safely unlock two runqueues
3088 *
3089 * Note this does not restore interrupts like task_rq_unlock,
3090 * you need to do so manually after calling.
3091 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003092static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 __releases(rq1->lock)
3094 __releases(rq2->lock)
3095{
3096 spin_unlock(&rq1->lock);
3097 if (rq1 != rq2)
3098 spin_unlock(&rq2->lock);
3099 else
3100 __release(rq2->lock);
3101}
3102
3103/*
3104 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3105 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01003106static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 __releases(this_rq->lock)
3108 __acquires(busiest->lock)
3109 __acquires(this_rq->lock)
3110{
Steven Rostedte8fa1362008-01-25 21:08:05 +01003111 int ret = 0;
3112
Kirill Korotaev054b9102006-12-10 02:20:11 -08003113 if (unlikely(!irqs_disabled())) {
3114 /* printk() doesn't work good under rq->lock */
3115 spin_unlock(&this_rq->lock);
3116 BUG_ON(1);
3117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003119 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 spin_unlock(&this_rq->lock);
3121 spin_lock(&busiest->lock);
3122 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01003123 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 } else
3125 spin_lock(&busiest->lock);
3126 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01003127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128}
3129
3130/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 * If dest_cpu is allowed for this process, migrate the task to it.
3132 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003133 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 * the cpu_allowed mask is restored.
3135 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003136static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003138 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003140 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142 rq = task_rq_lock(p, &flags);
3143 if (!cpu_isset(dest_cpu, p->cpus_allowed)
3144 || unlikely(cpu_is_offline(dest_cpu)))
3145 goto out;
3146
3147 /* force the process onto the specified CPU */
3148 if (migrate_task(p, dest_cpu, &req)) {
3149 /* Need to wait for migration thread (might exit: take ref). */
3150 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 get_task_struct(mt);
3153 task_rq_unlock(rq, &flags);
3154 wake_up_process(mt);
3155 put_task_struct(mt);
3156 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 return;
3159 }
3160out:
3161 task_rq_unlock(rq, &flags);
3162}
3163
3164/*
Nick Piggin476d1392005-06-25 14:57:29 -07003165 * sched_exec - execve() is a valuable balancing opportunity, because at
3166 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 */
3168void sched_exec(void)
3169{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003171 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003173 if (new_cpu != this_cpu)
3174 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
3176
3177/*
3178 * pull_task - move a task from a remote runqueue to the local runqueue.
3179 * Both runqueues must be locked.
3180 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003181static void pull_task(struct rq *src_rq, struct task_struct *p,
3182 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003184 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003186 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 /*
3188 * Note that idle threads have a prio of MAX_PRIO, for this test
3189 * to be always true for them.
3190 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003191 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
3194/*
3195 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3196 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003197static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003198int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003199 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003200 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202 /*
3203 * We do not migrate tasks that are:
3204 * 1) running (obviously), or
3205 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3206 * 3) are cache-hot on their current CPU.
3207 */
Ingo Molnarcc367732007-10-15 17:00:18 +02003208 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3209 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003211 }
Nick Piggin81026792005-06-25 14:57:07 -07003212 *all_pinned = 0;
3213
Ingo Molnarcc367732007-10-15 17:00:18 +02003214 if (task_running(rq, p)) {
3215 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003216 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Ingo Molnarda84d962007-10-15 17:00:18 +02003219 /*
3220 * Aggressive migration if:
3221 * 1) task is cache cold, or
3222 * 2) too many balance attempts have failed.
3223 */
3224
Ingo Molnar6bc16652007-10-15 17:00:18 +02003225 if (!task_hot(p, rq->clock, sd) ||
3226 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003227#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02003228 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003229 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003230 schedstat_inc(p, se.nr_forced_migrations);
3231 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003232#endif
3233 return 1;
3234 }
3235
Ingo Molnarcc367732007-10-15 17:00:18 +02003236 if (task_hot(p, rq->clock, sd)) {
3237 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003238 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return 1;
3241}
3242
Peter Williamse1d14842007-10-24 18:23:51 +02003243static unsigned long
3244balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3245 unsigned long max_load_move, struct sched_domain *sd,
3246 enum cpu_idle_type idle, int *all_pinned,
3247 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003248{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003249 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02003250 struct task_struct *p;
3251 long rem_load_move = max_load_move;
3252
Peter Williamse1d14842007-10-24 18:23:51 +02003253 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003254 goto out;
3255
3256 pinned = 1;
3257
3258 /*
3259 * Start the load-balancing iterator:
3260 */
3261 p = iterator->start(iterator->arg);
3262next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003263 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003264 goto out;
3265 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003266 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02003267 * skip a task if it will be the highest priority task (i.e. smallest
3268 * prio value) on its new queue regardless of its load weight
3269 */
3270 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
3271 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003272 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003273 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003274 p = iterator->next(iterator->arg);
3275 goto next;
3276 }
3277
3278 pull_task(busiest, p, this_rq, this_cpu);
3279 pulled++;
3280 rem_load_move -= p->se.load.weight;
3281
3282 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003283 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003284 */
Peter Williamse1d14842007-10-24 18:23:51 +02003285 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003286 if (p->prio < *this_best_prio)
3287 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003288 p = iterator->next(iterator->arg);
3289 goto next;
3290 }
3291out:
3292 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003293 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003294 * so we can safely collect pull_task() stats here rather than
3295 * inside pull_task().
3296 */
3297 schedstat_add(sd, lb_gained[idle], pulled);
3298
3299 if (all_pinned)
3300 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003301
3302 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003303}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305/*
Peter Williams43010652007-08-09 11:16:46 +02003306 * move_tasks tries to move up to max_load_move weighted load from busiest to
3307 * this_rq, as part of a balancing operation within domain "sd".
3308 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 *
3310 * Called with both runqueues locked.
3311 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003312static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003313 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003314 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003315 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003317 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003318 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003319 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Ingo Molnardd41f592007-07-09 18:51:59 +02003321 do {
Peter Williams43010652007-08-09 11:16:46 +02003322 total_load_moved +=
3323 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003324 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003325 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003326 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003327 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Peter Williams43010652007-08-09 11:16:46 +02003329 return total_load_moved > 0;
3330}
3331
Peter Williamse1d14842007-10-24 18:23:51 +02003332static int
3333iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3334 struct sched_domain *sd, enum cpu_idle_type idle,
3335 struct rq_iterator *iterator)
3336{
3337 struct task_struct *p = iterator->start(iterator->arg);
3338 int pinned = 0;
3339
3340 while (p) {
3341 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3342 pull_task(busiest, p, this_rq, this_cpu);
3343 /*
3344 * Right now, this is only the second place pull_task()
3345 * is called, so we can safely collect pull_task()
3346 * stats here rather than inside pull_task().
3347 */
3348 schedstat_inc(sd, lb_gained[idle]);
3349
3350 return 1;
3351 }
3352 p = iterator->next(iterator->arg);
3353 }
3354
3355 return 0;
3356}
3357
Peter Williams43010652007-08-09 11:16:46 +02003358/*
3359 * move_one_task tries to move exactly one task from busiest to this_rq, as
3360 * part of active balancing operations within "domain".
3361 * Returns 1 if successful and 0 otherwise.
3362 *
3363 * Called with both runqueues locked.
3364 */
3365static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3366 struct sched_domain *sd, enum cpu_idle_type idle)
3367{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003368 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003369
3370 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003371 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003372 return 1;
3373
3374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
3376
3377/*
3378 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003379 * domain. It calculates and returns the amount of weighted load which
3380 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 */
3382static struct sched_group *
3383find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003384 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003385 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
3387 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3388 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003389 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003390 unsigned long busiest_load_per_task, busiest_nr_running;
3391 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003392 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003393#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3394 int power_savings_balance = 1;
3395 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3396 unsigned long min_nr_running = ULONG_MAX;
3397 struct sched_group *group_min = NULL, *group_leader = NULL;
3398#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
3400 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003401 busiest_load_per_task = busiest_nr_running = 0;
3402 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003403 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003404 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003405 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003406 load_idx = sd->newidle_idx;
3407 else
3408 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
3410 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003411 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 int local_group;
3413 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003414 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003415 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003416 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
3418 local_group = cpu_isset(this_cpu, group->cpumask);
3419
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003420 if (local_group)
3421 balance_cpu = first_cpu(group->cpumask);
3422
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003424 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02003425 max_cpu_load = 0;
3426 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
3428 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003429 struct rq *rq;
3430
3431 if (!cpu_isset(i, *cpus))
3432 continue;
3433
3434 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003435
Suresh Siddha9439aab2007-07-19 21:28:35 +02003436 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003437 *sd_idle = 0;
3438
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003440 if (local_group) {
3441 if (idle_cpu(i) && !first_idle_cpu) {
3442 first_idle_cpu = 1;
3443 balance_cpu = i;
3444 }
3445
Nick Piggina2000572006-02-10 01:51:02 -08003446 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003447 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003448 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003449 if (load > max_cpu_load)
3450 max_cpu_load = load;
3451 if (min_cpu_load > load)
3452 min_cpu_load = load;
3453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
3455 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003456 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003457 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 }
3459
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003460 /*
3461 * First idle cpu or the first cpu(busiest) in this sched group
3462 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003463 * domains. In the newly idle case, we will allow all the cpu's
3464 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003465 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003466 if (idle != CPU_NEWLY_IDLE && local_group &&
3467 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003468 *balance = 0;
3469 goto ret;
3470 }
3471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003473 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
3475 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003476 avg_load = sg_div_cpu_power(group,
3477 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Ken Chen908a7c12007-10-17 16:55:11 +02003479 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3480 __group_imb = 1;
3481
Eric Dumazet5517d862007-05-08 00:32:57 -07003482 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 if (local_group) {
3485 this_load = avg_load;
3486 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003487 this_nr_running = sum_nr_running;
3488 this_load_per_task = sum_weighted_load;
3489 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003490 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 max_load = avg_load;
3492 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003493 busiest_nr_running = sum_nr_running;
3494 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003495 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003497
3498#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3499 /*
3500 * Busy processors will not participate in power savings
3501 * balance.
3502 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003503 if (idle == CPU_NOT_IDLE ||
3504 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3505 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003506
3507 /*
3508 * If the local group is idle or completely loaded
3509 * no need to do power savings balance at this domain
3510 */
3511 if (local_group && (this_nr_running >= group_capacity ||
3512 !this_nr_running))
3513 power_savings_balance = 0;
3514
Ingo Molnardd41f592007-07-09 18:51:59 +02003515 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003516 * If a group is already running at full capacity or idle,
3517 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003518 */
3519 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003520 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003521 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003522
Ingo Molnardd41f592007-07-09 18:51:59 +02003523 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003524 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003525 * This is the group from where we need to pick up the load
3526 * for saving power
3527 */
3528 if ((sum_nr_running < min_nr_running) ||
3529 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003530 first_cpu(group->cpumask) <
3531 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003532 group_min = group;
3533 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003534 min_load_per_task = sum_weighted_load /
3535 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003536 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003537
Ingo Molnardd41f592007-07-09 18:51:59 +02003538 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003539 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003540 * capacity but still has some space to pick up some load
3541 * from other group and save more power
3542 */
3543 if (sum_nr_running <= group_capacity - 1) {
3544 if (sum_nr_running > leader_nr_running ||
3545 (sum_nr_running == leader_nr_running &&
3546 first_cpu(group->cpumask) >
3547 first_cpu(group_leader->cpumask))) {
3548 group_leader = group;
3549 leader_nr_running = sum_nr_running;
3550 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003551 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003552group_next:
3553#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 group = group->next;
3555 } while (group != sd->groups);
3556
Peter Williams2dd73a42006-06-27 02:54:34 -07003557 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 goto out_balanced;
3559
3560 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3561
3562 if (this_load >= avg_load ||
3563 100*max_load <= sd->imbalance_pct*this_load)
3564 goto out_balanced;
3565
Peter Williams2dd73a42006-06-27 02:54:34 -07003566 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003567 if (group_imb)
3568 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 /*
3571 * We're trying to get all the cpus to the average_load, so we don't
3572 * want to push ourselves above the average load, nor do we wish to
3573 * reduce the max loaded cpu below the average load, as either of these
3574 * actions would just result in more rebalancing later, and ping-pong
3575 * tasks around. Thus we look for the minimum possible imbalance.
3576 * Negative imbalances (*we* are more loaded than anyone else) will
3577 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003578 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 * appear as very large values with unsigned longs.
3580 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003581 if (max_load <= busiest_load_per_task)
3582 goto out_balanced;
3583
3584 /*
3585 * In the presence of smp nice balancing, certain scenarios can have
3586 * max load less than avg load(as we skip the groups at or below
3587 * its cpu_power, while calculating max_load..)
3588 */
3589 if (max_load < avg_load) {
3590 *imbalance = 0;
3591 goto small_imbalance;
3592 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003593
3594 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003595 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003598 *imbalance = min(max_pull * busiest->__cpu_power,
3599 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 / SCHED_LOAD_SCALE;
3601
Peter Williams2dd73a42006-06-27 02:54:34 -07003602 /*
3603 * if *imbalance is less than the average load per runnable task
3604 * there is no gaurantee that any tasks will be moved so we'll have
3605 * a think about bumping its value to force at least one task to be
3606 * moved
3607 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003608 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003609 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003610 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Peter Williams2dd73a42006-06-27 02:54:34 -07003612small_imbalance:
3613 pwr_move = pwr_now = 0;
3614 imbn = 2;
3615 if (this_nr_running) {
3616 this_load_per_task /= this_nr_running;
3617 if (busiest_load_per_task > this_load_per_task)
3618 imbn = 1;
3619 } else
3620 this_load_per_task = SCHED_LOAD_SCALE;
3621
Ingo Molnardd41f592007-07-09 18:51:59 +02003622 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3623 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003624 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 return busiest;
3626 }
3627
3628 /*
3629 * OK, we don't have enough imbalance to justify moving tasks,
3630 * however we may be able to increase total CPU power used by
3631 * moving them.
3632 */
3633
Eric Dumazet5517d862007-05-08 00:32:57 -07003634 pwr_now += busiest->__cpu_power *
3635 min(busiest_load_per_task, max_load);
3636 pwr_now += this->__cpu_power *
3637 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 pwr_now /= SCHED_LOAD_SCALE;
3639
3640 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003641 tmp = sg_div_cpu_power(busiest,
3642 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003644 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003645 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
3647 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003648 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003649 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003650 tmp = sg_div_cpu_power(this,
3651 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003653 tmp = sg_div_cpu_power(this,
3654 busiest_load_per_task * SCHED_LOAD_SCALE);
3655 pwr_move += this->__cpu_power *
3656 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 pwr_move /= SCHED_LOAD_SCALE;
3658
3659 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003660 if (pwr_move > pwr_now)
3661 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 }
3663
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return busiest;
3665
3666out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003667#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003668 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003669 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003671 if (this == group_leader && group_leader != group_min) {
3672 *imbalance = min_load_per_task;
3673 return group_min;
3674 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003675#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003676ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 *imbalance = 0;
3678 return NULL;
3679}
3680
3681/*
3682 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3683 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003684static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003685find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003686 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003688 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003689 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 int i;
3691
3692 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003693 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003694
3695 if (!cpu_isset(i, *cpus))
3696 continue;
3697
Ingo Molnar48f24c42006-07-03 00:25:40 -07003698 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003699 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Ingo Molnardd41f592007-07-09 18:51:59 +02003701 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003702 continue;
3703
Ingo Molnardd41f592007-07-09 18:51:59 +02003704 if (wl > max_load) {
3705 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003706 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 }
3708 }
3709
3710 return busiest;
3711}
3712
3713/*
Nick Piggin77391d72005-06-25 14:57:30 -07003714 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3715 * so long as it is large enough.
3716 */
3717#define MAX_PINNED_INTERVAL 512
3718
3719/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3721 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003723static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003724 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003725 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
Peter Williams43010652007-08-09 11:16:46 +02003727 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003730 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003731 unsigned long flags;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003732 int unlock_aggregate;
Nick Piggin5969fe02005-09-10 00:26:19 -07003733
Mike Travis7c16ec52008-04-04 18:11:11 -07003734 cpus_setall(*cpus);
3735
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003736 unlock_aggregate = get_aggregate(sd);
3737
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003738 /*
3739 * When power savings policy is enabled for the parent domain, idle
3740 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003741 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003742 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003743 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003744 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003745 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003746 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
Ingo Molnar2d723762007-10-15 17:00:12 +02003748 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003750redo:
3751 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003752 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003753
Chen, Kenneth W06066712006-12-10 02:20:35 -08003754 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003755 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 if (!group) {
3758 schedstat_inc(sd, lb_nobusyg[idle]);
3759 goto out_balanced;
3760 }
3761
Mike Travis7c16ec52008-04-04 18:11:11 -07003762 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 if (!busiest) {
3764 schedstat_inc(sd, lb_nobusyq[idle]);
3765 goto out_balanced;
3766 }
3767
Nick Piggindb935db2005-06-25 14:57:11 -07003768 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
3770 schedstat_add(sd, lb_imbalance[idle], imbalance);
3771
Peter Williams43010652007-08-09 11:16:46 +02003772 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 if (busiest->nr_running > 1) {
3774 /*
3775 * Attempt to move tasks. If find_busiest_group has found
3776 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003777 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 * correctly treated as an imbalance.
3779 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003780 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003781 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003782 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003783 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003784 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003785 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003786
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003787 /*
3788 * some other cpu did the load balance for us.
3789 */
Peter Williams43010652007-08-09 11:16:46 +02003790 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003791 resched_cpu(this_cpu);
3792
Nick Piggin81026792005-06-25 14:57:07 -07003793 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003794 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003795 cpu_clear(cpu_of(busiest), *cpus);
3796 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003797 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003798 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 }
Nick Piggin81026792005-06-25 14:57:07 -07003801
Peter Williams43010652007-08-09 11:16:46 +02003802 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 schedstat_inc(sd, lb_failed[idle]);
3804 sd->nr_balance_failed++;
3805
3806 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003808 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003809
3810 /* don't kick the migration_thread, if the curr
3811 * task on busiest cpu can't be moved to this_cpu
3812 */
3813 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003814 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003815 all_pinned = 1;
3816 goto out_one_pinned;
3817 }
3818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 if (!busiest->active_balance) {
3820 busiest->active_balance = 1;
3821 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003822 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003824 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003825 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 wake_up_process(busiest->migration_thread);
3827
3828 /*
3829 * We've kicked active balancing, reset the failure
3830 * counter.
3831 */
Nick Piggin39507452005-06-25 14:57:09 -07003832 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 }
Nick Piggin81026792005-06-25 14:57:07 -07003834 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 sd->nr_balance_failed = 0;
3836
Nick Piggin81026792005-06-25 14:57:07 -07003837 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 /* We were unbalanced, so reset the balancing interval */
3839 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003840 } else {
3841 /*
3842 * If we've begun active balancing, start to back off. This
3843 * case may not be covered by the all_pinned logic if there
3844 * is only 1 task on the busy runqueue (because we don't call
3845 * move_tasks).
3846 */
3847 if (sd->balance_interval < sd->max_interval)
3848 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 }
3850
Peter Williams43010652007-08-09 11:16:46 +02003851 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003852 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003853 ld_moved = -1;
3854
3855 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
3857out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 schedstat_inc(sd, lb_balanced[idle]);
3859
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003860 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003861
3862out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003864 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3865 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 sd->balance_interval *= 2;
3867
Ingo Molnar48f24c42006-07-03 00:25:40 -07003868 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003869 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003870 ld_moved = -1;
3871 else
3872 ld_moved = 0;
3873out:
3874 if (unlock_aggregate)
3875 put_aggregate(sd);
3876 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877}
3878
3879/*
3880 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3881 * tasks if there is an imbalance.
3882 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003883 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 * this_rq is locked.
3885 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003886static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003887load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3888 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
3890 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003891 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003893 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003894 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003895 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003896
3897 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003898
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003899 /*
3900 * When power savings policy is enabled for the parent domain, idle
3901 * sibling can pick up load irrespective of busy siblings. In this case,
3902 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003903 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003904 */
3905 if (sd->flags & SD_SHARE_CPUPOWER &&
3906 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003907 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Ingo Molnar2d723762007-10-15 17:00:12 +02003909 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003910redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003911 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003912 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003914 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003915 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 }
3917
Mike Travis7c16ec52008-04-04 18:11:11 -07003918 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003919 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003920 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003921 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 }
3923
Nick Piggindb935db2005-06-25 14:57:11 -07003924 BUG_ON(busiest == this_rq);
3925
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003926 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003927
Peter Williams43010652007-08-09 11:16:46 +02003928 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003929 if (busiest->nr_running > 1) {
3930 /* Attempt to move tasks */
3931 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003932 /* this_rq->clock is already updated */
3933 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003934 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003935 imbalance, sd, CPU_NEWLY_IDLE,
3936 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003937 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003938
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003939 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003940 cpu_clear(cpu_of(busiest), *cpus);
3941 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003942 goto redo;
3943 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003944 }
3945
Peter Williams43010652007-08-09 11:16:46 +02003946 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003947 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003948 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3949 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003950 return -1;
3951 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003952 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
Peter Williams43010652007-08-09 11:16:46 +02003954 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003955
3956out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003957 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003958 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003959 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003960 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003961 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003962
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003963 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964}
3965
3966/*
3967 * idle_balance is called by schedule() if this_cpu is about to become
3968 * idle. Attempts to pull tasks from other CPUs.
3969 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003970static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971{
3972 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003973 int pulled_task = -1;
3974 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003975 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
3977 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003978 unsigned long interval;
3979
3980 if (!(sd->flags & SD_LOAD_BALANCE))
3981 continue;
3982
3983 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003984 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003985 pulled_task = load_balance_newidle(this_cpu, this_rq,
3986 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003987
3988 interval = msecs_to_jiffies(sd->balance_interval);
3989 if (time_after(next_balance, sd->last_balance + interval))
3990 next_balance = sd->last_balance + interval;
3991 if (pulled_task)
3992 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003994 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003995 /*
3996 * We are going idle. next_balance may be set based on
3997 * a busy processor. So reset next_balance.
3998 */
3999 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02004000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
4003/*
4004 * active_load_balance is run by migration threads. It pushes running tasks
4005 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
4006 * running on each physical CPU where possible, and avoids physical /
4007 * logical imbalances.
4008 *
4009 * Called with busiest_rq locked.
4010 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004011static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012{
Nick Piggin39507452005-06-25 14:57:09 -07004013 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004014 struct sched_domain *sd;
4015 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07004016
Ingo Molnar48f24c42006-07-03 00:25:40 -07004017 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07004018 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07004019 return;
4020
4021 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
4023 /*
Nick Piggin39507452005-06-25 14:57:09 -07004024 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004025 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07004026 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 */
Nick Piggin39507452005-06-25 14:57:09 -07004028 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Nick Piggin39507452005-06-25 14:57:09 -07004030 /* move a task from busiest_rq to target_rq */
4031 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02004032 update_rq_clock(busiest_rq);
4033 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Nick Piggin39507452005-06-25 14:57:09 -07004035 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07004036 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07004037 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004038 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07004039 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07004040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Ingo Molnar48f24c42006-07-03 00:25:40 -07004042 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004043 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Peter Williams43010652007-08-09 11:16:46 +02004045 if (move_one_task(target_rq, target_cpu, busiest_rq,
4046 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07004047 schedstat_inc(sd, alb_pushed);
4048 else
4049 schedstat_inc(sd, alb_failed);
4050 }
Nick Piggin39507452005-06-25 14:57:09 -07004051 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052}
4053
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004054#ifdef CONFIG_NO_HZ
4055static struct {
4056 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004057 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004058} nohz ____cacheline_aligned = {
4059 .load_balancer = ATOMIC_INIT(-1),
4060 .cpu_mask = CPU_MASK_NONE,
4061};
4062
Christoph Lameter7835b982006-12-10 02:20:22 -08004063/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004064 * This routine will try to nominate the ilb (idle load balancing)
4065 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
4066 * load balancing on behalf of all those cpus. If all the cpus in the system
4067 * go into this tickless mode, then there will be no ilb owner (as there is
4068 * no need for one) and all the cpus will sleep till the next wakeup event
4069 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08004070 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004071 * For the ilb owner, tick is not stopped. And this tick will be used
4072 * for idle load balancing. ilb owner will still be part of
4073 * nohz.cpu_mask..
4074 *
4075 * While stopping the tick, this cpu will become the ilb owner if there
4076 * is no other owner. And will be the owner till that cpu becomes busy
4077 * or if all cpus in the system stop their ticks at which point
4078 * there is no need for ilb owner.
4079 *
4080 * When the ilb owner becomes busy, it nominates another owner, during the
4081 * next busy scheduler_tick()
4082 */
4083int select_nohz_load_balancer(int stop_tick)
4084{
4085 int cpu = smp_processor_id();
4086
4087 if (stop_tick) {
4088 cpu_set(cpu, nohz.cpu_mask);
4089 cpu_rq(cpu)->in_nohz_recently = 1;
4090
4091 /*
4092 * If we are going offline and still the leader, give up!
4093 */
4094 if (cpu_is_offline(cpu) &&
4095 atomic_read(&nohz.load_balancer) == cpu) {
4096 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4097 BUG();
4098 return 0;
4099 }
4100
4101 /* time for ilb owner also to sleep */
4102 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4103 if (atomic_read(&nohz.load_balancer) == cpu)
4104 atomic_set(&nohz.load_balancer, -1);
4105 return 0;
4106 }
4107
4108 if (atomic_read(&nohz.load_balancer) == -1) {
4109 /* make me the ilb owner */
4110 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4111 return 1;
4112 } else if (atomic_read(&nohz.load_balancer) == cpu)
4113 return 1;
4114 } else {
4115 if (!cpu_isset(cpu, nohz.cpu_mask))
4116 return 0;
4117
4118 cpu_clear(cpu, nohz.cpu_mask);
4119
4120 if (atomic_read(&nohz.load_balancer) == cpu)
4121 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4122 BUG();
4123 }
4124 return 0;
4125}
4126#endif
4127
4128static DEFINE_SPINLOCK(balancing);
4129
4130/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004131 * It checks each scheduling domain to see if it is due to be balanced,
4132 * and initiates a balancing operation if so.
4133 *
4134 * Balancing parameters are set up in arch_init_sched_domains.
4135 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004136static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08004137{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004138 int balance = 1;
4139 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004140 unsigned long interval;
4141 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004142 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004143 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004144 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07004145 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004147 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 if (!(sd->flags & SD_LOAD_BALANCE))
4149 continue;
4150
4151 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004152 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 interval *= sd->busy_factor;
4154
4155 /* scale ms to jiffies */
4156 interval = msecs_to_jiffies(interval);
4157 if (unlikely(!interval))
4158 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004159 if (interval > HZ*NR_CPUS/10)
4160 interval = HZ*NR_CPUS/10;
4161
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
Christoph Lameter08c183f2006-12-10 02:20:29 -08004163 if (sd->flags & SD_SERIALIZE) {
4164 if (!spin_trylock(&balancing))
4165 goto out;
4166 }
4167
Christoph Lameterc9819f42006-12-10 02:20:25 -08004168 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07004169 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004170 /*
4171 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004172 * longer idle, or one of our SMT siblings is
4173 * not idle.
4174 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004175 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004177 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08004179 if (sd->flags & SD_SERIALIZE)
4180 spin_unlock(&balancing);
4181out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004182 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004183 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004184 update_next_balance = 1;
4185 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004186
4187 /*
4188 * Stop the load balance at this level. There is another
4189 * CPU in our sched group which is doing load balancing more
4190 * actively.
4191 */
4192 if (!balance)
4193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004195
4196 /*
4197 * next_balance will be updated only when there is a need.
4198 * When the cpu is attached to null domain for ex, it will not be
4199 * updated.
4200 */
4201 if (likely(update_next_balance))
4202 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004203}
4204
4205/*
4206 * run_rebalance_domains is triggered when needed from the scheduler tick.
4207 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4208 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4209 */
4210static void run_rebalance_domains(struct softirq_action *h)
4211{
Ingo Molnardd41f592007-07-09 18:51:59 +02004212 int this_cpu = smp_processor_id();
4213 struct rq *this_rq = cpu_rq(this_cpu);
4214 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4215 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004216
Ingo Molnardd41f592007-07-09 18:51:59 +02004217 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004218
4219#ifdef CONFIG_NO_HZ
4220 /*
4221 * If this cpu is the owner for idle load balancing, then do the
4222 * balancing on behalf of the other idle cpus whose ticks are
4223 * stopped.
4224 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004225 if (this_rq->idle_at_tick &&
4226 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004227 cpumask_t cpus = nohz.cpu_mask;
4228 struct rq *rq;
4229 int balance_cpu;
4230
Ingo Molnardd41f592007-07-09 18:51:59 +02004231 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004232 for_each_cpu_mask(balance_cpu, cpus) {
4233 /*
4234 * If this cpu gets work to do, stop the load balancing
4235 * work being done for other cpus. Next load
4236 * balancing owner will pick it up.
4237 */
4238 if (need_resched())
4239 break;
4240
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004241 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004242
4243 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004244 if (time_after(this_rq->next_balance, rq->next_balance))
4245 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004246 }
4247 }
4248#endif
4249}
4250
4251/*
4252 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4253 *
4254 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4255 * idle load balancing owner or decide to stop the periodic load balancing,
4256 * if the whole system is idle.
4257 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004258static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004259{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004260#ifdef CONFIG_NO_HZ
4261 /*
4262 * If we were in the nohz mode recently and busy at the current
4263 * scheduler tick, then check if we need to nominate new idle
4264 * load balancer.
4265 */
4266 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4267 rq->in_nohz_recently = 0;
4268
4269 if (atomic_read(&nohz.load_balancer) == cpu) {
4270 cpu_clear(cpu, nohz.cpu_mask);
4271 atomic_set(&nohz.load_balancer, -1);
4272 }
4273
4274 if (atomic_read(&nohz.load_balancer) == -1) {
4275 /*
4276 * simple selection for now: Nominate the
4277 * first cpu in the nohz list to be the next
4278 * ilb owner.
4279 *
4280 * TBD: Traverse the sched domains and nominate
4281 * the nearest cpu in the nohz.cpu_mask.
4282 */
4283 int ilb = first_cpu(nohz.cpu_mask);
4284
Mike Travis434d53b2008-04-04 18:11:04 -07004285 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004286 resched_cpu(ilb);
4287 }
4288 }
4289
4290 /*
4291 * If this cpu is idle and doing idle load balancing for all the
4292 * cpus with ticks stopped, is it time for that to stop?
4293 */
4294 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4295 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4296 resched_cpu(cpu);
4297 return;
4298 }
4299
4300 /*
4301 * If this cpu is idle and the idle load balancing is done by
4302 * someone else, then no need raise the SCHED_SOFTIRQ
4303 */
4304 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4305 cpu_isset(cpu, nohz.cpu_mask))
4306 return;
4307#endif
4308 if (time_after_eq(jiffies, rq->next_balance))
4309 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310}
Ingo Molnardd41f592007-07-09 18:51:59 +02004311
4312#else /* CONFIG_SMP */
4313
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314/*
4315 * on UP we do not need to balance between CPUs:
4316 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004317static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
4319}
Ingo Molnardd41f592007-07-09 18:51:59 +02004320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321#endif
4322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323DEFINE_PER_CPU(struct kernel_stat, kstat);
4324
4325EXPORT_PER_CPU_SYMBOL(kstat);
4326
4327/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004328 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4329 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004331unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004334 u64 ns, delta_exec;
4335 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004336
Ingo Molnar41b86e92007-07-09 18:51:58 +02004337 rq = task_rq_lock(p, &flags);
4338 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004339 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004340 update_rq_clock(rq);
4341 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004342 if ((s64)delta_exec > 0)
4343 ns += delta_exec;
4344 }
4345 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 return ns;
4348}
4349
4350/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 * Account user cpu time to a process.
4352 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 * @cputime: the cpu time spent in user space since the last update
4354 */
4355void account_user_time(struct task_struct *p, cputime_t cputime)
4356{
4357 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4358 cputime64_t tmp;
4359
4360 p->utime = cputime_add(p->utime, cputime);
4361
4362 /* Add user time to cpustat. */
4363 tmp = cputime_to_cputime64(cputime);
4364 if (TASK_NICE(p) > 0)
4365 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4366 else
4367 cpustat->user = cputime64_add(cpustat->user, tmp);
4368}
4369
4370/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004371 * Account guest cpu time to a process.
4372 * @p: the process that the cpu time gets accounted to
4373 * @cputime: the cpu time spent in virtual machine since the last update
4374 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004375static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004376{
4377 cputime64_t tmp;
4378 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4379
4380 tmp = cputime_to_cputime64(cputime);
4381
4382 p->utime = cputime_add(p->utime, cputime);
4383 p->gtime = cputime_add(p->gtime, cputime);
4384
4385 cpustat->user = cputime64_add(cpustat->user, tmp);
4386 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4387}
4388
4389/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004390 * Account scaled user cpu time to a process.
4391 * @p: the process that the cpu time gets accounted to
4392 * @cputime: the cpu time spent in user space since the last update
4393 */
4394void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4395{
4396 p->utimescaled = cputime_add(p->utimescaled, cputime);
4397}
4398
4399/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 * Account system cpu time to a process.
4401 * @p: the process that the cpu time gets accounted to
4402 * @hardirq_offset: the offset to subtract from hardirq_count()
4403 * @cputime: the cpu time spent in kernel space since the last update
4404 */
4405void account_system_time(struct task_struct *p, int hardirq_offset,
4406 cputime_t cputime)
4407{
4408 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004409 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 cputime64_t tmp;
4411
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004412 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4413 account_guest_time(p, cputime);
4414 return;
4415 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004416
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 p->stime = cputime_add(p->stime, cputime);
4418
4419 /* Add system time to cpustat. */
4420 tmp = cputime_to_cputime64(cputime);
4421 if (hardirq_count() - hardirq_offset)
4422 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4423 else if (softirq_count())
4424 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004425 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004427 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4429 else
4430 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4431 /* Account for system time used */
4432 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433}
4434
4435/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004436 * Account scaled system cpu time to a process.
4437 * @p: the process that the cpu time gets accounted to
4438 * @hardirq_offset: the offset to subtract from hardirq_count()
4439 * @cputime: the cpu time spent in kernel space since the last update
4440 */
4441void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4442{
4443 p->stimescaled = cputime_add(p->stimescaled, cputime);
4444}
4445
4446/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 * Account for involuntary wait time.
4448 * @p: the process from which the cpu time has been stolen
4449 * @steal: the cpu time spent in involuntary wait
4450 */
4451void account_steal_time(struct task_struct *p, cputime_t steal)
4452{
4453 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4454 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004455 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
4457 if (p == rq->idle) {
4458 p->stime = cputime_add(p->stime, steal);
4459 if (atomic_read(&rq->nr_iowait) > 0)
4460 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4461 else
4462 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004463 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4465}
4466
Christoph Lameter7835b982006-12-10 02:20:22 -08004467/*
4468 * This function gets called by the timer code, with HZ frequency.
4469 * We call it with interrupts disabled.
4470 *
4471 * It also gets called by the fork code, when changing the parent's
4472 * timeslices.
4473 */
4474void scheduler_tick(void)
4475{
Christoph Lameter7835b982006-12-10 02:20:22 -08004476 int cpu = smp_processor_id();
4477 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004478 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02004479 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08004480
Ingo Molnardd41f592007-07-09 18:51:59 +02004481 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02004482 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02004483 /*
4484 * Let rq->clock advance by at least TICK_NSEC:
4485 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004486 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02004487 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004488 rq->clock_underflows++;
4489 }
Ingo Molnar529c7722007-08-10 23:05:11 +02004490 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02004491 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004492 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004493 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004494 spin_unlock(&rq->lock);
4495
Christoph Lametere418e1c2006-12-10 02:20:23 -08004496#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004497 rq->idle_at_tick = idle_cpu(cpu);
4498 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004499#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500}
4501
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
4503
Srinivasa Ds43627582008-02-23 15:24:04 -08004504void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
4506 /*
4507 * Underflow?
4508 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004509 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4510 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 preempt_count() += val;
4512 /*
4513 * Spinlock count overflowing soon?
4514 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004515 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4516 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517}
4518EXPORT_SYMBOL(add_preempt_count);
4519
Srinivasa Ds43627582008-02-23 15:24:04 -08004520void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521{
4522 /*
4523 * Underflow?
4524 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004525 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4526 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 /*
4528 * Is the spinlock portion underflowing?
4529 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004530 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4531 !(preempt_count() & PREEMPT_MASK)))
4532 return;
4533
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 preempt_count() -= val;
4535}
4536EXPORT_SYMBOL(sub_preempt_count);
4537
4538#endif
4539
4540/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004541 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004543static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
Satyam Sharma838225b2007-10-24 18:23:50 +02004545 struct pt_regs *regs = get_irq_regs();
4546
4547 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4548 prev->comm, prev->pid, preempt_count());
4549
Ingo Molnardd41f592007-07-09 18:51:59 +02004550 debug_show_held_locks(prev);
4551 if (irqs_disabled())
4552 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004553
4554 if (regs)
4555 show_regs(regs);
4556 else
4557 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004558}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Ingo Molnardd41f592007-07-09 18:51:59 +02004560/*
4561 * Various schedule()-time debugging checks and statistics:
4562 */
4563static inline void schedule_debug(struct task_struct *prev)
4564{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004566 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 * schedule() atomically, we ignore that path for now.
4568 * Otherwise, whine if we are scheduling when we should not be.
4569 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004570 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4571 __schedule_bug(prev);
4572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4574
Ingo Molnar2d723762007-10-15 17:00:12 +02004575 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004576#ifdef CONFIG_SCHEDSTATS
4577 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004578 schedstat_inc(this_rq(), bkl_count);
4579 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004580 }
4581#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004582}
4583
4584/*
4585 * Pick up the highest-prio task:
4586 */
4587static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004588pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004589{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004590 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004591 struct task_struct *p;
4592
4593 /*
4594 * Optimization: we know that if all tasks are in
4595 * the fair class we can call that function directly:
4596 */
4597 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004598 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004599 if (likely(p))
4600 return p;
4601 }
4602
4603 class = sched_class_highest;
4604 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004605 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004606 if (p)
4607 return p;
4608 /*
4609 * Will never be NULL as the idle class always
4610 * returns a non-NULL p:
4611 */
4612 class = class->next;
4613 }
4614}
4615
4616/*
4617 * schedule() is the main scheduler function.
4618 */
4619asmlinkage void __sched schedule(void)
4620{
4621 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004622 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004623 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004624 int cpu;
4625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626need_resched:
4627 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004628 cpu = smp_processor_id();
4629 rq = cpu_rq(cpu);
4630 rcu_qsctr_inc(cpu);
4631 prev = rq->curr;
4632 switch_count = &prev->nivcsw;
4633
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 release_kernel_lock(prev);
4635need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
Ingo Molnardd41f592007-07-09 18:51:59 +02004637 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004639 hrtick_clear(rq);
4640
Ingo Molnar1e819952007-10-15 17:00:13 +02004641 /*
4642 * Do the rq-clock update outside the rq lock:
4643 */
4644 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004645 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004646 spin_lock(&rq->lock);
4647 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Ingo Molnardd41f592007-07-09 18:51:59 +02004649 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4650 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004651 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004652 prev->state = TASK_RUNNING;
4653 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004654 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004655 }
4656 switch_count = &prev->nvcsw;
4657 }
4658
Steven Rostedt9a897c52008-01-25 21:08:22 +01004659#ifdef CONFIG_SMP
4660 if (prev->sched_class->pre_schedule)
4661 prev->sched_class->pre_schedule(rq, prev);
4662#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004663
Ingo Molnardd41f592007-07-09 18:51:59 +02004664 if (unlikely(!rq->nr_running))
4665 idle_balance(cpu, rq);
4666
Ingo Molnar31ee5292007-08-09 11:16:49 +02004667 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004668 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01004671 sched_info_switch(prev, next);
4672
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 rq->nr_switches++;
4674 rq->curr = next;
4675 ++*switch_count;
4676
Ingo Molnardd41f592007-07-09 18:51:59 +02004677 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004678 /*
4679 * the context switch might have flipped the stack from under
4680 * us, hence refresh the local variables.
4681 */
4682 cpu = smp_processor_id();
4683 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 } else
4685 spin_unlock_irq(&rq->lock);
4686
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004687 hrtick_set(rq);
4688
4689 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004691
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 preempt_enable_no_resched();
4693 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4694 goto need_resched;
4695}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696EXPORT_SYMBOL(schedule);
4697
4698#ifdef CONFIG_PREEMPT
4699/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004700 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004701 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 * occur there and call schedule directly.
4703 */
4704asmlinkage void __sched preempt_schedule(void)
4705{
4706 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 struct task_struct *task = current;
4708 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004709
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 /*
4711 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004712 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004714 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 return;
4716
Andi Kleen3a5c3592007-10-15 17:00:14 +02004717 do {
4718 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Andi Kleen3a5c3592007-10-15 17:00:14 +02004720 /*
4721 * We keep the big kernel semaphore locked, but we
4722 * clear ->lock_depth so that schedule() doesnt
4723 * auto-release the semaphore:
4724 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004725 saved_lock_depth = task->lock_depth;
4726 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004727 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004728 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004729 sub_preempt_count(PREEMPT_ACTIVE);
4730
4731 /*
4732 * Check again in case we missed a preemption opportunity
4733 * between schedule and now.
4734 */
4735 barrier();
4736 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738EXPORT_SYMBOL(preempt_schedule);
4739
4740/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004741 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 * off of irq context.
4743 * Note, that this is called and return with irqs disabled. This will
4744 * protect us against recursive calling from irq.
4745 */
4746asmlinkage void __sched preempt_schedule_irq(void)
4747{
4748 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 struct task_struct *task = current;
4750 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004751
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004752 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 BUG_ON(ti->preempt_count || !irqs_disabled());
4754
Andi Kleen3a5c3592007-10-15 17:00:14 +02004755 do {
4756 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
Andi Kleen3a5c3592007-10-15 17:00:14 +02004758 /*
4759 * We keep the big kernel semaphore locked, but we
4760 * clear ->lock_depth so that schedule() doesnt
4761 * auto-release the semaphore:
4762 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004763 saved_lock_depth = task->lock_depth;
4764 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004765 local_irq_enable();
4766 schedule();
4767 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004768 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004769 sub_preempt_count(PREEMPT_ACTIVE);
4770
4771 /*
4772 * Check again in case we missed a preemption opportunity
4773 * between schedule and now.
4774 */
4775 barrier();
4776 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777}
4778
4779#endif /* CONFIG_PREEMPT */
4780
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004781int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4782 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004784 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786EXPORT_SYMBOL(default_wake_function);
4787
4788/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004789 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4790 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 * number) then we wake all the non-exclusive tasks and one exclusive task.
4792 *
4793 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004794 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4796 */
4797static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4798 int nr_exclusive, int sync, void *key)
4799{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004800 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004802 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004803 unsigned flags = curr->flags;
4804
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004806 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 break;
4808 }
4809}
4810
4811/**
4812 * __wake_up - wake up threads blocked on a waitqueue.
4813 * @q: the waitqueue
4814 * @mode: which threads
4815 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004816 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004818void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004819 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820{
4821 unsigned long flags;
4822
4823 spin_lock_irqsave(&q->lock, flags);
4824 __wake_up_common(q, mode, nr_exclusive, 0, key);
4825 spin_unlock_irqrestore(&q->lock, flags);
4826}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827EXPORT_SYMBOL(__wake_up);
4828
4829/*
4830 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4831 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004832void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833{
4834 __wake_up_common(q, mode, 1, 0, NULL);
4835}
4836
4837/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004838 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 * @q: the waitqueue
4840 * @mode: which threads
4841 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4842 *
4843 * The sync wakeup differs that the waker knows that it will schedule
4844 * away soon, so while the target thread will be woken up, it will not
4845 * be migrated to another CPU - ie. the two threads are 'synchronized'
4846 * with each other. This can prevent needless bouncing between CPUs.
4847 *
4848 * On UP it can prevent extra preemption.
4849 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004850void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004851__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
4853 unsigned long flags;
4854 int sync = 1;
4855
4856 if (unlikely(!q))
4857 return;
4858
4859 if (unlikely(!nr_exclusive))
4860 sync = 0;
4861
4862 spin_lock_irqsave(&q->lock, flags);
4863 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4864 spin_unlock_irqrestore(&q->lock, flags);
4865}
4866EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4867
Ingo Molnarb15136e2007-10-24 18:23:48 +02004868void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869{
4870 unsigned long flags;
4871
4872 spin_lock_irqsave(&x->wait.lock, flags);
4873 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004874 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 spin_unlock_irqrestore(&x->wait.lock, flags);
4876}
4877EXPORT_SYMBOL(complete);
4878
Ingo Molnarb15136e2007-10-24 18:23:48 +02004879void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880{
4881 unsigned long flags;
4882
4883 spin_lock_irqsave(&x->wait.lock, flags);
4884 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004885 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 spin_unlock_irqrestore(&x->wait.lock, flags);
4887}
4888EXPORT_SYMBOL(complete_all);
4889
Andi Kleen8cbbe862007-10-15 17:00:14 +02004890static inline long __sched
4891do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 if (!x->done) {
4894 DECLARE_WAITQUEUE(wait, current);
4895
4896 wait.flags |= WQ_FLAG_EXCLUSIVE;
4897 __add_wait_queue_tail(&x->wait, &wait);
4898 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004899 if ((state == TASK_INTERRUPTIBLE &&
4900 signal_pending(current)) ||
4901 (state == TASK_KILLABLE &&
4902 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004903 __remove_wait_queue(&x->wait, &wait);
4904 return -ERESTARTSYS;
4905 }
4906 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004908 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004910 if (!timeout) {
4911 __remove_wait_queue(&x->wait, &wait);
4912 return timeout;
4913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 } while (!x->done);
4915 __remove_wait_queue(&x->wait, &wait);
4916 }
4917 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004918 return timeout;
4919}
4920
4921static long __sched
4922wait_for_common(struct completion *x, long timeout, int state)
4923{
4924 might_sleep();
4925
4926 spin_lock_irq(&x->wait.lock);
4927 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004929 return timeout;
4930}
4931
Ingo Molnarb15136e2007-10-24 18:23:48 +02004932void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004933{
4934 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935}
4936EXPORT_SYMBOL(wait_for_completion);
4937
Ingo Molnarb15136e2007-10-24 18:23:48 +02004938unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4940{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004941 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942}
4943EXPORT_SYMBOL(wait_for_completion_timeout);
4944
Andi Kleen8cbbe862007-10-15 17:00:14 +02004945int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946{
Andi Kleen51e97992007-10-18 21:32:55 +02004947 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4948 if (t == -ERESTARTSYS)
4949 return t;
4950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951}
4952EXPORT_SYMBOL(wait_for_completion_interruptible);
4953
Ingo Molnarb15136e2007-10-24 18:23:48 +02004954unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955wait_for_completion_interruptible_timeout(struct completion *x,
4956 unsigned long timeout)
4957{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004958 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959}
4960EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4961
Matthew Wilcox009e5772007-12-06 12:29:54 -05004962int __sched wait_for_completion_killable(struct completion *x)
4963{
4964 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4965 if (t == -ERESTARTSYS)
4966 return t;
4967 return 0;
4968}
4969EXPORT_SYMBOL(wait_for_completion_killable);
4970
Andi Kleen8cbbe862007-10-15 17:00:14 +02004971static long __sched
4972sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004973{
4974 unsigned long flags;
4975 wait_queue_t wait;
4976
4977 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Andi Kleen8cbbe862007-10-15 17:00:14 +02004979 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
Andi Kleen8cbbe862007-10-15 17:00:14 +02004981 spin_lock_irqsave(&q->lock, flags);
4982 __add_wait_queue(q, &wait);
4983 spin_unlock(&q->lock);
4984 timeout = schedule_timeout(timeout);
4985 spin_lock_irq(&q->lock);
4986 __remove_wait_queue(q, &wait);
4987 spin_unlock_irqrestore(&q->lock, flags);
4988
4989 return timeout;
4990}
4991
4992void __sched interruptible_sleep_on(wait_queue_head_t *q)
4993{
4994 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996EXPORT_SYMBOL(interruptible_sleep_on);
4997
Ingo Molnar0fec1712007-07-09 18:52:01 +02004998long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004999interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005001 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003EXPORT_SYMBOL(interruptible_sleep_on_timeout);
5004
Ingo Molnar0fec1712007-07-09 18:52:01 +02005005void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005007 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009EXPORT_SYMBOL(sleep_on);
5010
Ingo Molnar0fec1712007-07-09 18:52:01 +02005011long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005013 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015EXPORT_SYMBOL(sleep_on_timeout);
5016
Ingo Molnarb29739f2006-06-27 02:54:51 -07005017#ifdef CONFIG_RT_MUTEXES
5018
5019/*
5020 * rt_mutex_setprio - set the current priority of a task
5021 * @p: task
5022 * @prio: prio value (kernel-internal form)
5023 *
5024 * This function changes the 'effective' priority of a task. It does
5025 * not touch ->normal_prio like __setscheduler().
5026 *
5027 * Used by the rt_mutex code to implement priority inheritance logic.
5028 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005029void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07005030{
5031 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005032 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005033 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01005034 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005035
5036 BUG_ON(prio < 0 || prio > MAX_PRIO);
5037
5038 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005039 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005040
Andrew Mortond5f9f942007-05-08 20:27:06 -07005041 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005042 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005043 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005044 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005045 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005046 if (running)
5047 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02005048
5049 if (rt_prio(prio))
5050 p->sched_class = &rt_sched_class;
5051 else
5052 p->sched_class = &fair_sched_class;
5053
Ingo Molnarb29739f2006-06-27 02:54:51 -07005054 p->prio = prio;
5055
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005056 if (running)
5057 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005058 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005059 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005060
5061 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005062 }
5063 task_rq_unlock(rq, &flags);
5064}
5065
5066#endif
5067
Ingo Molnar36c8b582006-07-03 00:25:41 -07005068void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069{
Ingo Molnardd41f592007-07-09 18:51:59 +02005070 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005072 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
5074 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5075 return;
5076 /*
5077 * We have to be careful, if called from sys_setpriority(),
5078 * the task might be in the middle of scheduling on another CPU.
5079 */
5080 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005081 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 /*
5083 * The RT priorities are set via sched_setscheduler(), but we still
5084 * allow the 'normal' nice value to be set - but as expected
5085 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02005086 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 */
Ingo Molnare05606d2007-07-09 18:51:59 +02005088 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 p->static_prio = NICE_TO_PRIO(nice);
5090 goto out_unlock;
5091 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005092 on_rq = p->se.on_rq;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02005093 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005094 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07005097 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005098 old_prio = p->prio;
5099 p->prio = effective_prio(p);
5100 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
Ingo Molnardd41f592007-07-09 18:51:59 +02005102 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005103 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07005105 * If the task increased its priority or is running and
5106 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005108 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 resched_task(rq->curr);
5110 }
5111out_unlock:
5112 task_rq_unlock(rq, &flags);
5113}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114EXPORT_SYMBOL(set_user_nice);
5115
Matt Mackalle43379f2005-05-01 08:59:00 -07005116/*
5117 * can_nice - check if a task can reduce its nice value
5118 * @p: task
5119 * @nice: nice value
5120 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005121int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005122{
Matt Mackall024f4742005-08-18 11:24:19 -07005123 /* convert nice value [19,-20] to rlimit style value [1,40] */
5124 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005125
Matt Mackalle43379f2005-05-01 08:59:00 -07005126 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5127 capable(CAP_SYS_NICE));
5128}
5129
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130#ifdef __ARCH_WANT_SYS_NICE
5131
5132/*
5133 * sys_nice - change the priority of the current process.
5134 * @increment: priority increment
5135 *
5136 * sys_setpriority is a more generic, but much slower function that
5137 * does similar things.
5138 */
5139asmlinkage long sys_nice(int increment)
5140{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005141 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142
5143 /*
5144 * Setpriority might change our priority at the same moment.
5145 * We don't have to worry. Conceptually one call occurs first
5146 * and we have a single winner.
5147 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005148 if (increment < -40)
5149 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 if (increment > 40)
5151 increment = 40;
5152
5153 nice = PRIO_TO_NICE(current->static_prio) + increment;
5154 if (nice < -20)
5155 nice = -20;
5156 if (nice > 19)
5157 nice = 19;
5158
Matt Mackalle43379f2005-05-01 08:59:00 -07005159 if (increment < 0 && !can_nice(current, nice))
5160 return -EPERM;
5161
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 retval = security_task_setnice(current, nice);
5163 if (retval)
5164 return retval;
5165
5166 set_user_nice(current, nice);
5167 return 0;
5168}
5169
5170#endif
5171
5172/**
5173 * task_prio - return the priority value of a given task.
5174 * @p: the task in question.
5175 *
5176 * This is the priority value as seen by users in /proc.
5177 * RT tasks are offset by -200. Normal tasks are centered
5178 * around 0, value goes from -16 to +15.
5179 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005180int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181{
5182 return p->prio - MAX_RT_PRIO;
5183}
5184
5185/**
5186 * task_nice - return the nice value of a given task.
5187 * @p: the task in question.
5188 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005189int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190{
5191 return TASK_NICE(p);
5192}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005193EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
5195/**
5196 * idle_cpu - is a given cpu idle currently?
5197 * @cpu: the processor in question.
5198 */
5199int idle_cpu(int cpu)
5200{
5201 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5202}
5203
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204/**
5205 * idle_task - return the idle task for a given cpu.
5206 * @cpu: the processor in question.
5207 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005208struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209{
5210 return cpu_rq(cpu)->idle;
5211}
5212
5213/**
5214 * find_process_by_pid - find a process with a matching PID value.
5215 * @pid: the pid in question.
5216 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005217static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005219 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220}
5221
5222/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005223static void
5224__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225{
Ingo Molnardd41f592007-07-09 18:51:59 +02005226 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005227
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005229 switch (p->policy) {
5230 case SCHED_NORMAL:
5231 case SCHED_BATCH:
5232 case SCHED_IDLE:
5233 p->sched_class = &fair_sched_class;
5234 break;
5235 case SCHED_FIFO:
5236 case SCHED_RR:
5237 p->sched_class = &rt_sched_class;
5238 break;
5239 }
5240
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005242 p->normal_prio = normal_prio(p);
5243 /* we are holding p->pi_lock already */
5244 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005245 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246}
5247
5248/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005249 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 * @p: the task in question.
5251 * @policy: new policy.
5252 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005253 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005254 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005256int sched_setscheduler(struct task_struct *p, int policy,
5257 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005259 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005261 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005262 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
Steven Rostedt66e53932006-06-27 02:54:44 -07005264 /* may grab non-irq protected spin_locks */
5265 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266recheck:
5267 /* double check policy once rq lock held */
5268 if (policy < 0)
5269 policy = oldpolicy = p->policy;
5270 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005271 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5272 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005273 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 /*
5275 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005276 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5277 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 */
5279 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005280 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005281 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005283 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 return -EINVAL;
5285
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005286 /*
5287 * Allow unprivileged RT tasks to decrease priority:
5288 */
5289 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005290 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005291 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005292
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005293 if (!lock_task_sighand(p, &flags))
5294 return -ESRCH;
5295 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5296 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005297
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005298 /* can't set/change the rt policy */
5299 if (policy != p->policy && !rlim_rtprio)
5300 return -EPERM;
5301
5302 /* can't increase priority */
5303 if (param->sched_priority > p->rt_priority &&
5304 param->sched_priority > rlim_rtprio)
5305 return -EPERM;
5306 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005307 /*
5308 * Like positive nice levels, dont allow tasks to
5309 * move out of SCHED_IDLE either:
5310 */
5311 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5312 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005313
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005314 /* can't change other user's priorities */
5315 if ((current->euid != p->euid) &&
5316 (current->euid != p->uid))
5317 return -EPERM;
5318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005320#ifdef CONFIG_RT_GROUP_SCHED
5321 /*
5322 * Do not allow realtime tasks into groups that have no runtime
5323 * assigned.
5324 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02005325 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005326 return -EPERM;
5327#endif
5328
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 retval = security_task_setscheduler(p, policy, param);
5330 if (retval)
5331 return retval;
5332 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005333 * make sure no PI-waiters arrive (or leave) while we are
5334 * changing the priority of the task:
5335 */
5336 spin_lock_irqsave(&p->pi_lock, flags);
5337 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 * To be able to change p->policy safely, the apropriate
5339 * runqueue lock must be held.
5340 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005341 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 /* recheck policy now with rq lock held */
5343 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5344 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005345 __task_rq_unlock(rq);
5346 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 goto recheck;
5348 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005349 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005350 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005351 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005352 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005353 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005354 if (running)
5355 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005356
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005358 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005359
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005360 if (running)
5361 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005362 if (on_rq) {
5363 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005364
5365 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005367 __task_rq_unlock(rq);
5368 spin_unlock_irqrestore(&p->pi_lock, flags);
5369
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005370 rt_mutex_adjust_pi(p);
5371
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 return 0;
5373}
5374EXPORT_SYMBOL_GPL(sched_setscheduler);
5375
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005376static int
5377do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 struct sched_param lparam;
5380 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005381 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382
5383 if (!param || pid < 0)
5384 return -EINVAL;
5385 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5386 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005387
5388 rcu_read_lock();
5389 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005391 if (p != NULL)
5392 retval = sched_setscheduler(p, policy, &lparam);
5393 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005394
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 return retval;
5396}
5397
5398/**
5399 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5400 * @pid: the pid in question.
5401 * @policy: new policy.
5402 * @param: structure containing the new RT priority.
5403 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005404asmlinkage long
5405sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406{
Jason Baronc21761f2006-01-18 17:43:03 -08005407 /* negative values for policy are not valid */
5408 if (policy < 0)
5409 return -EINVAL;
5410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 return do_sched_setscheduler(pid, policy, param);
5412}
5413
5414/**
5415 * sys_sched_setparam - set/change the RT priority of a thread
5416 * @pid: the pid in question.
5417 * @param: structure containing the new RT priority.
5418 */
5419asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5420{
5421 return do_sched_setscheduler(pid, -1, param);
5422}
5423
5424/**
5425 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5426 * @pid: the pid in question.
5427 */
5428asmlinkage long sys_sched_getscheduler(pid_t pid)
5429{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005430 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005431 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
5433 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005434 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435
5436 retval = -ESRCH;
5437 read_lock(&tasklist_lock);
5438 p = find_process_by_pid(pid);
5439 if (p) {
5440 retval = security_task_getscheduler(p);
5441 if (!retval)
5442 retval = p->policy;
5443 }
5444 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 return retval;
5446}
5447
5448/**
5449 * sys_sched_getscheduler - get the RT priority of a thread
5450 * @pid: the pid in question.
5451 * @param: structure containing the RT priority.
5452 */
5453asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5454{
5455 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005456 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005457 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
5459 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005460 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
5462 read_lock(&tasklist_lock);
5463 p = find_process_by_pid(pid);
5464 retval = -ESRCH;
5465 if (!p)
5466 goto out_unlock;
5467
5468 retval = security_task_getscheduler(p);
5469 if (retval)
5470 goto out_unlock;
5471
5472 lp.sched_priority = p->rt_priority;
5473 read_unlock(&tasklist_lock);
5474
5475 /*
5476 * This one might sleep, we cannot do it with a spinlock held ...
5477 */
5478 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5479
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 return retval;
5481
5482out_unlock:
5483 read_unlock(&tasklist_lock);
5484 return retval;
5485}
5486
Mike Travisb53e9212008-04-04 18:11:08 -07005487long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005490 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005491 struct task_struct *p;
5492 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005494 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 read_lock(&tasklist_lock);
5496
5497 p = find_process_by_pid(pid);
5498 if (!p) {
5499 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005500 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 return -ESRCH;
5502 }
5503
5504 /*
5505 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005506 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 * usage count and then drop tasklist_lock.
5508 */
5509 get_task_struct(p);
5510 read_unlock(&tasklist_lock);
5511
5512 retval = -EPERM;
5513 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5514 !capable(CAP_SYS_NICE))
5515 goto out_unlock;
5516
David Quigleye7834f82006-06-23 02:03:59 -07005517 retval = security_task_setscheduler(p, 0, NULL);
5518 if (retval)
5519 goto out_unlock;
5520
Mike Travisf9a86fc2008-04-04 18:11:07 -07005521 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005523 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005524 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Paul Menage8707d8b2007-10-18 23:40:22 -07005526 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005527 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005528 if (!cpus_subset(new_mask, cpus_allowed)) {
5529 /*
5530 * We must have raced with a concurrent cpuset
5531 * update. Just reset the cpus_allowed to the
5532 * cpuset's cpus_allowed
5533 */
5534 new_mask = cpus_allowed;
5535 goto again;
5536 }
5537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538out_unlock:
5539 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005540 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 return retval;
5542}
5543
5544static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5545 cpumask_t *new_mask)
5546{
5547 if (len < sizeof(cpumask_t)) {
5548 memset(new_mask, 0, sizeof(cpumask_t));
5549 } else if (len > sizeof(cpumask_t)) {
5550 len = sizeof(cpumask_t);
5551 }
5552 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5553}
5554
5555/**
5556 * sys_sched_setaffinity - set the cpu affinity of a process
5557 * @pid: pid of the process
5558 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5559 * @user_mask_ptr: user-space pointer to the new cpu mask
5560 */
5561asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5562 unsigned long __user *user_mask_ptr)
5563{
5564 cpumask_t new_mask;
5565 int retval;
5566
5567 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5568 if (retval)
5569 return retval;
5570
Mike Travisb53e9212008-04-04 18:11:08 -07005571 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572}
5573
5574/*
5575 * Represents all cpu's present in the system
5576 * In systems capable of hotplug, this map could dynamically grow
5577 * as new cpu's are detected in the system via any platform specific
5578 * method, such as ACPI for e.g.
5579 */
5580
Andi Kleen4cef0c62006-01-11 22:44:57 +01005581cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582EXPORT_SYMBOL(cpu_present_map);
5583
5584#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005585cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005586EXPORT_SYMBOL(cpu_online_map);
5587
Andi Kleen4cef0c62006-01-11 22:44:57 +01005588cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005589EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590#endif
5591
5592long sched_getaffinity(pid_t pid, cpumask_t *mask)
5593{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005594 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005597 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 read_lock(&tasklist_lock);
5599
5600 retval = -ESRCH;
5601 p = find_process_by_pid(pid);
5602 if (!p)
5603 goto out_unlock;
5604
David Quigleye7834f82006-06-23 02:03:59 -07005605 retval = security_task_getscheduler(p);
5606 if (retval)
5607 goto out_unlock;
5608
Jack Steiner2f7016d2006-02-01 03:05:18 -08005609 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
5611out_unlock:
5612 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005613 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
Ulrich Drepper9531b622007-08-09 11:16:46 +02005615 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616}
5617
5618/**
5619 * sys_sched_getaffinity - get the cpu affinity of a process
5620 * @pid: pid of the process
5621 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5622 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5623 */
5624asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5625 unsigned long __user *user_mask_ptr)
5626{
5627 int ret;
5628 cpumask_t mask;
5629
5630 if (len < sizeof(cpumask_t))
5631 return -EINVAL;
5632
5633 ret = sched_getaffinity(pid, &mask);
5634 if (ret < 0)
5635 return ret;
5636
5637 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5638 return -EFAULT;
5639
5640 return sizeof(cpumask_t);
5641}
5642
5643/**
5644 * sys_sched_yield - yield the current processor to other threads.
5645 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005646 * This function yields the current CPU to other tasks. If there are no
5647 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 */
5649asmlinkage long sys_sched_yield(void)
5650{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005651 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
Ingo Molnar2d723762007-10-15 17:00:12 +02005653 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005654 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
5656 /*
5657 * Since we are going to call schedule() anyway, there's
5658 * no need to preempt or enable interrupts:
5659 */
5660 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005661 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 _raw_spin_unlock(&rq->lock);
5663 preempt_enable_no_resched();
5664
5665 schedule();
5666
5667 return 0;
5668}
5669
Andrew Mortone7b38402006-06-30 01:56:00 -07005670static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005672#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5673 __might_sleep(__FILE__, __LINE__);
5674#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005675 /*
5676 * The BKS might be reacquired before we have dropped
5677 * PREEMPT_ACTIVE, which could trigger a second
5678 * cond_resched() call.
5679 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 do {
5681 add_preempt_count(PREEMPT_ACTIVE);
5682 schedule();
5683 sub_preempt_count(PREEMPT_ACTIVE);
5684 } while (need_resched());
5685}
5686
Herbert Xu02b67cc32008-01-25 21:08:28 +01005687#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5688int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689{
Ingo Molnar94142322006-12-29 16:48:13 -08005690 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5691 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 __cond_resched();
5693 return 1;
5694 }
5695 return 0;
5696}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005697EXPORT_SYMBOL(_cond_resched);
5698#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699
5700/*
5701 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5702 * call schedule, and on return reacquire the lock.
5703 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005704 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 * operations here to prevent schedule() from being called twice (once via
5706 * spin_unlock(), once by hand).
5707 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005708int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709{
Nick Piggin95c354f2008-01-30 13:31:20 +01005710 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005711 int ret = 0;
5712
Nick Piggin95c354f2008-01-30 13:31:20 +01005713 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005715 if (resched && need_resched())
5716 __cond_resched();
5717 else
5718 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005719 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005722 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724EXPORT_SYMBOL(cond_resched_lock);
5725
5726int __sched cond_resched_softirq(void)
5727{
5728 BUG_ON(!in_softirq());
5729
Ingo Molnar94142322006-12-29 16:48:13 -08005730 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005731 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 __cond_resched();
5733 local_bh_disable();
5734 return 1;
5735 }
5736 return 0;
5737}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738EXPORT_SYMBOL(cond_resched_softirq);
5739
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740/**
5741 * yield - yield the current processor to other threads.
5742 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005743 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 * thread runnable and calls sys_sched_yield().
5745 */
5746void __sched yield(void)
5747{
5748 set_current_state(TASK_RUNNING);
5749 sys_sched_yield();
5750}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751EXPORT_SYMBOL(yield);
5752
5753/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005754 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 * that process accounting knows that this is a task in IO wait state.
5756 *
5757 * But don't do that if it is a deliberate, throttling IO wait (this task
5758 * has set its backing_dev_info: the queue against which it should throttle)
5759 */
5760void __sched io_schedule(void)
5761{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005762 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005764 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 atomic_inc(&rq->nr_iowait);
5766 schedule();
5767 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005768 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770EXPORT_SYMBOL(io_schedule);
5771
5772long __sched io_schedule_timeout(long timeout)
5773{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005774 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 long ret;
5776
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005777 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 atomic_inc(&rq->nr_iowait);
5779 ret = schedule_timeout(timeout);
5780 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005781 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 return ret;
5783}
5784
5785/**
5786 * sys_sched_get_priority_max - return maximum RT priority.
5787 * @policy: scheduling class.
5788 *
5789 * this syscall returns the maximum rt_priority that can be used
5790 * by a given scheduling class.
5791 */
5792asmlinkage long sys_sched_get_priority_max(int policy)
5793{
5794 int ret = -EINVAL;
5795
5796 switch (policy) {
5797 case SCHED_FIFO:
5798 case SCHED_RR:
5799 ret = MAX_USER_RT_PRIO-1;
5800 break;
5801 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005802 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005803 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 ret = 0;
5805 break;
5806 }
5807 return ret;
5808}
5809
5810/**
5811 * sys_sched_get_priority_min - return minimum RT priority.
5812 * @policy: scheduling class.
5813 *
5814 * this syscall returns the minimum rt_priority that can be used
5815 * by a given scheduling class.
5816 */
5817asmlinkage long sys_sched_get_priority_min(int policy)
5818{
5819 int ret = -EINVAL;
5820
5821 switch (policy) {
5822 case SCHED_FIFO:
5823 case SCHED_RR:
5824 ret = 1;
5825 break;
5826 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005827 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005828 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 ret = 0;
5830 }
5831 return ret;
5832}
5833
5834/**
5835 * sys_sched_rr_get_interval - return the default timeslice of a process.
5836 * @pid: pid of the process.
5837 * @interval: userspace pointer to the timeslice value.
5838 *
5839 * this syscall writes the default timeslice value of a given process
5840 * into the user-space timespec buffer. A value of '0' means infinity.
5841 */
5842asmlinkage
5843long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5844{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005845 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005846 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005847 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
5850 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005851 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852
5853 retval = -ESRCH;
5854 read_lock(&tasklist_lock);
5855 p = find_process_by_pid(pid);
5856 if (!p)
5857 goto out_unlock;
5858
5859 retval = security_task_getscheduler(p);
5860 if (retval)
5861 goto out_unlock;
5862
Ingo Molnar77034932007-12-04 17:04:39 +01005863 /*
5864 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5865 * tasks that are on an otherwise idle runqueue:
5866 */
5867 time_slice = 0;
5868 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005869 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005870 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005871 struct sched_entity *se = &p->se;
5872 unsigned long flags;
5873 struct rq *rq;
5874
5875 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005876 if (rq->cfs.load.weight)
5877 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005878 task_rq_unlock(rq, &flags);
5879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005881 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005884
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885out_unlock:
5886 read_unlock(&tasklist_lock);
5887 return retval;
5888}
5889
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005890static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005891
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005892void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005895 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005898 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005899 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005900#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005902 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005904 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905#else
5906 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005907 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005909 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910#endif
5911#ifdef CONFIG_DEBUG_STACK_USAGE
5912 {
Al Viro10ebffd2005-11-13 16:06:56 -08005913 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 while (!*n)
5915 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005916 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 }
5918#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005919 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005920 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005922 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923}
5924
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005925void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005927 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
Ingo Molnar4bd77322007-07-11 21:21:47 +02005929#if BITS_PER_LONG == 32
5930 printk(KERN_INFO
5931 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005933 printk(KERN_INFO
5934 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935#endif
5936 read_lock(&tasklist_lock);
5937 do_each_thread(g, p) {
5938 /*
5939 * reset the NMI-timeout, listing all files on a slow
5940 * console might take alot of time:
5941 */
5942 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005943 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005944 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 } while_each_thread(g, p);
5946
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005947 touch_all_softlockup_watchdogs();
5948
Ingo Molnardd41f592007-07-09 18:51:59 +02005949#ifdef CONFIG_SCHED_DEBUG
5950 sysrq_sched_debug_show();
5951#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005953 /*
5954 * Only show locks if all tasks are dumped:
5955 */
5956 if (state_filter == -1)
5957 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958}
5959
Ingo Molnar1df21052007-07-09 18:51:58 +02005960void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5961{
Ingo Molnardd41f592007-07-09 18:51:59 +02005962 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005963}
5964
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005965/**
5966 * init_idle - set up an idle thread for a given CPU
5967 * @idle: task in question
5968 * @cpu: cpu the idle task belongs to
5969 *
5970 * NOTE: this function does not set the idle thread's NEED_RESCHED
5971 * flag, to make booting more robust.
5972 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005973void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005975 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 unsigned long flags;
5977
Ingo Molnardd41f592007-07-09 18:51:59 +02005978 __sched_fork(idle);
5979 idle->se.exec_start = sched_clock();
5980
Ingo Molnarb29739f2006-06-27 02:54:51 -07005981 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005983 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
5985 spin_lock_irqsave(&rq->lock, flags);
5986 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005987#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5988 idle->oncpu = 1;
5989#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 spin_unlock_irqrestore(&rq->lock, flags);
5991
5992 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f542005-11-13 16:06:55 -08005993 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005994
Ingo Molnardd41f592007-07-09 18:51:59 +02005995 /*
5996 * The idle tasks have their own, simple scheduling class:
5997 */
5998 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999}
6000
6001/*
6002 * In a system that switches off the HZ timer nohz_cpu_mask
6003 * indicates which cpus entered this state. This is used
6004 * in the rcu update to wait only for active cpus. For system
6005 * which do not switch off the HZ timer nohz_cpu_mask should
6006 * always be CPU_MASK_NONE.
6007 */
6008cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
6009
Ingo Molnar19978ca2007-11-09 22:39:38 +01006010/*
6011 * Increase the granularity value when there are more CPUs,
6012 * because with more CPUs the 'effective latency' as visible
6013 * to users decreases. But the relationship is not linear,
6014 * so pick a second-best guess by going with the log2 of the
6015 * number of CPUs.
6016 *
6017 * This idea comes from the SD scheduler of Con Kolivas:
6018 */
6019static inline void sched_init_granularity(void)
6020{
6021 unsigned int factor = 1 + ilog2(num_online_cpus());
6022 const unsigned long limit = 200000000;
6023
6024 sysctl_sched_min_granularity *= factor;
6025 if (sysctl_sched_min_granularity > limit)
6026 sysctl_sched_min_granularity = limit;
6027
6028 sysctl_sched_latency *= factor;
6029 if (sysctl_sched_latency > limit)
6030 sysctl_sched_latency = limit;
6031
6032 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01006033}
6034
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035#ifdef CONFIG_SMP
6036/*
6037 * This is how migration works:
6038 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07006039 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 * runqueue and wake up that CPU's migration thread.
6041 * 2) we down() the locked semaphore => thread blocks.
6042 * 3) migration thread wakes up (implicitly it forces the migrated
6043 * thread off the CPU)
6044 * 4) it gets the migration request and checks whether the migrated
6045 * task is still in the wrong runqueue.
6046 * 5) if it's in the wrong runqueue then the migration thread removes
6047 * it and puts it into the right queue.
6048 * 6) migration thread up()s the semaphore.
6049 * 7) we wake up and the migration is done.
6050 */
6051
6052/*
6053 * Change a given task's CPU affinity. Migrate the thread to a
6054 * proper CPU and schedule it away if the CPU it's executing on
6055 * is removed from the allowed bitmask.
6056 *
6057 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006058 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 * call is not atomic; no spinlocks may be held.
6060 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006061int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006063 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006065 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006066 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
6068 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006069 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 ret = -EINVAL;
6071 goto out;
6072 }
6073
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01006074 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006075 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01006076 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006077 p->cpus_allowed = *new_mask;
6078 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01006079 }
6080
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006082 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 goto out;
6084
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006085 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 /* Need help from migration thread: drop lock and wait. */
6087 task_rq_unlock(rq, &flags);
6088 wake_up_process(rq->migration_thread);
6089 wait_for_completion(&req.done);
6090 tlb_migrate_finish(p->mm);
6091 return 0;
6092 }
6093out:
6094 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006095
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 return ret;
6097}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006098EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
6100/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006101 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 * this because either it can't run here any more (set_cpus_allowed()
6103 * away from this CPU, or CPU going down), or because we're
6104 * attempting to rebalance this task on exec (sched_exec).
6105 *
6106 * So we race with normal scheduler movements, but that's OK, as long
6107 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006108 *
6109 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006111static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006113 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006114 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
6116 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006117 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118
6119 rq_src = cpu_rq(src_cpu);
6120 rq_dest = cpu_rq(dest_cpu);
6121
6122 double_rq_lock(rq_src, rq_dest);
6123 /* Already moved. */
6124 if (task_cpu(p) != src_cpu)
6125 goto out;
6126 /* Affinity changed (again). */
6127 if (!cpu_isset(dest_cpu, p->cpus_allowed))
6128 goto out;
6129
Ingo Molnardd41f592007-07-09 18:51:59 +02006130 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006131 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006132 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006133
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006135 if (on_rq) {
6136 activate_task(rq_dest, p, 0);
6137 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07006139 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140out:
6141 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006142 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143}
6144
6145/*
6146 * migration_thread - this is a highprio system thread that performs
6147 * thread migration by bumping thread off CPU then 'pushing' onto
6148 * another runqueue.
6149 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006150static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006153 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
6155 rq = cpu_rq(cpu);
6156 BUG_ON(rq->migration_thread != current);
6157
6158 set_current_state(TASK_INTERRUPTIBLE);
6159 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006160 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 spin_lock_irq(&rq->lock);
6164
6165 if (cpu_is_offline(cpu)) {
6166 spin_unlock_irq(&rq->lock);
6167 goto wait_to_die;
6168 }
6169
6170 if (rq->active_balance) {
6171 active_load_balance(rq, cpu);
6172 rq->active_balance = 0;
6173 }
6174
6175 head = &rq->migration_queue;
6176
6177 if (list_empty(head)) {
6178 spin_unlock_irq(&rq->lock);
6179 schedule();
6180 set_current_state(TASK_INTERRUPTIBLE);
6181 continue;
6182 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006183 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 list_del_init(head->next);
6185
Nick Piggin674311d2005-06-25 14:57:27 -07006186 spin_unlock(&rq->lock);
6187 __migrate_task(req->task, cpu, req->dest_cpu);
6188 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189
6190 complete(&req->done);
6191 }
6192 __set_current_state(TASK_RUNNING);
6193 return 0;
6194
6195wait_to_die:
6196 /* Wait for kthread_stop */
6197 set_current_state(TASK_INTERRUPTIBLE);
6198 while (!kthread_should_stop()) {
6199 schedule();
6200 set_current_state(TASK_INTERRUPTIBLE);
6201 }
6202 __set_current_state(TASK_RUNNING);
6203 return 0;
6204}
6205
6206#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006207
6208static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6209{
6210 int ret;
6211
6212 local_irq_disable();
6213 ret = __migrate_task(p, src_cpu, dest_cpu);
6214 local_irq_enable();
6215 return ret;
6216}
6217
Kirill Korotaev054b9102006-12-10 02:20:11 -08006218/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006219 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006220 * NOTE: interrupts should be disabled by the caller
6221 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006222static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006224 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006226 struct rq *rq;
6227 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
Andi Kleen3a5c3592007-10-15 17:00:14 +02006229 do {
6230 /* On same node? */
6231 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6232 cpus_and(mask, mask, p->cpus_allowed);
6233 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234
Andi Kleen3a5c3592007-10-15 17:00:14 +02006235 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006236 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006237 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238
Andi Kleen3a5c3592007-10-15 17:00:14 +02006239 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006240 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006241 cpumask_t cpus_allowed;
6242
6243 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006244 /*
6245 * Try to stay on the same cpuset, where the
6246 * current cpuset may be a subset of all cpus.
6247 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006248 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006249 * called within calls to cpuset_lock/cpuset_unlock.
6250 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006251 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006252 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006253 dest_cpu = any_online_cpu(p->cpus_allowed);
6254 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
Andi Kleen3a5c3592007-10-15 17:00:14 +02006256 /*
6257 * Don't tell them about moving exiting tasks or
6258 * kernel threads (both mm NULL), since they never
6259 * leave kernel.
6260 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006261 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006262 printk(KERN_INFO "process %d (%s) no "
6263 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006264 task_pid_nr(p), p->comm, dead_cpu);
6265 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006266 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006267 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268}
6269
6270/*
6271 * While a dead CPU has no uninterruptible tasks queued at this point,
6272 * it might still have a nonzero ->nr_uninterruptible counter, because
6273 * for performance reasons the counter is not stricly tracking tasks to
6274 * their home CPUs. So we just add the counter to another CPU's counter,
6275 * to keep the global sum constant after CPU-down:
6276 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006277static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278{
Mike Travis7c16ec52008-04-04 18:11:11 -07006279 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 unsigned long flags;
6281
6282 local_irq_save(flags);
6283 double_rq_lock(rq_src, rq_dest);
6284 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6285 rq_src->nr_uninterruptible = 0;
6286 double_rq_unlock(rq_src, rq_dest);
6287 local_irq_restore(flags);
6288}
6289
6290/* Run through task list and migrate tasks from the dead cpu. */
6291static void migrate_live_tasks(int src_cpu)
6292{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006293 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006295 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
Ingo Molnar48f24c42006-07-03 00:25:40 -07006297 do_each_thread(t, p) {
6298 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 continue;
6300
Ingo Molnar48f24c42006-07-03 00:25:40 -07006301 if (task_cpu(p) == src_cpu)
6302 move_task_off_dead_cpu(src_cpu, p);
6303 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006305 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306}
6307
Ingo Molnardd41f592007-07-09 18:51:59 +02006308/*
6309 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006310 * It does so by boosting its priority to highest possible.
6311 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 */
6313void sched_idle_next(void)
6314{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006315 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006316 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 struct task_struct *p = rq->idle;
6318 unsigned long flags;
6319
6320 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006321 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322
Ingo Molnar48f24c42006-07-03 00:25:40 -07006323 /*
6324 * Strictly not necessary since rest of the CPUs are stopped by now
6325 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 */
6327 spin_lock_irqsave(&rq->lock, flags);
6328
Ingo Molnardd41f592007-07-09 18:51:59 +02006329 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006330
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006331 update_rq_clock(rq);
6332 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
6334 spin_unlock_irqrestore(&rq->lock, flags);
6335}
6336
Ingo Molnar48f24c42006-07-03 00:25:40 -07006337/*
6338 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 * offline.
6340 */
6341void idle_task_exit(void)
6342{
6343 struct mm_struct *mm = current->active_mm;
6344
6345 BUG_ON(cpu_online(smp_processor_id()));
6346
6347 if (mm != &init_mm)
6348 switch_mm(mm, &init_mm, current);
6349 mmdrop(mm);
6350}
6351
Kirill Korotaev054b9102006-12-10 02:20:11 -08006352/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006353static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006355 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
6357 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006358 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359
6360 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006361 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
Ingo Molnar48f24c42006-07-03 00:25:40 -07006363 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
6365 /*
6366 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006367 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 * fine.
6369 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006370 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006371 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006372 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373
Ingo Molnar48f24c42006-07-03 00:25:40 -07006374 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375}
6376
6377/* release_task() removes task from tasklist, so we won't find dead tasks. */
6378static void migrate_dead_tasks(unsigned int dead_cpu)
6379{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006380 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006381 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
Ingo Molnardd41f592007-07-09 18:51:59 +02006383 for ( ; ; ) {
6384 if (!rq->nr_running)
6385 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006386 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006387 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006388 if (!next)
6389 break;
6390 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006391
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 }
6393}
6394#endif /* CONFIG_HOTPLUG_CPU */
6395
Nick Piggine692ab52007-07-26 13:40:43 +02006396#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6397
6398static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006399 {
6400 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006401 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006402 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006403 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006404};
6405
6406static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006407 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006408 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006409 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006410 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006411 .child = sd_ctl_dir,
6412 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006413 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006414};
6415
6416static struct ctl_table *sd_alloc_ctl_entry(int n)
6417{
6418 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006419 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006420
Nick Piggine692ab52007-07-26 13:40:43 +02006421 return entry;
6422}
6423
Milton Miller6382bc92007-10-15 17:00:19 +02006424static void sd_free_ctl_entry(struct ctl_table **tablep)
6425{
Milton Millercd7900762007-10-17 16:55:11 +02006426 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006427
Milton Millercd7900762007-10-17 16:55:11 +02006428 /*
6429 * In the intermediate directories, both the child directory and
6430 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006431 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006432 * static strings and all have proc handlers.
6433 */
6434 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006435 if (entry->child)
6436 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006437 if (entry->proc_handler == NULL)
6438 kfree(entry->procname);
6439 }
Milton Miller6382bc92007-10-15 17:00:19 +02006440
6441 kfree(*tablep);
6442 *tablep = NULL;
6443}
6444
Nick Piggine692ab52007-07-26 13:40:43 +02006445static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006446set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006447 const char *procname, void *data, int maxlen,
6448 mode_t mode, proc_handler *proc_handler)
6449{
Nick Piggine692ab52007-07-26 13:40:43 +02006450 entry->procname = procname;
6451 entry->data = data;
6452 entry->maxlen = maxlen;
6453 entry->mode = mode;
6454 entry->proc_handler = proc_handler;
6455}
6456
6457static struct ctl_table *
6458sd_alloc_ctl_domain_table(struct sched_domain *sd)
6459{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006460 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006461
Milton Millerad1cdc12007-10-15 17:00:19 +02006462 if (table == NULL)
6463 return NULL;
6464
Alexey Dobriyane0361852007-08-09 11:16:46 +02006465 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006466 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006467 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006468 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006469 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006470 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006471 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006472 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006473 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006474 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006475 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006476 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006477 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006478 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006479 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006480 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006481 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006482 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006483 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006484 &sd->cache_nice_tries,
6485 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006486 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006487 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006488 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006489
6490 return table;
6491}
6492
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006493static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006494{
6495 struct ctl_table *entry, *table;
6496 struct sched_domain *sd;
6497 int domain_num = 0, i;
6498 char buf[32];
6499
6500 for_each_domain(cpu, sd)
6501 domain_num++;
6502 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006503 if (table == NULL)
6504 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006505
6506 i = 0;
6507 for_each_domain(cpu, sd) {
6508 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006509 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006510 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006511 entry->child = sd_alloc_ctl_domain_table(sd);
6512 entry++;
6513 i++;
6514 }
6515 return table;
6516}
6517
6518static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006519static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006520{
6521 int i, cpu_num = num_online_cpus();
6522 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6523 char buf[32];
6524
Milton Miller73785472007-10-24 18:23:48 +02006525 WARN_ON(sd_ctl_dir[0].child);
6526 sd_ctl_dir[0].child = entry;
6527
Milton Millerad1cdc12007-10-15 17:00:19 +02006528 if (entry == NULL)
6529 return;
6530
Milton Miller97b6ea72007-10-15 17:00:19 +02006531 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006532 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006533 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006534 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006535 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006536 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006537 }
Milton Miller73785472007-10-24 18:23:48 +02006538
6539 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006540 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6541}
Milton Miller6382bc92007-10-15 17:00:19 +02006542
Milton Miller73785472007-10-24 18:23:48 +02006543/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006544static void unregister_sched_domain_sysctl(void)
6545{
Milton Miller73785472007-10-24 18:23:48 +02006546 if (sd_sysctl_header)
6547 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006548 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006549 if (sd_ctl_dir[0].child)
6550 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006551}
Nick Piggine692ab52007-07-26 13:40:43 +02006552#else
Milton Miller6382bc92007-10-15 17:00:19 +02006553static void register_sched_domain_sysctl(void)
6554{
6555}
6556static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006557{
6558}
6559#endif
6560
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561/*
6562 * migration_call - callback that gets triggered when a CPU is added.
6563 * Here we can start up the necessary migration thread for the new CPU.
6564 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006565static int __cpuinit
6566migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006569 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006571 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572
6573 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006574
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006576 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006577 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 if (IS_ERR(p))
6579 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 kthread_bind(p, cpu);
6581 /* Must be high prio: stop_machine expects to yield to it. */
6582 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006583 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 task_rq_unlock(rq, &flags);
6585 cpu_rq(cpu)->migration_thread = p;
6586 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006587
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006589 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006590 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006592
6593 /* Update our root-domain */
6594 rq = cpu_rq(cpu);
6595 spin_lock_irqsave(&rq->lock, flags);
6596 if (rq->rd) {
6597 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6598 cpu_set(cpu, rq->rd->online);
6599 }
6600 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006602
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603#ifdef CONFIG_HOTPLUG_CPU
6604 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006605 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006606 if (!cpu_rq(cpu)->migration_thread)
6607 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006608 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006609 kthread_bind(cpu_rq(cpu)->migration_thread,
6610 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 kthread_stop(cpu_rq(cpu)->migration_thread);
6612 cpu_rq(cpu)->migration_thread = NULL;
6613 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006614
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006616 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006617 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 migrate_live_tasks(cpu);
6619 rq = cpu_rq(cpu);
6620 kthread_stop(rq->migration_thread);
6621 rq->migration_thread = NULL;
6622 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006623 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006624 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006625 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006627 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6628 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006630 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006631 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 migrate_nr_uninterruptible(rq);
6633 BUG_ON(rq->nr_running != 0);
6634
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006635 /*
6636 * No need to migrate the tasks: it was best-effort if
6637 * they didn't take sched_hotcpu_mutex. Just wake up
6638 * the requestors.
6639 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 spin_lock_irq(&rq->lock);
6641 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006642 struct migration_req *req;
6643
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006645 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 list_del_init(&req->list);
6647 complete(&req->done);
6648 }
6649 spin_unlock_irq(&rq->lock);
6650 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006651
Gregory Haskins08f503b2008-03-10 17:59:11 -04006652 case CPU_DYING:
6653 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006654 /* Update our root-domain */
6655 rq = cpu_rq(cpu);
6656 spin_lock_irqsave(&rq->lock, flags);
6657 if (rq->rd) {
6658 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6659 cpu_clear(cpu, rq->rd->online);
6660 }
6661 spin_unlock_irqrestore(&rq->lock, flags);
6662 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663#endif
6664 }
6665 return NOTIFY_OK;
6666}
6667
6668/* Register at highest priority so that task migration (migrate_all_tasks)
6669 * happens before everything else.
6670 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006671static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 .notifier_call = migration_call,
6673 .priority = 10
6674};
6675
Adrian Bunke6fe6642007-11-09 22:39:39 +01006676void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677{
6678 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006679 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006680
6681 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006682 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6683 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6685 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686}
6687#endif
6688
6689#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006690
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006691#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006692
Mike Travis7c16ec52008-04-04 18:11:11 -07006693static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6694 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006695{
6696 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006697 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006698
Mike Travis434d53b2008-04-04 18:11:04 -07006699 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006700 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006701
6702 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6703
6704 if (!(sd->flags & SD_LOAD_BALANCE)) {
6705 printk("does not load-balance\n");
6706 if (sd->parent)
6707 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6708 " has parent");
6709 return -1;
6710 }
6711
6712 printk(KERN_CONT "span %s\n", str);
6713
6714 if (!cpu_isset(cpu, sd->span)) {
6715 printk(KERN_ERR "ERROR: domain->span does not contain "
6716 "CPU%d\n", cpu);
6717 }
6718 if (!cpu_isset(cpu, group->cpumask)) {
6719 printk(KERN_ERR "ERROR: domain->groups does not contain"
6720 " CPU%d\n", cpu);
6721 }
6722
6723 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6724 do {
6725 if (!group) {
6726 printk("\n");
6727 printk(KERN_ERR "ERROR: group is NULL\n");
6728 break;
6729 }
6730
6731 if (!group->__cpu_power) {
6732 printk(KERN_CONT "\n");
6733 printk(KERN_ERR "ERROR: domain->cpu_power not "
6734 "set\n");
6735 break;
6736 }
6737
6738 if (!cpus_weight(group->cpumask)) {
6739 printk(KERN_CONT "\n");
6740 printk(KERN_ERR "ERROR: empty group\n");
6741 break;
6742 }
6743
Mike Travis7c16ec52008-04-04 18:11:11 -07006744 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006745 printk(KERN_CONT "\n");
6746 printk(KERN_ERR "ERROR: repeated CPUs\n");
6747 break;
6748 }
6749
Mike Travis7c16ec52008-04-04 18:11:11 -07006750 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006751
Mike Travis434d53b2008-04-04 18:11:04 -07006752 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006753 printk(KERN_CONT " %s", str);
6754
6755 group = group->next;
6756 } while (group != sd->groups);
6757 printk(KERN_CONT "\n");
6758
Mike Travis7c16ec52008-04-04 18:11:11 -07006759 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006760 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6761
Mike Travis7c16ec52008-04-04 18:11:11 -07006762 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006763 printk(KERN_ERR "ERROR: parent span is not a superset "
6764 "of domain->span\n");
6765 return 0;
6766}
6767
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768static void sched_domain_debug(struct sched_domain *sd, int cpu)
6769{
Mike Travis7c16ec52008-04-04 18:11:11 -07006770 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 int level = 0;
6772
Nick Piggin41c7ce92005-06-25 14:57:24 -07006773 if (!sd) {
6774 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6775 return;
6776 }
6777
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6779
Mike Travis7c16ec52008-04-04 18:11:11 -07006780 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6781 if (!groupmask) {
6782 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6783 return;
6784 }
6785
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006786 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006787 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 level++;
6790 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006791 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006792 break;
6793 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006794 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795}
6796#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006797# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798#endif
6799
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006800static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006801{
6802 if (cpus_weight(sd->span) == 1)
6803 return 1;
6804
6805 /* Following flags need at least 2 groups */
6806 if (sd->flags & (SD_LOAD_BALANCE |
6807 SD_BALANCE_NEWIDLE |
6808 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006809 SD_BALANCE_EXEC |
6810 SD_SHARE_CPUPOWER |
6811 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006812 if (sd->groups != sd->groups->next)
6813 return 0;
6814 }
6815
6816 /* Following flags don't use groups */
6817 if (sd->flags & (SD_WAKE_IDLE |
6818 SD_WAKE_AFFINE |
6819 SD_WAKE_BALANCE))
6820 return 0;
6821
6822 return 1;
6823}
6824
Ingo Molnar48f24c42006-07-03 00:25:40 -07006825static int
6826sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006827{
6828 unsigned long cflags = sd->flags, pflags = parent->flags;
6829
6830 if (sd_degenerate(parent))
6831 return 1;
6832
6833 if (!cpus_equal(sd->span, parent->span))
6834 return 0;
6835
6836 /* Does parent contain flags not in child? */
6837 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6838 if (cflags & SD_WAKE_AFFINE)
6839 pflags &= ~SD_WAKE_BALANCE;
6840 /* Flags needing groups don't count if only 1 group in parent */
6841 if (parent->groups == parent->groups->next) {
6842 pflags &= ~(SD_LOAD_BALANCE |
6843 SD_BALANCE_NEWIDLE |
6844 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006845 SD_BALANCE_EXEC |
6846 SD_SHARE_CPUPOWER |
6847 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006848 }
6849 if (~cflags & pflags)
6850 return 0;
6851
6852 return 1;
6853}
6854
Gregory Haskins57d885f2008-01-25 21:08:18 +01006855static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6856{
6857 unsigned long flags;
6858 const struct sched_class *class;
6859
6860 spin_lock_irqsave(&rq->lock, flags);
6861
6862 if (rq->rd) {
6863 struct root_domain *old_rd = rq->rd;
6864
Ingo Molnar0eab9142008-01-25 21:08:19 +01006865 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006866 if (class->leave_domain)
6867 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006868 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006869
Gregory Haskinsdc938522008-01-25 21:08:26 +01006870 cpu_clear(rq->cpu, old_rd->span);
6871 cpu_clear(rq->cpu, old_rd->online);
6872
Gregory Haskins57d885f2008-01-25 21:08:18 +01006873 if (atomic_dec_and_test(&old_rd->refcount))
6874 kfree(old_rd);
6875 }
6876
6877 atomic_inc(&rd->refcount);
6878 rq->rd = rd;
6879
Gregory Haskinsdc938522008-01-25 21:08:26 +01006880 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006881 if (cpu_isset(rq->cpu, cpu_online_map))
6882 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006883
Ingo Molnar0eab9142008-01-25 21:08:19 +01006884 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006885 if (class->join_domain)
6886 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006887 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006888
6889 spin_unlock_irqrestore(&rq->lock, flags);
6890}
6891
Gregory Haskinsdc938522008-01-25 21:08:26 +01006892static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006893{
6894 memset(rd, 0, sizeof(*rd));
6895
Gregory Haskinsdc938522008-01-25 21:08:26 +01006896 cpus_clear(rd->span);
6897 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006898}
6899
6900static void init_defrootdomain(void)
6901{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006902 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006903 atomic_set(&def_root_domain.refcount, 1);
6904}
6905
Gregory Haskinsdc938522008-01-25 21:08:26 +01006906static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006907{
6908 struct root_domain *rd;
6909
6910 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6911 if (!rd)
6912 return NULL;
6913
Gregory Haskinsdc938522008-01-25 21:08:26 +01006914 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006915
6916 return rd;
6917}
6918
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006920 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 * hold the hotplug lock.
6922 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006923static void
6924cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006926 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006927 struct sched_domain *tmp;
6928
6929 /* Remove the sched domains which do not contribute to scheduling. */
6930 for (tmp = sd; tmp; tmp = tmp->parent) {
6931 struct sched_domain *parent = tmp->parent;
6932 if (!parent)
6933 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006934 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006935 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006936 if (parent->parent)
6937 parent->parent->child = tmp;
6938 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006939 }
6940
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006941 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006942 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006943 if (sd)
6944 sd->child = NULL;
6945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946
6947 sched_domain_debug(sd, cpu);
6948
Gregory Haskins57d885f2008-01-25 21:08:18 +01006949 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006950 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951}
6952
6953/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006954static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955
6956/* Setup the mask of cpus configured for isolated domains */
6957static int __init isolated_cpu_setup(char *str)
6958{
6959 int ints[NR_CPUS], i;
6960
6961 str = get_options(str, ARRAY_SIZE(ints), ints);
6962 cpus_clear(cpu_isolated_map);
6963 for (i = 1; i <= ints[0]; i++)
6964 if (ints[i] < NR_CPUS)
6965 cpu_set(ints[i], cpu_isolated_map);
6966 return 1;
6967}
6968
Ingo Molnar8927f492007-10-15 17:00:13 +02006969__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970
6971/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006972 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6973 * to a function which identifies what group(along with sched group) a CPU
6974 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6975 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 *
6977 * init_sched_build_groups will build a circular linked list of the groups
6978 * covered by the given span, and will set each group's ->cpumask correctly,
6979 * and ->cpu_power to 0.
6980 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006981static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006982init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006983 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006984 struct sched_group **sg,
6985 cpumask_t *tmpmask),
6986 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987{
6988 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 int i;
6990
Mike Travis7c16ec52008-04-04 18:11:11 -07006991 cpus_clear(*covered);
6992
6993 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006994 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006995 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 int j;
6997
Mike Travis7c16ec52008-04-04 18:11:11 -07006998 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 continue;
7000
Mike Travis7c16ec52008-04-04 18:11:11 -07007001 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07007002 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003
Mike Travis7c16ec52008-04-04 18:11:11 -07007004 for_each_cpu_mask(j, *span) {
7005 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 continue;
7007
Mike Travis7c16ec52008-04-04 18:11:11 -07007008 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009 cpu_set(j, sg->cpumask);
7010 }
7011 if (!first)
7012 first = sg;
7013 if (last)
7014 last->next = sg;
7015 last = sg;
7016 }
7017 last->next = first;
7018}
7019
John Hawkes9c1cfda2005-09-06 15:18:14 -07007020#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021
John Hawkes9c1cfda2005-09-06 15:18:14 -07007022#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08007023
John Hawkes9c1cfda2005-09-06 15:18:14 -07007024/**
7025 * find_next_best_node - find the next node to include in a sched_domain
7026 * @node: node whose sched_domain we're building
7027 * @used_nodes: nodes already in the sched_domain
7028 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007029 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07007030 * finds the closest node not already in the @used_nodes map.
7031 *
7032 * Should use nodemask_t.
7033 */
Mike Travisc5f59f02008-04-04 18:11:10 -07007034static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007035{
7036 int i, n, val, min_val, best_node = 0;
7037
7038 min_val = INT_MAX;
7039
7040 for (i = 0; i < MAX_NUMNODES; i++) {
7041 /* Start at @node */
7042 n = (node + i) % MAX_NUMNODES;
7043
7044 if (!nr_cpus_node(n))
7045 continue;
7046
7047 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07007048 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007049 continue;
7050
7051 /* Simple min distance search */
7052 val = node_distance(node, n);
7053
7054 if (val < min_val) {
7055 min_val = val;
7056 best_node = n;
7057 }
7058 }
7059
Mike Travisc5f59f02008-04-04 18:11:10 -07007060 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007061 return best_node;
7062}
7063
7064/**
7065 * sched_domain_node_span - get a cpumask for a node's sched_domain
7066 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07007067 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007068 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007069 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007070 * should be one that prevents unnecessary balancing, but also spreads tasks
7071 * out optimally.
7072 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07007073static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007074{
Mike Travisc5f59f02008-04-04 18:11:10 -07007075 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07007076 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007077 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007078
Mike Travis4bdbaad32008-04-15 16:35:52 -07007079 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007080 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007081
Mike Travis4bdbaad32008-04-15 16:35:52 -07007082 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07007083 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007084
7085 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007086 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007087
Mike Travisc5f59f02008-04-04 18:11:10 -07007088 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007089 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007090 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007091}
7092#endif
7093
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007094int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007095
John Hawkes9c1cfda2005-09-06 15:18:14 -07007096/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07007097 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07007098 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099#ifdef CONFIG_SCHED_SMT
7100static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007101static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007102
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007103static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007104cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7105 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007107 if (sg)
7108 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 return cpu;
7110}
7111#endif
7112
Ingo Molnar48f24c42006-07-03 00:25:40 -07007113/*
7114 * multi-core sched-domains:
7115 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007116#ifdef CONFIG_SCHED_MC
7117static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007118static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007119#endif
7120
7121#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007122static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007123cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7124 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007125{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007126 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007127
7128 *mask = per_cpu(cpu_sibling_map, cpu);
7129 cpus_and(*mask, *mask, *cpu_map);
7130 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007131 if (sg)
7132 *sg = &per_cpu(sched_group_core, group);
7133 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007134}
7135#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007136static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007137cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7138 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007139{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007140 if (sg)
7141 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007142 return cpu;
7143}
7144#endif
7145
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007147static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007148
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007149static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007150cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7151 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007153 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007154#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007155 *mask = cpu_coregroup_map(cpu);
7156 cpus_and(*mask, *mask, *cpu_map);
7157 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007158#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007159 *mask = per_cpu(cpu_sibling_map, cpu);
7160 cpus_and(*mask, *mask, *cpu_map);
7161 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007163 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007165 if (sg)
7166 *sg = &per_cpu(sched_group_phys, group);
7167 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168}
7169
7170#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007171/*
7172 * The init_sched_build_groups can't handle what we want to do with node
7173 * groups, so roll our own. Now each node has its own list of groups which
7174 * gets dynamically allocated.
7175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007177static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007178
7179static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007180static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007181
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007182static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007183 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007185 int group;
7186
Mike Travis7c16ec52008-04-04 18:11:11 -07007187 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7188 cpus_and(*nodemask, *nodemask, *cpu_map);
7189 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007190
7191 if (sg)
7192 *sg = &per_cpu(sched_group_allnodes, group);
7193 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007195
Siddha, Suresh B08069032006-03-27 01:15:23 -08007196static void init_numa_sched_groups_power(struct sched_group *group_head)
7197{
7198 struct sched_group *sg = group_head;
7199 int j;
7200
7201 if (!sg)
7202 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007203 do {
7204 for_each_cpu_mask(j, sg->cpumask) {
7205 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007206
Andi Kleen3a5c3592007-10-15 17:00:14 +02007207 sd = &per_cpu(phys_domains, j);
7208 if (j != first_cpu(sd->groups->cpumask)) {
7209 /*
7210 * Only add "power" once for each
7211 * physical package.
7212 */
7213 continue;
7214 }
7215
7216 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007217 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007218 sg = sg->next;
7219 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007220}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221#endif
7222
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007223#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007224/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007225static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007226{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007227 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007228
7229 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007230 struct sched_group **sched_group_nodes
7231 = sched_group_nodes_bycpu[cpu];
7232
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007233 if (!sched_group_nodes)
7234 continue;
7235
7236 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007237 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7238
Mike Travis7c16ec52008-04-04 18:11:11 -07007239 *nodemask = node_to_cpumask(i);
7240 cpus_and(*nodemask, *nodemask, *cpu_map);
7241 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007242 continue;
7243
7244 if (sg == NULL)
7245 continue;
7246 sg = sg->next;
7247next_sg:
7248 oldsg = sg;
7249 sg = sg->next;
7250 kfree(oldsg);
7251 if (oldsg != sched_group_nodes[i])
7252 goto next_sg;
7253 }
7254 kfree(sched_group_nodes);
7255 sched_group_nodes_bycpu[cpu] = NULL;
7256 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007257}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007258#else
Mike Travis7c16ec52008-04-04 18:11:11 -07007259static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007260{
7261}
7262#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007263
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007265 * Initialize sched groups cpu_power.
7266 *
7267 * cpu_power indicates the capacity of sched group, which is used while
7268 * distributing the load between different sched groups in a sched domain.
7269 * Typically cpu_power for all the groups in a sched domain will be same unless
7270 * there are asymmetries in the topology. If there are asymmetries, group
7271 * having more cpu_power will pickup more load compared to the group having
7272 * less cpu_power.
7273 *
7274 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7275 * the maximum number of tasks a group can handle in the presence of other idle
7276 * or lightly loaded groups in the same sched domain.
7277 */
7278static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7279{
7280 struct sched_domain *child;
7281 struct sched_group *group;
7282
7283 WARN_ON(!sd || !sd->groups);
7284
7285 if (cpu != first_cpu(sd->groups->cpumask))
7286 return;
7287
7288 child = sd->child;
7289
Eric Dumazet5517d862007-05-08 00:32:57 -07007290 sd->groups->__cpu_power = 0;
7291
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007292 /*
7293 * For perf policy, if the groups in child domain share resources
7294 * (for example cores sharing some portions of the cache hierarchy
7295 * or SMT), then set this domain groups cpu_power such that each group
7296 * can handle only one task, when there are other idle groups in the
7297 * same sched domain.
7298 */
7299 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7300 (child->flags &
7301 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007302 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007303 return;
7304 }
7305
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007306 /*
7307 * add cpu_power of each child group to this groups cpu_power
7308 */
7309 group = child->groups;
7310 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007311 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007312 group = group->next;
7313 } while (group != child->groups);
7314}
7315
7316/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007317 * Initializers for schedule domains
7318 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7319 */
7320
7321#define SD_INIT(sd, type) sd_init_##type(sd)
7322#define SD_INIT_FUNC(type) \
7323static noinline void sd_init_##type(struct sched_domain *sd) \
7324{ \
7325 memset(sd, 0, sizeof(*sd)); \
7326 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007327 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007328}
7329
7330SD_INIT_FUNC(CPU)
7331#ifdef CONFIG_NUMA
7332 SD_INIT_FUNC(ALLNODES)
7333 SD_INIT_FUNC(NODE)
7334#endif
7335#ifdef CONFIG_SCHED_SMT
7336 SD_INIT_FUNC(SIBLING)
7337#endif
7338#ifdef CONFIG_SCHED_MC
7339 SD_INIT_FUNC(MC)
7340#endif
7341
7342/*
7343 * To minimize stack usage kmalloc room for cpumasks and share the
7344 * space as the usage in build_sched_domains() dictates. Used only
7345 * if the amount of space is significant.
7346 */
7347struct allmasks {
7348 cpumask_t tmpmask; /* make this one first */
7349 union {
7350 cpumask_t nodemask;
7351 cpumask_t this_sibling_map;
7352 cpumask_t this_core_map;
7353 };
7354 cpumask_t send_covered;
7355
7356#ifdef CONFIG_NUMA
7357 cpumask_t domainspan;
7358 cpumask_t covered;
7359 cpumask_t notcovered;
7360#endif
7361};
7362
7363#if NR_CPUS > 128
7364#define SCHED_CPUMASK_ALLOC 1
7365#define SCHED_CPUMASK_FREE(v) kfree(v)
7366#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7367#else
7368#define SCHED_CPUMASK_ALLOC 0
7369#define SCHED_CPUMASK_FREE(v)
7370#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7371#endif
7372
7373#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7374 ((unsigned long)(a) + offsetof(struct allmasks, v))
7375
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007376static int default_relax_domain_level = -1;
7377
7378static int __init setup_relax_domain_level(char *str)
7379{
7380 default_relax_domain_level = simple_strtoul(str, NULL, 0);
7381 return 1;
7382}
7383__setup("relax_domain_level=", setup_relax_domain_level);
7384
7385static void set_domain_attribute(struct sched_domain *sd,
7386 struct sched_domain_attr *attr)
7387{
7388 int request;
7389
7390 if (!attr || attr->relax_domain_level < 0) {
7391 if (default_relax_domain_level < 0)
7392 return;
7393 else
7394 request = default_relax_domain_level;
7395 } else
7396 request = attr->relax_domain_level;
7397 if (request < sd->level) {
7398 /* turn off idle balance on this domain */
7399 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7400 } else {
7401 /* turn on idle balance on this domain */
7402 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7403 }
7404}
7405
Mike Travis7c16ec52008-04-04 18:11:11 -07007406/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007407 * Build sched domains for a given set of cpus and attach the sched domains
7408 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007410static int __build_sched_domains(const cpumask_t *cpu_map,
7411 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412{
7413 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007414 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007415 SCHED_CPUMASK_DECLARE(allmasks);
7416 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007417#ifdef CONFIG_NUMA
7418 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007419 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007420
7421 /*
7422 * Allocate the per-node list of sched groups
7423 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007424 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007425 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007426 if (!sched_group_nodes) {
7427 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007428 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007429 }
John Hawkesd1b55132005-09-06 15:18:14 -07007430#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431
Gregory Haskinsdc938522008-01-25 21:08:26 +01007432 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007433 if (!rd) {
7434 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007435#ifdef CONFIG_NUMA
7436 kfree(sched_group_nodes);
7437#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007438 return -ENOMEM;
7439 }
7440
Mike Travis7c16ec52008-04-04 18:11:11 -07007441#if SCHED_CPUMASK_ALLOC
7442 /* get space for all scratch cpumask variables */
7443 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7444 if (!allmasks) {
7445 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7446 kfree(rd);
7447#ifdef CONFIG_NUMA
7448 kfree(sched_group_nodes);
7449#endif
7450 return -ENOMEM;
7451 }
7452#endif
7453 tmpmask = (cpumask_t *)allmasks;
7454
7455
7456#ifdef CONFIG_NUMA
7457 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7458#endif
7459
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007461 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007463 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007465 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466
Mike Travis7c16ec52008-04-04 18:11:11 -07007467 *nodemask = node_to_cpumask(cpu_to_node(i));
7468 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469
7470#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007471 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007472 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007473 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007474 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007475 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007476 sd->span = *cpu_map;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007477 sd->first_cpu = first_cpu(sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07007478 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007479 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007480 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007481 } else
7482 p = NULL;
7483
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007485 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007486 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007487 sched_domain_node_span(cpu_to_node(i), &sd->span);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007488 sd->first_cpu = first_cpu(sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007489 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007490 if (p)
7491 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007492 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493#endif
7494
7495 p = sd;
7496 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007497 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007498 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007499 sd->span = *nodemask;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007500 sd->first_cpu = first_cpu(sd->span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007502 if (p)
7503 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007504 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007506#ifdef CONFIG_SCHED_MC
7507 p = sd;
7508 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007509 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007510 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007511 sd->span = cpu_coregroup_map(i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007512 sd->first_cpu = first_cpu(sd->span);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007513 cpus_and(sd->span, sd->span, *cpu_map);
7514 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007515 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007516 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007517#endif
7518
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519#ifdef CONFIG_SCHED_SMT
7520 p = sd;
7521 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007522 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007523 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007524 sd->span = per_cpu(cpu_sibling_map, i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007525 sd->first_cpu = first_cpu(sd->span);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007526 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007528 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007529 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530#endif
7531 }
7532
7533#ifdef CONFIG_SCHED_SMT
7534 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007535 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007536 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7537 SCHED_CPUMASK_VAR(send_covered, allmasks);
7538
7539 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7540 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7541 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542 continue;
7543
Ingo Molnardd41f592007-07-09 18:51:59 +02007544 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007545 &cpu_to_cpu_group,
7546 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 }
7548#endif
7549
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007550#ifdef CONFIG_SCHED_MC
7551 /* Set up multi-core groups */
7552 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007553 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7554 SCHED_CPUMASK_VAR(send_covered, allmasks);
7555
7556 *this_core_map = cpu_coregroup_map(i);
7557 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7558 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007559 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007560
Ingo Molnardd41f592007-07-09 18:51:59 +02007561 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007562 &cpu_to_core_group,
7563 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007564 }
7565#endif
7566
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 /* Set up physical groups */
7568 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007569 SCHED_CPUMASK_VAR(nodemask, allmasks);
7570 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571
Mike Travis7c16ec52008-04-04 18:11:11 -07007572 *nodemask = node_to_cpumask(i);
7573 cpus_and(*nodemask, *nodemask, *cpu_map);
7574 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 continue;
7576
Mike Travis7c16ec52008-04-04 18:11:11 -07007577 init_sched_build_groups(nodemask, cpu_map,
7578 &cpu_to_phys_group,
7579 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 }
7581
7582#ifdef CONFIG_NUMA
7583 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007584 if (sd_allnodes) {
7585 SCHED_CPUMASK_VAR(send_covered, allmasks);
7586
7587 init_sched_build_groups(cpu_map, cpu_map,
7588 &cpu_to_allnodes_group,
7589 send_covered, tmpmask);
7590 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007591
7592 for (i = 0; i < MAX_NUMNODES; i++) {
7593 /* Set up node groups */
7594 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007595 SCHED_CPUMASK_VAR(nodemask, allmasks);
7596 SCHED_CPUMASK_VAR(domainspan, allmasks);
7597 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007598 int j;
7599
Mike Travis7c16ec52008-04-04 18:11:11 -07007600 *nodemask = node_to_cpumask(i);
7601 cpus_clear(*covered);
7602
7603 cpus_and(*nodemask, *nodemask, *cpu_map);
7604 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007605 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007606 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007607 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007608
Mike Travis4bdbaad32008-04-15 16:35:52 -07007609 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007610 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007611
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007612 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007613 if (!sg) {
7614 printk(KERN_WARNING "Can not alloc domain group for "
7615 "node %d\n", i);
7616 goto error;
7617 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007618 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007619 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007620 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007621
John Hawkes9c1cfda2005-09-06 15:18:14 -07007622 sd = &per_cpu(node_domains, j);
7623 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007624 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007625 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007626 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007627 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007628 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007629 prev = sg;
7630
7631 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007632 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007633 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007634 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007635
Mike Travis7c16ec52008-04-04 18:11:11 -07007636 cpus_complement(*notcovered, *covered);
7637 cpus_and(*tmpmask, *notcovered, *cpu_map);
7638 cpus_and(*tmpmask, *tmpmask, *domainspan);
7639 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007640 break;
7641
Mike Travis7c16ec52008-04-04 18:11:11 -07007642 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7643 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007644 continue;
7645
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007646 sg = kmalloc_node(sizeof(struct sched_group),
7647 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007648 if (!sg) {
7649 printk(KERN_WARNING
7650 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007651 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007652 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007653 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007654 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007655 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007656 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007657 prev->next = sg;
7658 prev = sg;
7659 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661#endif
7662
7663 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007664#ifdef CONFIG_SCHED_SMT
7665 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007666 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7667
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007668 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007669 }
7670#endif
7671#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007672 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007673 struct sched_domain *sd = &per_cpu(core_domains, i);
7674
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007675 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007676 }
7677#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007679 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007680 struct sched_domain *sd = &per_cpu(phys_domains, i);
7681
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007682 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683 }
7684
John Hawkes9c1cfda2005-09-06 15:18:14 -07007685#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007686 for (i = 0; i < MAX_NUMNODES; i++)
7687 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007688
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007689 if (sd_allnodes) {
7690 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007691
Mike Travis7c16ec52008-04-04 18:11:11 -07007692 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7693 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007694 init_numa_sched_groups_power(sg);
7695 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007696#endif
7697
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007699 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700 struct sched_domain *sd;
7701#ifdef CONFIG_SCHED_SMT
7702 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007703#elif defined(CONFIG_SCHED_MC)
7704 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705#else
7706 sd = &per_cpu(phys_domains, i);
7707#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007708 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007710
Mike Travis7c16ec52008-04-04 18:11:11 -07007711 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007712 return 0;
7713
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007714#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007715error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007716 free_sched_groups(cpu_map, tmpmask);
7717 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007718 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007719#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720}
Paul Jackson029190c2007-10-18 23:40:20 -07007721
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007722static int build_sched_domains(const cpumask_t *cpu_map)
7723{
7724 return __build_sched_domains(cpu_map, NULL);
7725}
7726
Paul Jackson029190c2007-10-18 23:40:20 -07007727static cpumask_t *doms_cur; /* current sched domains */
7728static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007729static struct sched_domain_attr *dattr_cur; /* attribues of custom domains
7730 in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007731
7732/*
7733 * Special case: If a kmalloc of a doms_cur partition (array of
7734 * cpumask_t) fails, then fallback to a single sched domain,
7735 * as determined by the single cpumask_t fallback_doms.
7736 */
7737static cpumask_t fallback_doms;
7738
Heiko Carstens22e52b02008-03-12 18:31:59 +01007739void __attribute__((weak)) arch_update_cpu_topology(void)
7740{
7741}
7742
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007743/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007744 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007745 * For now this just excludes isolated cpus, but could be used to
7746 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007747 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007748static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007749{
Milton Miller73785472007-10-24 18:23:48 +02007750 int err;
7751
Heiko Carstens22e52b02008-03-12 18:31:59 +01007752 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007753 ndoms_cur = 1;
7754 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7755 if (!doms_cur)
7756 doms_cur = &fallback_doms;
7757 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007758 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007759 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007760 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007761
7762 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007763}
7764
Mike Travis7c16ec52008-04-04 18:11:11 -07007765static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7766 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767{
Mike Travis7c16ec52008-04-04 18:11:11 -07007768 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007771/*
7772 * Detach sched domains from a group of cpus specified in cpu_map
7773 * These cpus will now be attached to the NULL domain
7774 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007775static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007776{
Mike Travis7c16ec52008-04-04 18:11:11 -07007777 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007778 int i;
7779
Milton Miller6382bc92007-10-15 17:00:19 +02007780 unregister_sched_domain_sysctl();
7781
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007782 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007783 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007784 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007785 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007786}
7787
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007788/* handle null as "default" */
7789static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7790 struct sched_domain_attr *new, int idx_new)
7791{
7792 struct sched_domain_attr tmp;
7793
7794 /* fast path */
7795 if (!new && !cur)
7796 return 1;
7797
7798 tmp = SD_ATTR_INIT;
7799 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7800 new ? (new + idx_new) : &tmp,
7801 sizeof(struct sched_domain_attr));
7802}
7803
Paul Jackson029190c2007-10-18 23:40:20 -07007804/*
7805 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007806 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007807 * doms_new[] to the current sched domain partitioning, doms_cur[].
7808 * It destroys each deleted domain and builds each new domain.
7809 *
7810 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007811 * The masks don't intersect (don't overlap.) We should setup one
7812 * sched domain for each mask. CPUs not in any of the cpumasks will
7813 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007814 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7815 * it as it is.
7816 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007817 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7818 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007819 * failed the kmalloc call, then it can pass in doms_new == NULL,
7820 * and partition_sched_domains() will fallback to the single partition
7821 * 'fallback_doms'.
7822 *
7823 * Call with hotplug lock held
7824 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007825void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7826 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007827{
7828 int i, j;
7829
Heiko Carstens712555e2008-04-28 11:33:07 +02007830 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007831
Milton Miller73785472007-10-24 18:23:48 +02007832 /* always unregister in case we don't destroy any domains */
7833 unregister_sched_domain_sysctl();
7834
Paul Jackson029190c2007-10-18 23:40:20 -07007835 if (doms_new == NULL) {
7836 ndoms_new = 1;
7837 doms_new = &fallback_doms;
7838 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007839 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007840 }
7841
7842 /* Destroy deleted domains */
7843 for (i = 0; i < ndoms_cur; i++) {
7844 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007845 if (cpus_equal(doms_cur[i], doms_new[j])
7846 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007847 goto match1;
7848 }
7849 /* no match - a current sched domain not in new doms_new[] */
7850 detach_destroy_domains(doms_cur + i);
7851match1:
7852 ;
7853 }
7854
7855 /* Build new domains */
7856 for (i = 0; i < ndoms_new; i++) {
7857 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007858 if (cpus_equal(doms_new[i], doms_cur[j])
7859 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007860 goto match2;
7861 }
7862 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007863 __build_sched_domains(doms_new + i,
7864 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007865match2:
7866 ;
7867 }
7868
7869 /* Remember the new sched domains */
7870 if (doms_cur != &fallback_doms)
7871 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007872 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007873 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007874 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007875 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007876
7877 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007878
Heiko Carstens712555e2008-04-28 11:33:07 +02007879 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007880}
7881
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007882#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007883int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007884{
7885 int err;
7886
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007887 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007888 mutex_lock(&sched_domains_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007889 detach_destroy_domains(&cpu_online_map);
7890 err = arch_init_sched_domains(&cpu_online_map);
Heiko Carstens712555e2008-04-28 11:33:07 +02007891 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007892 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007893
7894 return err;
7895}
7896
7897static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7898{
7899 int ret;
7900
7901 if (buf[0] != '0' && buf[0] != '1')
7902 return -EINVAL;
7903
7904 if (smt)
7905 sched_smt_power_savings = (buf[0] == '1');
7906 else
7907 sched_mc_power_savings = (buf[0] == '1');
7908
7909 ret = arch_reinit_sched_domains();
7910
7911 return ret ? ret : count;
7912}
7913
Adrian Bunk6707de002007-08-12 18:08:19 +02007914#ifdef CONFIG_SCHED_MC
7915static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7916{
7917 return sprintf(page, "%u\n", sched_mc_power_savings);
7918}
7919static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7920 const char *buf, size_t count)
7921{
7922 return sched_power_savings_store(buf, count, 0);
7923}
7924static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7925 sched_mc_power_savings_store);
7926#endif
7927
7928#ifdef CONFIG_SCHED_SMT
7929static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7930{
7931 return sprintf(page, "%u\n", sched_smt_power_savings);
7932}
7933static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7934 const char *buf, size_t count)
7935{
7936 return sched_power_savings_store(buf, count, 1);
7937}
7938static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7939 sched_smt_power_savings_store);
7940#endif
7941
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007942int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7943{
7944 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007945
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007946#ifdef CONFIG_SCHED_SMT
7947 if (smt_capable())
7948 err = sysfs_create_file(&cls->kset.kobj,
7949 &attr_sched_smt_power_savings.attr);
7950#endif
7951#ifdef CONFIG_SCHED_MC
7952 if (!err && mc_capable())
7953 err = sysfs_create_file(&cls->kset.kobj,
7954 &attr_sched_mc_power_savings.attr);
7955#endif
7956 return err;
7957}
7958#endif
7959
Linus Torvalds1da177e2005-04-16 15:20:36 -07007960/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007961 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007963 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964 * which will prevent rebalancing while the sched domains are recalculated.
7965 */
7966static int update_sched_domains(struct notifier_block *nfb,
7967 unsigned long action, void *hcpu)
7968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969 switch (action) {
7970 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007971 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007973 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007974 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007975 return NOTIFY_OK;
7976
7977 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007978 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007980 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007982 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007984 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 /*
7986 * Fall through and re-initialise the domains.
7987 */
7988 break;
7989 default:
7990 return NOTIFY_DONE;
7991 }
7992
7993 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007994 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995
7996 return NOTIFY_OK;
7997}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998
7999void __init sched_init_smp(void)
8000{
Nick Piggin5c1e1762006-10-03 01:14:04 -07008001 cpumask_t non_isolated_cpus;
8002
Mike Travis434d53b2008-04-04 18:11:04 -07008003#if defined(CONFIG_NUMA)
8004 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
8005 GFP_KERNEL);
8006 BUG_ON(sched_group_nodes_bycpu == NULL);
8007#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008008 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02008009 mutex_lock(&sched_domains_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008010 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08008011 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07008012 if (cpus_empty(non_isolated_cpus))
8013 cpu_set(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02008014 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008015 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008016 /* XXX: Theoretical race here - CPU may be hotplugged now */
8017 hotcpu_notifier(update_sched_domains, 0);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02008018 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07008019
8020 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07008021 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07008022 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01008023 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024}
8025#else
8026void __init sched_init_smp(void)
8027{
Ingo Molnar19978ca2007-11-09 22:39:38 +01008028 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029}
8030#endif /* CONFIG_SMP */
8031
8032int in_sched_functions(unsigned long addr)
8033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034 return in_lock_functions(addr) ||
8035 (addr >= (unsigned long)__sched_text_start
8036 && addr < (unsigned long)__sched_text_end);
8037}
8038
Alexey Dobriyana9957442007-10-15 17:00:13 +02008039static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02008040{
8041 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02008042 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02008043#ifdef CONFIG_FAIR_GROUP_SCHED
8044 cfs_rq->rq = rq;
8045#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02008046 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02008047}
8048
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008049static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
8050{
8051 struct rt_prio_array *array;
8052 int i;
8053
8054 array = &rt_rq->active;
8055 for (i = 0; i < MAX_RT_PRIO; i++) {
8056 INIT_LIST_HEAD(array->queue + i);
8057 __clear_bit(i, array->bitmap);
8058 }
8059 /* delimiter for bitsearch: */
8060 __set_bit(MAX_RT_PRIO, array->bitmap);
8061
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008062#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01008063 rt_rq->highest_prio = MAX_RT_PRIO;
8064#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008065#ifdef CONFIG_SMP
8066 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008067 rt_rq->overloaded = 0;
8068#endif
8069
8070 rt_rq->rt_time = 0;
8071 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008072 rt_rq->rt_runtime = 0;
8073 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008074
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008075#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01008076 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008077 rt_rq->rq = rq;
8078#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008079}
8080
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008081#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008082static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
8083 struct sched_entity *se, int cpu, int add,
8084 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008085{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008086 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008087 tg->cfs_rq[cpu] = cfs_rq;
8088 init_cfs_rq(cfs_rq, rq);
8089 cfs_rq->tg = tg;
8090 if (add)
8091 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
8092
8093 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008094 /* se could be NULL for init_task_group */
8095 if (!se)
8096 return;
8097
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008098 if (!parent)
8099 se->cfs_rq = &rq->cfs;
8100 else
8101 se->cfs_rq = parent->my_q;
8102
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008103 se->my_q = cfs_rq;
8104 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008105 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008106 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008107}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008108#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008109
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008110#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008111static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8112 struct sched_rt_entity *rt_se, int cpu, int add,
8113 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008114{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008115 struct rq *rq = cpu_rq(cpu);
8116
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008117 tg->rt_rq[cpu] = rt_rq;
8118 init_rt_rq(rt_rq, rq);
8119 rt_rq->tg = tg;
8120 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008121 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008122 if (add)
8123 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8124
8125 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008126 if (!rt_se)
8127 return;
8128
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008129 if (!parent)
8130 rt_se->rt_rq = &rq->rt;
8131 else
8132 rt_se->rt_rq = parent->my_q;
8133
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008134 rt_se->rt_rq = &rq->rt;
8135 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008136 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008137 INIT_LIST_HEAD(&rt_se->run_list);
8138}
8139#endif
8140
Linus Torvalds1da177e2005-04-16 15:20:36 -07008141void __init sched_init(void)
8142{
Ingo Molnardd41f592007-07-09 18:51:59 +02008143 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008144 unsigned long alloc_size = 0, ptr;
8145
8146#ifdef CONFIG_FAIR_GROUP_SCHED
8147 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8148#endif
8149#ifdef CONFIG_RT_GROUP_SCHED
8150 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8151#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008152#ifdef CONFIG_USER_SCHED
8153 alloc_size *= 2;
8154#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008155 /*
8156 * As sched_init() is called before page_alloc is setup,
8157 * we use alloc_bootmem().
8158 */
8159 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07008160 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07008161
8162#ifdef CONFIG_FAIR_GROUP_SCHED
8163 init_task_group.se = (struct sched_entity **)ptr;
8164 ptr += nr_cpu_ids * sizeof(void **);
8165
8166 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8167 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008168
8169#ifdef CONFIG_USER_SCHED
8170 root_task_group.se = (struct sched_entity **)ptr;
8171 ptr += nr_cpu_ids * sizeof(void **);
8172
8173 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8174 ptr += nr_cpu_ids * sizeof(void **);
8175#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008176#endif
8177#ifdef CONFIG_RT_GROUP_SCHED
8178 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8179 ptr += nr_cpu_ids * sizeof(void **);
8180
8181 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008182 ptr += nr_cpu_ids * sizeof(void **);
8183
8184#ifdef CONFIG_USER_SCHED
8185 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8186 ptr += nr_cpu_ids * sizeof(void **);
8187
8188 root_task_group.rt_rq = (struct rt_rq **)ptr;
8189 ptr += nr_cpu_ids * sizeof(void **);
8190#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008191#endif
8192 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008193
Gregory Haskins57d885f2008-01-25 21:08:18 +01008194#ifdef CONFIG_SMP
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008195 init_aggregate();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008196 init_defrootdomain();
8197#endif
8198
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008199 init_rt_bandwidth(&def_rt_bandwidth,
8200 global_rt_period(), global_rt_runtime());
8201
8202#ifdef CONFIG_RT_GROUP_SCHED
8203 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8204 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008205#ifdef CONFIG_USER_SCHED
8206 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8207 global_rt_period(), RUNTIME_INF);
8208#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008209#endif
8210
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008211#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008212 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008213 INIT_LIST_HEAD(&init_task_group.children);
8214
8215#ifdef CONFIG_USER_SCHED
8216 INIT_LIST_HEAD(&root_task_group.children);
8217 init_task_group.parent = &root_task_group;
8218 list_add(&init_task_group.siblings, &root_task_group.children);
8219#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008220#endif
8221
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008222 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008223 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224
8225 rq = cpu_rq(i);
8226 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07008227 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07008228 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008229 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02008230 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02008231 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008232 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008233#ifdef CONFIG_FAIR_GROUP_SCHED
8234 init_task_group.shares = init_task_group_load;
8235 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008236#ifdef CONFIG_CGROUP_SCHED
8237 /*
8238 * How much cpu bandwidth does init_task_group get?
8239 *
8240 * In case of task-groups formed thr' the cgroup filesystem, it
8241 * gets 100% of the cpu resources in the system. This overall
8242 * system cpu resource is divided among the tasks of
8243 * init_task_group and its child task-groups in a fair manner,
8244 * based on each entity's (task or task-group's) weight
8245 * (se->load.weight).
8246 *
8247 * In other words, if init_task_group has 10 tasks of weight
8248 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8249 * then A0's share of the cpu resource is:
8250 *
8251 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8252 *
8253 * We achieve this by letting init_task_group's tasks sit
8254 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8255 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008256 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008257#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008258 root_task_group.shares = NICE_0_LOAD;
8259 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008260 /*
8261 * In case of task-groups formed thr' the user id of tasks,
8262 * init_task_group represents tasks belonging to root user.
8263 * Hence it forms a sibling of all subsequent groups formed.
8264 * In this case, init_task_group gets only a fraction of overall
8265 * system cpu resource, based on the weight assigned to root
8266 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8267 * by letting tasks of init_task_group sit in a separate cfs_rq
8268 * (init_cfs_rq) and having one entity represent this group of
8269 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8270 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008271 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008272 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008273 &per_cpu(init_sched_entity, i), i, 1,
8274 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008275
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008276#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008277#endif /* CONFIG_FAIR_GROUP_SCHED */
8278
8279 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008280#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008281 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008282#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008283 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008284#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008285 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008286 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008287 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008288 &per_cpu(init_sched_rt_entity, i), i, 1,
8289 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008290#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292
Ingo Molnardd41f592007-07-09 18:51:59 +02008293 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8294 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008296 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008297 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008299 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008301 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302 rq->migration_thread = NULL;
8303 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008304 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008306 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008307 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008308 }
8309
Peter Williams2dd73a42006-06-27 02:54:34 -07008310 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008311
Avi Kivitye107be32007-07-26 13:40:43 +02008312#ifdef CONFIG_PREEMPT_NOTIFIERS
8313 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8314#endif
8315
Christoph Lameterc9819f42006-12-10 02:20:25 -08008316#ifdef CONFIG_SMP
8317 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8318#endif
8319
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008320#ifdef CONFIG_RT_MUTEXES
8321 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8322#endif
8323
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324 /*
8325 * The boot idle thread does lazy MMU switching as well:
8326 */
8327 atomic_inc(&init_mm.mm_count);
8328 enter_lazy_tlb(&init_mm, current);
8329
8330 /*
8331 * Make us the idle thread. Technically, schedule() should not be
8332 * called from this thread, however somewhere below it might be,
8333 * but because we are the idle thread, we just pick up running again
8334 * when this runqueue becomes "idle".
8335 */
8336 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008337 /*
8338 * During early bootup we pretend to be a normal task:
8339 */
8340 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008341
8342 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343}
8344
8345#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8346void __might_sleep(char *file, int line)
8347{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008348#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008349 static unsigned long prev_jiffy; /* ratelimiting */
8350
8351 if ((in_atomic() || irqs_disabled()) &&
8352 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8353 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8354 return;
8355 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008356 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357 " context at %s:%d\n", file, line);
8358 printk("in_atomic():%d, irqs_disabled():%d\n",
8359 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008360 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008361 if (irqs_disabled())
8362 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008363 dump_stack();
8364 }
8365#endif
8366}
8367EXPORT_SYMBOL(__might_sleep);
8368#endif
8369
8370#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008371static void normalize_task(struct rq *rq, struct task_struct *p)
8372{
8373 int on_rq;
8374 update_rq_clock(rq);
8375 on_rq = p->se.on_rq;
8376 if (on_rq)
8377 deactivate_task(rq, p, 0);
8378 __setscheduler(rq, p, SCHED_NORMAL, 0);
8379 if (on_rq) {
8380 activate_task(rq, p, 0);
8381 resched_task(rq->curr);
8382 }
8383}
8384
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385void normalize_rt_tasks(void)
8386{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008387 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008389 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008391 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008392 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008393 /*
8394 * Only normalize user tasks:
8395 */
8396 if (!p->mm)
8397 continue;
8398
Ingo Molnardd41f592007-07-09 18:51:59 +02008399 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008400#ifdef CONFIG_SCHEDSTATS
8401 p->se.wait_start = 0;
8402 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008403 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008404#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008405 task_rq(p)->clock = 0;
8406
8407 if (!rt_task(p)) {
8408 /*
8409 * Renice negative nice level userspace
8410 * tasks back to 0:
8411 */
8412 if (TASK_NICE(p) < 0 && p->mm)
8413 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008417 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008418 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419
Ingo Molnar178be792007-10-15 17:00:18 +02008420 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008421
Ingo Molnarb29739f2006-06-27 02:54:51 -07008422 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008423 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008424 } while_each_thread(g, p);
8425
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008426 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008427}
8428
8429#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008430
8431#ifdef CONFIG_IA64
8432/*
8433 * These functions are only useful for the IA64 MCA handling.
8434 *
8435 * They can only be called when the whole system has been
8436 * stopped - every CPU needs to be quiescent, and no scheduling
8437 * activity can take place. Using them for anything else would
8438 * be a serious bug, and as a result, they aren't even visible
8439 * under any other configuration.
8440 */
8441
8442/**
8443 * curr_task - return the current task for a given cpu.
8444 * @cpu: the processor in question.
8445 *
8446 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8447 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008448struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008449{
8450 return cpu_curr(cpu);
8451}
8452
8453/**
8454 * set_curr_task - set the current task for a given cpu.
8455 * @cpu: the processor in question.
8456 * @p: the task pointer to set.
8457 *
8458 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008459 * are serviced on a separate stack. It allows the architecture to switch the
8460 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008461 * must be called with all CPU's synchronized, and interrupts disabled, the
8462 * and caller must save the original value of the current task (see
8463 * curr_task() above) and restore that value before reenabling interrupts and
8464 * re-starting the system.
8465 *
8466 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8467 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008468void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008469{
8470 cpu_curr(cpu) = p;
8471}
8472
8473#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008474
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008475#ifdef CONFIG_FAIR_GROUP_SCHED
8476static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008477{
8478 int i;
8479
8480 for_each_possible_cpu(i) {
8481 if (tg->cfs_rq)
8482 kfree(tg->cfs_rq[i]);
8483 if (tg->se)
8484 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008485 }
8486
8487 kfree(tg->cfs_rq);
8488 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008489}
8490
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008491static
8492int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008493{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008494 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008495 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008496 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008497 int i;
8498
Mike Travis434d53b2008-04-04 18:11:04 -07008499 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008500 if (!tg->cfs_rq)
8501 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008502 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008503 if (!tg->se)
8504 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008505
8506 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008507
8508 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008509 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008510
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008511 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8512 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008513 if (!cfs_rq)
8514 goto err;
8515
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008516 se = kmalloc_node(sizeof(struct sched_entity),
8517 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008518 if (!se)
8519 goto err;
8520
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008521 parent_se = parent ? parent->se[i] : NULL;
8522 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008523 }
8524
8525 return 1;
8526
8527 err:
8528 return 0;
8529}
8530
8531static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8532{
8533 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8534 &cpu_rq(cpu)->leaf_cfs_rq_list);
8535}
8536
8537static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8538{
8539 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8540}
8541#else
8542static inline void free_fair_sched_group(struct task_group *tg)
8543{
8544}
8545
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008546static inline
8547int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008548{
8549 return 1;
8550}
8551
8552static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8553{
8554}
8555
8556static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8557{
8558}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008559#endif
8560
8561#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008562static void free_rt_sched_group(struct task_group *tg)
8563{
8564 int i;
8565
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008566 destroy_rt_bandwidth(&tg->rt_bandwidth);
8567
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008568 for_each_possible_cpu(i) {
8569 if (tg->rt_rq)
8570 kfree(tg->rt_rq[i]);
8571 if (tg->rt_se)
8572 kfree(tg->rt_se[i]);
8573 }
8574
8575 kfree(tg->rt_rq);
8576 kfree(tg->rt_se);
8577}
8578
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008579static
8580int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008581{
8582 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008583 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008584 struct rq *rq;
8585 int i;
8586
Mike Travis434d53b2008-04-04 18:11:04 -07008587 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008588 if (!tg->rt_rq)
8589 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008590 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008591 if (!tg->rt_se)
8592 goto err;
8593
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008594 init_rt_bandwidth(&tg->rt_bandwidth,
8595 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008596
8597 for_each_possible_cpu(i) {
8598 rq = cpu_rq(i);
8599
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008600 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8601 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8602 if (!rt_rq)
8603 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008604
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008605 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8606 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8607 if (!rt_se)
8608 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008609
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008610 parent_se = parent ? parent->rt_se[i] : NULL;
8611 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008612 }
8613
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008614 return 1;
8615
8616 err:
8617 return 0;
8618}
8619
8620static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8621{
8622 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8623 &cpu_rq(cpu)->leaf_rt_rq_list);
8624}
8625
8626static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8627{
8628 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8629}
8630#else
8631static inline void free_rt_sched_group(struct task_group *tg)
8632{
8633}
8634
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008635static inline
8636int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008637{
8638 return 1;
8639}
8640
8641static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8642{
8643}
8644
8645static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8646{
8647}
8648#endif
8649
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008650#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008651static void free_sched_group(struct task_group *tg)
8652{
8653 free_fair_sched_group(tg);
8654 free_rt_sched_group(tg);
8655 kfree(tg);
8656}
8657
8658/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008659struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008660{
8661 struct task_group *tg;
8662 unsigned long flags;
8663 int i;
8664
8665 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8666 if (!tg)
8667 return ERR_PTR(-ENOMEM);
8668
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008669 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008670 goto err;
8671
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008672 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008673 goto err;
8674
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008675 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008676 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008677 register_fair_sched_group(tg, i);
8678 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008679 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008680 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008681
8682 WARN_ON(!parent); /* root should already exist */
8683
8684 tg->parent = parent;
8685 list_add_rcu(&tg->siblings, &parent->children);
8686 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008687 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008688
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008689 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008690
8691err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008692 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008693 return ERR_PTR(-ENOMEM);
8694}
8695
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008696/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008697static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008698{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008699 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008700 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008701}
8702
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008703/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008704void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008705{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008706 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008707 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008708
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008709 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008710 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008711 unregister_fair_sched_group(tg, i);
8712 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008713 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008714 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008715 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008716 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008717
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008718 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008719 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008720}
8721
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008722/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008723 * The caller of this function should have put the task in its new group
8724 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8725 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008726 */
8727void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008728{
8729 int on_rq, running;
8730 unsigned long flags;
8731 struct rq *rq;
8732
8733 rq = task_rq_lock(tsk, &flags);
8734
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008735 update_rq_clock(rq);
8736
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008737 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008738 on_rq = tsk->se.on_rq;
8739
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008740 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008741 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008742 if (unlikely(running))
8743 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008744
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008745 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008746
Peter Zijlstra810b3812008-02-29 15:21:01 -05008747#ifdef CONFIG_FAIR_GROUP_SCHED
8748 if (tsk->sched_class->moved_group)
8749 tsk->sched_class->moved_group(tsk);
8750#endif
8751
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008752 if (unlikely(running))
8753 tsk->sched_class->set_curr_task(rq);
8754 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008755 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008756
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008757 task_rq_unlock(rq, &flags);
8758}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008759#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008760
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008761#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008762static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008763{
8764 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008765 int on_rq;
8766
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008767 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008768 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008769 dequeue_entity(cfs_rq, se, 0);
8770
8771 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008772 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008773
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008774 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008775 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008776}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008777
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008778static void set_se_shares(struct sched_entity *se, unsigned long shares)
8779{
8780 struct cfs_rq *cfs_rq = se->cfs_rq;
8781 struct rq *rq = cfs_rq->rq;
8782 unsigned long flags;
8783
8784 spin_lock_irqsave(&rq->lock, flags);
8785 __set_se_shares(se, shares);
8786 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008787}
8788
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008789static DEFINE_MUTEX(shares_mutex);
8790
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008791int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008792{
8793 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008794 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008795
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008796 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008797 * We can't change the weight of the root cgroup.
8798 */
8799 if (!tg->se[0])
8800 return -EINVAL;
8801
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008802 if (shares < MIN_SHARES)
8803 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008804 else if (shares > MAX_SHARES)
8805 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008806
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008807 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008808 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008809 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008810
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008811 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008812 for_each_possible_cpu(i)
8813 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008814 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008815 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008816
8817 /* wait for any ongoing reference to this group to finish */
8818 synchronize_sched();
8819
8820 /*
8821 * Now we are free to modify the group's share on each cpu
8822 * w/o tripping rebalance_share or load_balance_fair.
8823 */
8824 tg->shares = shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008825 for_each_possible_cpu(i) {
8826 /*
8827 * force a rebalance
8828 */
8829 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008830 set_se_shares(tg->se[i], shares);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008831 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008832
8833 /*
8834 * Enable load balance activity on this group, by inserting it back on
8835 * each cpu's rq->leaf_cfs_rq_list.
8836 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008837 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008838 for_each_possible_cpu(i)
8839 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008840 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008841 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008842done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008843 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008844 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008845}
8846
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008847unsigned long sched_group_shares(struct task_group *tg)
8848{
8849 return tg->shares;
8850}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008851#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008852
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008853#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008854/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008855 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008856 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008857static DEFINE_MUTEX(rt_constraints_mutex);
8858
8859static unsigned long to_ratio(u64 period, u64 runtime)
8860{
8861 if (runtime == RUNTIME_INF)
8862 return 1ULL << 16;
8863
Roman Zippel6f6d6a12008-05-01 04:34:28 -07008864 return div64_u64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008865}
8866
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008867#ifdef CONFIG_CGROUP_SCHED
8868static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8869{
8870 struct task_group *tgi, *parent = tg->parent;
8871 unsigned long total = 0;
8872
8873 if (!parent) {
8874 if (global_rt_period() < period)
8875 return 0;
8876
8877 return to_ratio(period, runtime) <
8878 to_ratio(global_rt_period(), global_rt_runtime());
8879 }
8880
8881 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8882 return 0;
8883
8884 rcu_read_lock();
8885 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8886 if (tgi == tg)
8887 continue;
8888
8889 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8890 tgi->rt_bandwidth.rt_runtime);
8891 }
8892 rcu_read_unlock();
8893
8894 return total + to_ratio(period, runtime) <
8895 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8896 parent->rt_bandwidth.rt_runtime);
8897}
8898#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008899static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008900{
8901 struct task_group *tgi;
8902 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008903 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008904 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008905
8906 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008907 list_for_each_entry_rcu(tgi, &task_groups, list) {
8908 if (tgi == tg)
8909 continue;
8910
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008911 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8912 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008913 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008914 rcu_read_unlock();
8915
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008916 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008917}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008918#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008919
Dhaval Giani521f1a242008-02-28 15:21:56 +05308920/* Must be called with tasklist_lock held */
8921static inline int tg_has_rt_tasks(struct task_group *tg)
8922{
8923 struct task_struct *g, *p;
8924 do_each_thread(g, p) {
8925 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8926 return 1;
8927 } while_each_thread(g, p);
8928 return 0;
8929}
8930
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008931static int tg_set_bandwidth(struct task_group *tg,
8932 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008933{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008934 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008935
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008936 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308937 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008938 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308939 err = -EBUSY;
8940 goto unlock;
8941 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008942 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8943 err = -EINVAL;
8944 goto unlock;
8945 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008946
8947 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008948 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8949 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008950
8951 for_each_possible_cpu(i) {
8952 struct rt_rq *rt_rq = tg->rt_rq[i];
8953
8954 spin_lock(&rt_rq->rt_runtime_lock);
8955 rt_rq->rt_runtime = rt_runtime;
8956 spin_unlock(&rt_rq->rt_runtime_lock);
8957 }
8958 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008959 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308960 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008961 mutex_unlock(&rt_constraints_mutex);
8962
8963 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008964}
8965
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008966int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8967{
8968 u64 rt_runtime, rt_period;
8969
8970 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8971 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8972 if (rt_runtime_us < 0)
8973 rt_runtime = RUNTIME_INF;
8974
8975 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8976}
8977
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008978long sched_group_rt_runtime(struct task_group *tg)
8979{
8980 u64 rt_runtime_us;
8981
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008982 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008983 return -1;
8984
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008985 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008986 do_div(rt_runtime_us, NSEC_PER_USEC);
8987 return rt_runtime_us;
8988}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008989
8990int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8991{
8992 u64 rt_runtime, rt_period;
8993
8994 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8995 rt_runtime = tg->rt_bandwidth.rt_runtime;
8996
8997 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8998}
8999
9000long sched_group_rt_period(struct task_group *tg)
9001{
9002 u64 rt_period_us;
9003
9004 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
9005 do_div(rt_period_us, NSEC_PER_USEC);
9006 return rt_period_us;
9007}
9008
9009static int sched_rt_global_constraints(void)
9010{
9011 int ret = 0;
9012
9013 mutex_lock(&rt_constraints_mutex);
9014 if (!__rt_schedulable(NULL, 1, 0))
9015 ret = -EINVAL;
9016 mutex_unlock(&rt_constraints_mutex);
9017
9018 return ret;
9019}
9020#else
9021static int sched_rt_global_constraints(void)
9022{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009023 unsigned long flags;
9024 int i;
9025
9026 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
9027 for_each_possible_cpu(i) {
9028 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
9029
9030 spin_lock(&rt_rq->rt_runtime_lock);
9031 rt_rq->rt_runtime = global_rt_runtime();
9032 spin_unlock(&rt_rq->rt_runtime_lock);
9033 }
9034 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
9035
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009036 return 0;
9037}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009038#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009039
9040int sched_rt_handler(struct ctl_table *table, int write,
9041 struct file *filp, void __user *buffer, size_t *lenp,
9042 loff_t *ppos)
9043{
9044 int ret;
9045 int old_period, old_runtime;
9046 static DEFINE_MUTEX(mutex);
9047
9048 mutex_lock(&mutex);
9049 old_period = sysctl_sched_rt_period;
9050 old_runtime = sysctl_sched_rt_runtime;
9051
9052 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
9053
9054 if (!ret && write) {
9055 ret = sched_rt_global_constraints();
9056 if (ret) {
9057 sysctl_sched_rt_period = old_period;
9058 sysctl_sched_rt_runtime = old_runtime;
9059 } else {
9060 def_rt_bandwidth.rt_runtime = global_rt_runtime();
9061 def_rt_bandwidth.rt_period =
9062 ns_to_ktime(global_rt_period());
9063 }
9064 }
9065 mutex_unlock(&mutex);
9066
9067 return ret;
9068}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009069
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009070#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009071
9072/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009073static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009074{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009075 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
9076 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009077}
9078
9079static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02009080cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009081{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009082 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009083
Paul Menage2b01dfe2007-10-24 18:23:50 +02009084 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009085 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009086 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009087 return &init_task_group.css;
9088 }
9089
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009090 parent = cgroup_tg(cgrp->parent);
9091 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009092 if (IS_ERR(tg))
9093 return ERR_PTR(-ENOMEM);
9094
9095 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009096 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009097
9098 return &tg->css;
9099}
9100
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009101static void
9102cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009103{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009104 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009105
9106 sched_destroy_group(tg);
9107}
9108
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009109static int
9110cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9111 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009112{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009113#ifdef CONFIG_RT_GROUP_SCHED
9114 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009115 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009116 return -EINVAL;
9117#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009118 /* We don't support RT-tasks being in separate groups */
9119 if (tsk->sched_class != &fair_sched_class)
9120 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009121#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009122
9123 return 0;
9124}
9125
9126static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009127cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009128 struct cgroup *old_cont, struct task_struct *tsk)
9129{
9130 sched_move_task(tsk);
9131}
9132
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009133#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009134static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009135 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009136{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009137 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009138}
9139
Paul Menagef4c753b2008-04-29 00:59:56 -07009140static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009141{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009142 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009143
9144 return (u64) tg->shares;
9145}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009146#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009147
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009148#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009149static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009150 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009151{
Paul Menage06ecb272008-04-29 01:00:06 -07009152 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009153}
9154
Paul Menage06ecb272008-04-29 01:00:06 -07009155static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009156{
Paul Menage06ecb272008-04-29 01:00:06 -07009157 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009158}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009159
9160static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9161 u64 rt_period_us)
9162{
9163 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9164}
9165
9166static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9167{
9168 return sched_group_rt_period(cgroup_tg(cgrp));
9169}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009170#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009171
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009172static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009173#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009174 {
9175 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009176 .read_u64 = cpu_shares_read_u64,
9177 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009178 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009179#endif
9180#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009181 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009182 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009183 .read_s64 = cpu_rt_runtime_read,
9184 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009185 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009186 {
9187 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009188 .read_u64 = cpu_rt_period_read_uint,
9189 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009190 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009191#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009192};
9193
9194static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9195{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009196 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009197}
9198
9199struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009200 .name = "cpu",
9201 .create = cpu_cgroup_create,
9202 .destroy = cpu_cgroup_destroy,
9203 .can_attach = cpu_cgroup_can_attach,
9204 .attach = cpu_cgroup_attach,
9205 .populate = cpu_cgroup_populate,
9206 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009207 .early_init = 1,
9208};
9209
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009210#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009211
9212#ifdef CONFIG_CGROUP_CPUACCT
9213
9214/*
9215 * CPU accounting code for task groups.
9216 *
9217 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9218 * (balbir@in.ibm.com).
9219 */
9220
9221/* track cpu usage of a group of tasks */
9222struct cpuacct {
9223 struct cgroup_subsys_state css;
9224 /* cpuusage holds pointer to a u64-type object on every cpu */
9225 u64 *cpuusage;
9226};
9227
9228struct cgroup_subsys cpuacct_subsys;
9229
9230/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309231static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009232{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309233 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009234 struct cpuacct, css);
9235}
9236
9237/* return cpu accounting group to which this task belongs */
9238static inline struct cpuacct *task_ca(struct task_struct *tsk)
9239{
9240 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9241 struct cpuacct, css);
9242}
9243
9244/* create a new cpu accounting group */
9245static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309246 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009247{
9248 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9249
9250 if (!ca)
9251 return ERR_PTR(-ENOMEM);
9252
9253 ca->cpuusage = alloc_percpu(u64);
9254 if (!ca->cpuusage) {
9255 kfree(ca);
9256 return ERR_PTR(-ENOMEM);
9257 }
9258
9259 return &ca->css;
9260}
9261
9262/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009263static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309264cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009265{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309266 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009267
9268 free_percpu(ca->cpuusage);
9269 kfree(ca);
9270}
9271
9272/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309273static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009274{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309275 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009276 u64 totalcpuusage = 0;
9277 int i;
9278
9279 for_each_possible_cpu(i) {
9280 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9281
9282 /*
9283 * Take rq->lock to make 64-bit addition safe on 32-bit
9284 * platforms.
9285 */
9286 spin_lock_irq(&cpu_rq(i)->lock);
9287 totalcpuusage += *cpuusage;
9288 spin_unlock_irq(&cpu_rq(i)->lock);
9289 }
9290
9291 return totalcpuusage;
9292}
9293
Dhaval Giani0297b802008-02-29 10:02:44 +05309294static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9295 u64 reset)
9296{
9297 struct cpuacct *ca = cgroup_ca(cgrp);
9298 int err = 0;
9299 int i;
9300
9301 if (reset) {
9302 err = -EINVAL;
9303 goto out;
9304 }
9305
9306 for_each_possible_cpu(i) {
9307 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9308
9309 spin_lock_irq(&cpu_rq(i)->lock);
9310 *cpuusage = 0;
9311 spin_unlock_irq(&cpu_rq(i)->lock);
9312 }
9313out:
9314 return err;
9315}
9316
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009317static struct cftype files[] = {
9318 {
9319 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009320 .read_u64 = cpuusage_read,
9321 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009322 },
9323};
9324
Dhaval Giani32cd7562008-02-29 10:02:43 +05309325static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009326{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309327 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009328}
9329
9330/*
9331 * charge this task's execution time to its accounting group.
9332 *
9333 * called with rq->lock held.
9334 */
9335static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9336{
9337 struct cpuacct *ca;
9338
9339 if (!cpuacct_subsys.active)
9340 return;
9341
9342 ca = task_ca(tsk);
9343 if (ca) {
9344 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9345
9346 *cpuusage += cputime;
9347 }
9348}
9349
9350struct cgroup_subsys cpuacct_subsys = {
9351 .name = "cpuacct",
9352 .create = cpuacct_create,
9353 .destroy = cpuacct_destroy,
9354 .populate = cpuacct_populate,
9355 .subsys_id = cpuacct_subsys_id,
9356};
9357#endif /* CONFIG_CGROUP_CPUACCT */