blob: f98f75f3c708414d14d60f49a6cc81a5c79d25e3 [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 Molnar62160e32007-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 Haskins73fe6aa2008-01-25 21:08:07 +0100459 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-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
913static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
914{
915 unsigned long flags;
916
917 spin_lock_irqsave(&time_sync_lock, flags);
918
919 if (time < prev_global_time) {
920 per_cpu(time_offset, cpu) += prev_global_time - time;
921 time = prev_global_time;
922 } else {
923 prev_global_time = time;
924 }
925
926 spin_unlock_irqrestore(&time_sync_lock, flags);
927
928 return time;
929}
930
931static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200932{
Ingo Molnare436d802007-07-19 21:28:35 +0200933 unsigned long long now;
934 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200935 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200936
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100937 /*
938 * Only call sched_clock() if the scheduler has already been
939 * initialized (some code might call cpu_clock() very early):
940 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100941 if (unlikely(!scheduler_running))
942 return 0;
943
944 local_irq_save(flags);
945 rq = cpu_rq(cpu);
946 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200947 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200948 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200949
950 return now;
951}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100952
953/*
954 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
955 * clock constructed from sched_clock():
956 */
957unsigned long long cpu_clock(int cpu)
958{
959 unsigned long long prev_cpu_time, time, delta_time;
960
961 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
962 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
963 delta_time = time-prev_cpu_time;
964
965 if (unlikely(delta_time > time_sync_thresh))
966 time = __sync_cpu_clock(time, cpu);
967
968 return time;
969}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200970EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700973# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700975#ifndef finish_arch_switch
976# define finish_arch_switch(prev) do { } while (0)
977#endif
978
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100979static inline int task_current(struct rq *rq, struct task_struct *p)
980{
981 return rq->curr == p;
982}
983
Nick Piggin4866cde2005-06-25 14:57:23 -0700984#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700985static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700986{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100987 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700988}
989
Ingo Molnar70b97a72006-07-03 00:25:42 -0700990static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700991{
992}
993
Ingo Molnar70b97a72006-07-03 00:25:42 -0700994static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700995{
Ingo Molnarda04c032005-09-13 11:17:59 +0200996#ifdef CONFIG_DEBUG_SPINLOCK
997 /* this is a valid case when another task releases the spinlock */
998 rq->lock.owner = current;
999#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07001000 /*
1001 * If we are tracking spinlock dependencies then we have to
1002 * fix up the runqueue lock - which gets 'carried over' from
1003 * prev into current:
1004 */
1005 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1006
Nick Piggin4866cde2005-06-25 14:57:23 -07001007 spin_unlock_irq(&rq->lock);
1008}
1009
1010#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001011static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -07001012{
1013#ifdef CONFIG_SMP
1014 return p->oncpu;
1015#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01001016 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -07001017#endif
1018}
1019
Ingo Molnar70b97a72006-07-03 00:25:42 -07001020static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07001021{
1022#ifdef CONFIG_SMP
1023 /*
1024 * We can optimise this out completely for !SMP, because the
1025 * SMP rebalancing from interrupt is the only thing that cares
1026 * here.
1027 */
1028 next->oncpu = 1;
1029#endif
1030#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1031 spin_unlock_irq(&rq->lock);
1032#else
1033 spin_unlock(&rq->lock);
1034#endif
1035}
1036
Ingo Molnar70b97a72006-07-03 00:25:42 -07001037static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -07001038{
1039#ifdef CONFIG_SMP
1040 /*
1041 * After ->oncpu is cleared, the task can be moved to a different CPU.
1042 * We must ensure this doesn't happen until the switch is completely
1043 * finished.
1044 */
1045 smp_wmb();
1046 prev->oncpu = 0;
1047#endif
1048#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1049 local_irq_enable();
1050#endif
1051}
1052#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001055 * __task_rq_lock - lock the runqueue a given task resides on.
1056 * Must be called interrupts disabled.
1057 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001058static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001059 __acquires(rq->lock)
1060{
Andi Kleen3a5c3592007-10-15 17:00:14 +02001061 for (;;) {
1062 struct rq *rq = task_rq(p);
1063 spin_lock(&rq->lock);
1064 if (likely(rq == task_rq(p)))
1065 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -07001066 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001067 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07001068}
1069
1070/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001072 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * explicitly disabling preemption.
1074 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001075static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 __acquires(rq->lock)
1077{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001078 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Andi Kleen3a5c3592007-10-15 17:00:14 +02001080 for (;;) {
1081 local_irq_save(*flags);
1082 rq = task_rq(p);
1083 spin_lock(&rq->lock);
1084 if (likely(rq == task_rq(p)))
1085 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Alexey Dobriyana9957442007-10-15 17:00:13 +02001090static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001091 __releases(rq->lock)
1092{
1093 spin_unlock(&rq->lock);
1094}
1095
Ingo Molnar70b97a72006-07-03 00:25:42 -07001096static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 __releases(rq->lock)
1098{
1099 spin_unlock_irqrestore(&rq->lock, *flags);
1100}
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001103 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001105static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 __acquires(rq->lock)
1107{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001108 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 local_irq_disable();
1111 rq = this_rq();
1112 spin_lock(&rq->lock);
1113
1114 return rq;
1115}
1116
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001117/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001118 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001119 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001120void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001121{
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001122 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001123
Andrew Mortond478c2c2008-04-26 11:30:34 -07001124 WARN_ON(!irqs_disabled());
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001125 spin_lock(&rq->lock);
1126 __update_rq_clock(rq);
1127 spin_unlock(&rq->lock);
1128 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001129}
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001130EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
1131
1132/*
1133 * We just idled delta nanoseconds (called with irqs disabled):
1134 */
1135void sched_clock_idle_wakeup_event(u64 delta_ns)
1136{
1137 struct rq *rq = cpu_rq(smp_processor_id());
1138 u64 now = sched_clock();
1139
Andrew Mortond478c2c2008-04-26 11:30:34 -07001140 WARN_ON(!irqs_disabled());
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001141 rq->idle_clock += delta_ns;
1142 /*
1143 * Override the previous timestamp and ignore all
1144 * sched_clock() deltas that occured while we idled,
1145 * and use the PM-provided delta_ns to advance the
1146 * rq clock:
1147 */
1148 spin_lock(&rq->lock);
1149 rq->prev_clock_raw = now;
1150 rq->clock += delta_ns;
1151 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +01001152 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001153}
1154EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001155
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001156static void __resched_task(struct task_struct *p, int tif_bit);
1157
1158static inline void resched_task(struct task_struct *p)
1159{
1160 __resched_task(p, TIF_NEED_RESCHED);
1161}
1162
1163#ifdef CONFIG_SCHED_HRTICK
1164/*
1165 * Use HR-timers to deliver accurate preemption points.
1166 *
1167 * Its all a bit involved since we cannot program an hrt while holding the
1168 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1169 * reschedule event.
1170 *
1171 * When we get rescheduled we reprogram the hrtick_timer outside of the
1172 * rq->lock.
1173 */
1174static inline void resched_hrt(struct task_struct *p)
1175{
1176 __resched_task(p, TIF_HRTICK_RESCHED);
1177}
1178
1179static inline void resched_rq(struct rq *rq)
1180{
1181 unsigned long flags;
1182
1183 spin_lock_irqsave(&rq->lock, flags);
1184 resched_task(rq->curr);
1185 spin_unlock_irqrestore(&rq->lock, flags);
1186}
1187
1188enum {
1189 HRTICK_SET, /* re-programm hrtick_timer */
1190 HRTICK_RESET, /* not a new slice */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001191 HRTICK_BLOCK, /* stop hrtick operations */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001192};
1193
1194/*
1195 * Use hrtick when:
1196 * - enabled by features
1197 * - hrtimer is actually high res
1198 */
1199static inline int hrtick_enabled(struct rq *rq)
1200{
1201 if (!sched_feat(HRTICK))
1202 return 0;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001203 if (unlikely(test_bit(HRTICK_BLOCK, &rq->hrtick_flags)))
1204 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001205 return hrtimer_is_hres_active(&rq->hrtick_timer);
1206}
1207
1208/*
1209 * Called to set the hrtick timer state.
1210 *
1211 * called with rq->lock held and irqs disabled
1212 */
1213static void hrtick_start(struct rq *rq, u64 delay, int reset)
1214{
1215 assert_spin_locked(&rq->lock);
1216
1217 /*
1218 * preempt at: now + delay
1219 */
1220 rq->hrtick_expire =
1221 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1222 /*
1223 * indicate we need to program the timer
1224 */
1225 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1226 if (reset)
1227 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1228
1229 /*
1230 * New slices are called from the schedule path and don't need a
1231 * forced reschedule.
1232 */
1233 if (reset)
1234 resched_hrt(rq->curr);
1235}
1236
1237static void hrtick_clear(struct rq *rq)
1238{
1239 if (hrtimer_active(&rq->hrtick_timer))
1240 hrtimer_cancel(&rq->hrtick_timer);
1241}
1242
1243/*
1244 * Update the timer from the possible pending state.
1245 */
1246static void hrtick_set(struct rq *rq)
1247{
1248 ktime_t time;
1249 int set, reset;
1250 unsigned long flags;
1251
1252 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1253
1254 spin_lock_irqsave(&rq->lock, flags);
1255 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1256 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1257 time = rq->hrtick_expire;
1258 clear_thread_flag(TIF_HRTICK_RESCHED);
1259 spin_unlock_irqrestore(&rq->lock, flags);
1260
1261 if (set) {
1262 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1263 if (reset && !hrtimer_active(&rq->hrtick_timer))
1264 resched_rq(rq);
1265 } else
1266 hrtick_clear(rq);
1267}
1268
1269/*
1270 * High-resolution timer tick.
1271 * Runs from hardirq context with interrupts disabled.
1272 */
1273static enum hrtimer_restart hrtick(struct hrtimer *timer)
1274{
1275 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1276
1277 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1278
1279 spin_lock(&rq->lock);
1280 __update_rq_clock(rq);
1281 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1282 spin_unlock(&rq->lock);
1283
1284 return HRTIMER_NORESTART;
1285}
1286
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001287static void hotplug_hrtick_disable(int cpu)
1288{
1289 struct rq *rq = cpu_rq(cpu);
1290 unsigned long flags;
1291
1292 spin_lock_irqsave(&rq->lock, flags);
1293 rq->hrtick_flags = 0;
1294 __set_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1295 spin_unlock_irqrestore(&rq->lock, flags);
1296
1297 hrtick_clear(rq);
1298}
1299
1300static void hotplug_hrtick_enable(int cpu)
1301{
1302 struct rq *rq = cpu_rq(cpu);
1303 unsigned long flags;
1304
1305 spin_lock_irqsave(&rq->lock, flags);
1306 __clear_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1307 spin_unlock_irqrestore(&rq->lock, flags);
1308}
1309
1310static int
1311hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1312{
1313 int cpu = (int)(long)hcpu;
1314
1315 switch (action) {
1316 case CPU_UP_CANCELED:
1317 case CPU_UP_CANCELED_FROZEN:
1318 case CPU_DOWN_PREPARE:
1319 case CPU_DOWN_PREPARE_FROZEN:
1320 case CPU_DEAD:
1321 case CPU_DEAD_FROZEN:
1322 hotplug_hrtick_disable(cpu);
1323 return NOTIFY_OK;
1324
1325 case CPU_UP_PREPARE:
1326 case CPU_UP_PREPARE_FROZEN:
1327 case CPU_DOWN_FAILED:
1328 case CPU_DOWN_FAILED_FROZEN:
1329 case CPU_ONLINE:
1330 case CPU_ONLINE_FROZEN:
1331 hotplug_hrtick_enable(cpu);
1332 return NOTIFY_OK;
1333 }
1334
1335 return NOTIFY_DONE;
1336}
1337
1338static void init_hrtick(void)
1339{
1340 hotcpu_notifier(hotplug_hrtick, 0);
1341}
1342
1343static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001344{
1345 rq->hrtick_flags = 0;
1346 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1347 rq->hrtick_timer.function = hrtick;
1348 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1349}
1350
1351void hrtick_resched(void)
1352{
1353 struct rq *rq;
1354 unsigned long flags;
1355
1356 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1357 return;
1358
1359 local_irq_save(flags);
1360 rq = cpu_rq(smp_processor_id());
1361 hrtick_set(rq);
1362 local_irq_restore(flags);
1363}
1364#else
1365static inline void hrtick_clear(struct rq *rq)
1366{
1367}
1368
1369static inline void hrtick_set(struct rq *rq)
1370{
1371}
1372
1373static inline void init_rq_hrtick(struct rq *rq)
1374{
1375}
1376
1377void hrtick_resched(void)
1378{
1379}
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001380
1381static inline void init_hrtick(void)
1382{
1383}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001384#endif
1385
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001386/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001387 * resched_task - mark a task 'to be rescheduled now'.
1388 *
1389 * On UP this means the setting of the need_resched flag, on SMP it
1390 * might also involve a cross-CPU call to trigger the scheduler on
1391 * the target CPU.
1392 */
1393#ifdef CONFIG_SMP
1394
1395#ifndef tsk_is_polling
1396#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1397#endif
1398
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001399static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001400{
1401 int cpu;
1402
1403 assert_spin_locked(&task_rq(p)->lock);
1404
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001405 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001406 return;
1407
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001408 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001409
1410 cpu = task_cpu(p);
1411 if (cpu == smp_processor_id())
1412 return;
1413
1414 /* NEED_RESCHED must be visible before we test polling */
1415 smp_mb();
1416 if (!tsk_is_polling(p))
1417 smp_send_reschedule(cpu);
1418}
1419
1420static void resched_cpu(int cpu)
1421{
1422 struct rq *rq = cpu_rq(cpu);
1423 unsigned long flags;
1424
1425 if (!spin_trylock_irqsave(&rq->lock, flags))
1426 return;
1427 resched_task(cpu_curr(cpu));
1428 spin_unlock_irqrestore(&rq->lock, flags);
1429}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001430
1431#ifdef CONFIG_NO_HZ
1432/*
1433 * When add_timer_on() enqueues a timer into the timer wheel of an
1434 * idle CPU then this timer might expire before the next timer event
1435 * which is scheduled to wake up that CPU. In case of a completely
1436 * idle system the next event might even be infinite time into the
1437 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1438 * leaves the inner idle loop so the newly added timer is taken into
1439 * account when the CPU goes back to idle and evaluates the timer
1440 * wheel for the next timer event.
1441 */
1442void wake_up_idle_cpu(int cpu)
1443{
1444 struct rq *rq = cpu_rq(cpu);
1445
1446 if (cpu == smp_processor_id())
1447 return;
1448
1449 /*
1450 * This is safe, as this function is called with the timer
1451 * wheel base lock of (cpu) held. When the CPU is on the way
1452 * to idle and has not yet set rq->curr to idle then it will
1453 * be serialized on the timer wheel base lock and take the new
1454 * timer into account automatically.
1455 */
1456 if (rq->curr != rq->idle)
1457 return;
1458
1459 /*
1460 * We can set TIF_RESCHED on the idle task of the other CPU
1461 * lockless. The worst case is that the other CPU runs the
1462 * idle task through an additional NOOP schedule()
1463 */
1464 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1465
1466 /* NEED_RESCHED must be visible before we test polling */
1467 smp_mb();
1468 if (!tsk_is_polling(rq->idle))
1469 smp_send_reschedule(cpu);
1470}
1471#endif
1472
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001473#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001474static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001475{
1476 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001477 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001478}
1479#endif
1480
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001481#if BITS_PER_LONG == 32
1482# define WMULT_CONST (~0UL)
1483#else
1484# define WMULT_CONST (1UL << 32)
1485#endif
1486
1487#define WMULT_SHIFT 32
1488
Ingo Molnar194081e2007-08-09 11:16:51 +02001489/*
1490 * Shift right and round:
1491 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001492#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001493
Peter Zijlstra8f1bc3852008-04-19 19:45:00 +02001494/*
1495 * delta *= weight / lw
1496 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001497static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001498calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1499 struct load_weight *lw)
1500{
1501 u64 tmp;
1502
Peter Zijlstrae05510d2008-05-05 23:56:17 +02001503 if (!lw->inv_weight)
1504 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)/(lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001505
1506 tmp = (u64)delta_exec * weight;
1507 /*
1508 * Check whether we'd overflow the 64-bit multiplication:
1509 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001510 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001511 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001512 WMULT_SHIFT/2);
1513 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001514 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001515
Ingo Molnarecf691d2007-08-02 17:41:40 +02001516 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001517}
1518
Ingo Molnar10919852007-10-15 17:00:04 +02001519static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001520{
1521 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001522 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001523}
1524
Ingo Molnar10919852007-10-15 17:00:04 +02001525static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001526{
1527 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001528 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001529}
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001532 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1533 * of tasks with abnormal "nice" values across CPUs the contribution that
1534 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001535 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001536 * scaled version of the new time slice allocation that they receive on time
1537 * slice expiry etc.
1538 */
1539
Ingo Molnardd41f592007-07-09 18:51:59 +02001540#define WEIGHT_IDLEPRIO 2
1541#define WMULT_IDLEPRIO (1 << 31)
1542
1543/*
1544 * Nice levels are multiplicative, with a gentle 10% change for every
1545 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1546 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1547 * that remained on nice 0.
1548 *
1549 * The "10% effect" is relative and cumulative: from _any_ nice level,
1550 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001551 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1552 * If a task goes up by ~10% and another task goes down by ~10% then
1553 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001554 */
1555static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001556 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1557 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1558 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1559 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1560 /* 0 */ 1024, 820, 655, 526, 423,
1561 /* 5 */ 335, 272, 215, 172, 137,
1562 /* 10 */ 110, 87, 70, 56, 45,
1563 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001564};
1565
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001566/*
1567 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1568 *
1569 * In cases where the weight does not change often, we can use the
1570 * precalculated inverse to speed up arithmetics by turning divisions
1571 * into multiplications:
1572 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001573static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001574 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1575 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1576 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1577 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1578 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1579 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1580 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1581 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001582};
Peter Williams2dd73a42006-06-27 02:54:34 -07001583
Ingo Molnardd41f592007-07-09 18:51:59 +02001584static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1585
1586/*
1587 * runqueue iterator, to support SMP load-balancing between different
1588 * scheduling classes, without having to expose their internal data
1589 * structures to the load-balancing proper:
1590 */
1591struct rq_iterator {
1592 void *arg;
1593 struct task_struct *(*start)(void *);
1594 struct task_struct *(*next)(void *);
1595};
1596
Peter Williamse1d14842007-10-24 18:23:51 +02001597#ifdef CONFIG_SMP
1598static unsigned long
1599balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1600 unsigned long max_load_move, struct sched_domain *sd,
1601 enum cpu_idle_type idle, int *all_pinned,
1602 int *this_best_prio, struct rq_iterator *iterator);
1603
1604static int
1605iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1606 struct sched_domain *sd, enum cpu_idle_type idle,
1607 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001608#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001609
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001610#ifdef CONFIG_CGROUP_CPUACCT
1611static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1612#else
1613static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1614#endif
1615
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001616static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1617{
1618 update_load_add(&rq->load, load);
1619}
1620
1621static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1622{
1623 update_load_sub(&rq->load, load);
1624}
1625
Gregory Haskinse7693a32008-01-25 21:08:09 +01001626#ifdef CONFIG_SMP
1627static unsigned long source_load(int cpu, int type);
1628static unsigned long target_load(int cpu, int type);
1629static unsigned long cpu_avg_load_per_task(int cpu);
1630static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001631
1632#ifdef CONFIG_FAIR_GROUP_SCHED
1633
1634/*
1635 * Group load balancing.
1636 *
1637 * We calculate a few balance domain wide aggregate numbers; load and weight.
1638 * Given the pictures below, and assuming each item has equal weight:
1639 *
1640 * root 1 - thread
1641 * / | \ A - group
1642 * A 1 B
1643 * /|\ / \
1644 * C 2 D 3 4
1645 * | |
1646 * 5 6
1647 *
1648 * load:
1649 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1650 * which equals 1/9-th of the total load.
1651 *
1652 * shares:
1653 * The weight of this group on the selected cpus.
1654 *
1655 * rq_weight:
1656 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1657 * B would get 2.
1658 *
1659 * task_weight:
1660 * Part of the rq_weight contributed by tasks; all groups except B would
1661 * get 1, B gets 2.
1662 */
1663
1664static inline struct aggregate_struct *
1665aggregate(struct task_group *tg, struct sched_domain *sd)
1666{
1667 return &tg->cfs_rq[sd->first_cpu]->aggregate;
1668}
1669
1670typedef void (*aggregate_func)(struct task_group *, struct sched_domain *);
1671
1672/*
1673 * Iterate the full tree, calling @down when first entering a node and @up when
1674 * leaving it for the final time.
1675 */
1676static
1677void aggregate_walk_tree(aggregate_func down, aggregate_func up,
1678 struct sched_domain *sd)
1679{
1680 struct task_group *parent, *child;
1681
1682 rcu_read_lock();
1683 parent = &root_task_group;
1684down:
1685 (*down)(parent, sd);
1686 list_for_each_entry_rcu(child, &parent->children, siblings) {
1687 parent = child;
1688 goto down;
1689
1690up:
1691 continue;
1692 }
1693 (*up)(parent, sd);
1694
1695 child = parent;
1696 parent = parent->parent;
1697 if (parent)
1698 goto up;
1699 rcu_read_unlock();
1700}
1701
1702/*
1703 * Calculate the aggregate runqueue weight.
1704 */
1705static
1706void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd)
1707{
1708 unsigned long rq_weight = 0;
1709 unsigned long task_weight = 0;
1710 int i;
1711
1712 for_each_cpu_mask(i, sd->span) {
1713 rq_weight += tg->cfs_rq[i]->load.weight;
1714 task_weight += tg->cfs_rq[i]->task_weight;
1715 }
1716
1717 aggregate(tg, sd)->rq_weight = rq_weight;
1718 aggregate(tg, sd)->task_weight = task_weight;
1719}
1720
1721/*
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001722 * Compute the weight of this group on the given cpus.
1723 */
1724static
1725void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd)
1726{
1727 unsigned long shares = 0;
1728 int i;
1729
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001730 for_each_cpu_mask(i, sd->span)
1731 shares += tg->cfs_rq[i]->shares;
1732
Peter Zijlstra3f5087a2008-04-25 00:25:08 +02001733 if ((!shares && aggregate(tg, sd)->rq_weight) || shares > tg->shares)
1734 shares = tg->shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001735
1736 aggregate(tg, sd)->shares = shares;
1737}
1738
1739/*
1740 * Compute the load fraction assigned to this group, relies on the aggregate
1741 * weight and this group's parent's load, i.e. top-down.
1742 */
1743static
1744void aggregate_group_load(struct task_group *tg, struct sched_domain *sd)
1745{
1746 unsigned long load;
1747
1748 if (!tg->parent) {
1749 int i;
1750
1751 load = 0;
1752 for_each_cpu_mask(i, sd->span)
1753 load += cpu_rq(i)->load.weight;
1754
1755 } else {
1756 load = aggregate(tg->parent, sd)->load;
1757
1758 /*
1759 * shares is our weight in the parent's rq so
1760 * shares/parent->rq_weight gives our fraction of the load
1761 */
1762 load *= aggregate(tg, sd)->shares;
1763 load /= aggregate(tg->parent, sd)->rq_weight + 1;
1764 }
1765
1766 aggregate(tg, sd)->load = load;
1767}
1768
1769static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1770
1771/*
1772 * Calculate and set the cpu's group shares.
1773 */
1774static void
1775__update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,
1776 int tcpu)
1777{
1778 int boost = 0;
1779 unsigned long shares;
1780 unsigned long rq_weight;
1781
1782 if (!tg->se[tcpu])
1783 return;
1784
1785 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1786
1787 /*
1788 * If there are currently no tasks on the cpu pretend there is one of
1789 * average load so that when a new task gets to run here it will not
1790 * get delayed by group starvation.
1791 */
1792 if (!rq_weight) {
1793 boost = 1;
1794 rq_weight = NICE_0_LOAD;
1795 }
1796
1797 /*
1798 * \Sum shares * rq_weight
1799 * shares = -----------------------
1800 * \Sum rq_weight
1801 *
1802 */
1803 shares = aggregate(tg, sd)->shares * rq_weight;
1804 shares /= aggregate(tg, sd)->rq_weight + 1;
1805
1806 /*
1807 * record the actual number of shares, not the boosted amount.
1808 */
1809 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1810
1811 if (shares < MIN_SHARES)
1812 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08001813 else if (shares > MAX_SHARES)
1814 shares = MAX_SHARES;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001815
1816 __set_se_shares(tg->se[tcpu], shares);
1817}
1818
1819/*
1820 * Re-adjust the weights on the cpu the task came from and on the cpu the
1821 * task went to.
1822 */
1823static void
1824__move_group_shares(struct task_group *tg, struct sched_domain *sd,
1825 int scpu, int dcpu)
1826{
1827 unsigned long shares;
1828
1829 shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1830
1831 __update_group_shares_cpu(tg, sd, scpu);
1832 __update_group_shares_cpu(tg, sd, dcpu);
1833
1834 /*
1835 * ensure we never loose shares due to rounding errors in the
1836 * above redistribution.
1837 */
1838 shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1839 if (shares)
1840 tg->cfs_rq[dcpu]->shares += shares;
1841}
1842
1843/*
1844 * Because changing a group's shares changes the weight of the super-group
1845 * we need to walk up the tree and change all shares until we hit the root.
1846 */
1847static void
1848move_group_shares(struct task_group *tg, struct sched_domain *sd,
1849 int scpu, int dcpu)
1850{
1851 while (tg) {
1852 __move_group_shares(tg, sd, scpu, dcpu);
1853 tg = tg->parent;
1854 }
1855}
1856
1857static
1858void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd)
1859{
1860 unsigned long shares = aggregate(tg, sd)->shares;
1861 int i;
1862
1863 for_each_cpu_mask(i, sd->span) {
1864 struct rq *rq = cpu_rq(i);
1865 unsigned long flags;
1866
1867 spin_lock_irqsave(&rq->lock, flags);
1868 __update_group_shares_cpu(tg, sd, i);
1869 spin_unlock_irqrestore(&rq->lock, flags);
1870 }
1871
1872 aggregate_group_shares(tg, sd);
1873
1874 /*
1875 * ensure we never loose shares due to rounding errors in the
1876 * above redistribution.
1877 */
1878 shares -= aggregate(tg, sd)->shares;
1879 if (shares) {
1880 tg->cfs_rq[sd->first_cpu]->shares += shares;
1881 aggregate(tg, sd)->shares += shares;
1882 }
1883}
1884
1885/*
1886 * Calculate the accumulative weight and recursive load of each task group
1887 * while walking down the tree.
1888 */
1889static
1890void aggregate_get_down(struct task_group *tg, struct sched_domain *sd)
1891{
1892 aggregate_group_weight(tg, sd);
1893 aggregate_group_shares(tg, sd);
1894 aggregate_group_load(tg, sd);
1895}
1896
1897/*
1898 * Rebalance the cpu shares while walking back up the tree.
1899 */
1900static
1901void aggregate_get_up(struct task_group *tg, struct sched_domain *sd)
1902{
1903 aggregate_group_set_shares(tg, sd);
1904}
1905
1906static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1907
1908static void __init init_aggregate(void)
1909{
1910 int i;
1911
1912 for_each_possible_cpu(i)
1913 spin_lock_init(&per_cpu(aggregate_lock, i));
1914}
1915
1916static int get_aggregate(struct sched_domain *sd)
1917{
1918 if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu)))
1919 return 0;
1920
1921 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd);
1922 return 1;
1923}
1924
1925static void put_aggregate(struct sched_domain *sd)
1926{
1927 spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu));
1928}
1929
1930static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1931{
1932 cfs_rq->shares = shares;
1933}
1934
1935#else
1936
1937static inline void init_aggregate(void)
1938{
1939}
1940
1941static inline int get_aggregate(struct sched_domain *sd)
1942{
1943 return 0;
1944}
1945
1946static inline void put_aggregate(struct sched_domain *sd)
1947{
1948}
1949#endif
1950
1951#else /* CONFIG_SMP */
1952
1953#ifdef CONFIG_FAIR_GROUP_SCHED
1954static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1955{
1956}
1957#endif
1958
Gregory Haskinse7693a32008-01-25 21:08:09 +01001959#endif /* CONFIG_SMP */
1960
Ingo Molnardd41f592007-07-09 18:51:59 +02001961#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001962#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001963#include "sched_fair.c"
1964#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001965#ifdef CONFIG_SCHED_DEBUG
1966# include "sched_debug.c"
1967#endif
1968
1969#define sched_class_highest (&rt_sched_class)
1970
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001971static void inc_nr_running(struct rq *rq)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001972{
1973 rq->nr_running++;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001974}
1975
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001976static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001977{
1978 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001979}
1980
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001981static void set_load_weight(struct task_struct *p)
1982{
1983 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001984 p->se.load.weight = prio_to_weight[0] * 2;
1985 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1986 return;
1987 }
1988
1989 /*
1990 * SCHED_IDLE tasks get minimal weight:
1991 */
1992 if (p->policy == SCHED_IDLE) {
1993 p->se.load.weight = WEIGHT_IDLEPRIO;
1994 p->se.load.inv_weight = WMULT_IDLEPRIO;
1995 return;
1996 }
1997
1998 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1999 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02002000}
2001
Ingo Molnar8159f872007-08-09 11:16:49 +02002002static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02002003{
2004 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02002005 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02002006 p->se.on_rq = 1;
2007}
2008
Ingo Molnar69be72c2007-08-09 11:16:49 +02002009static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02002010{
Ingo Molnarf02231e2007-08-09 11:16:48 +02002011 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02002012 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02002013}
2014
2015/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002016 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02002017 */
Ingo Molnar14531182007-07-09 18:51:59 +02002018static inline int __normal_prio(struct task_struct *p)
2019{
Ingo Molnardd41f592007-07-09 18:51:59 +02002020 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02002021}
2022
2023/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07002024 * Calculate the expected normal priority: i.e. priority
2025 * without taking RT-inheritance into account. Might be
2026 * boosted by interactivity modifiers. Changes upon fork,
2027 * setprio syscalls, and whenever the interactivity
2028 * estimator recalculates.
2029 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002030static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07002031{
2032 int prio;
2033
Ingo Molnare05606d2007-07-09 18:51:59 +02002034 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07002035 prio = MAX_RT_PRIO-1 - p->rt_priority;
2036 else
2037 prio = __normal_prio(p);
2038 return prio;
2039}
2040
2041/*
2042 * Calculate the current priority, i.e. the priority
2043 * taken into account by the scheduler. This value might
2044 * be boosted by RT tasks, or might be boosted by
2045 * interactivity modifiers. Will be RT if the task got
2046 * RT-boosted. If not then it returns p->normal_prio.
2047 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002048static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07002049{
2050 p->normal_prio = normal_prio(p);
2051 /*
2052 * If we are RT tasks or we were boosted to RT priority,
2053 * keep the priority unchanged. Otherwise, update priority
2054 * to the normal priority:
2055 */
2056 if (!rt_prio(p->prio))
2057 return p->normal_prio;
2058 return p->prio;
2059}
2060
2061/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002062 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002064static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002066 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02002067 rq->nr_uninterruptible--;
2068
Ingo Molnar8159f872007-08-09 11:16:49 +02002069 enqueue_task(rq, p, wakeup);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002070 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
2073/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 * deactivate_task - remove a task from the runqueue.
2075 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002076static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002078 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02002079 rq->nr_uninterruptible++;
2080
Ingo Molnar69be72c2007-08-09 11:16:49 +02002081 dequeue_task(rq, p, sleep);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002082 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085/**
2086 * task_curr - is this task currently executing on a CPU?
2087 * @p: the task in question.
2088 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002089inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 return cpu_curr(task_cpu(p)) == p;
2092}
2093
Peter Williams2dd73a42006-06-27 02:54:34 -07002094/* Used instead of source_load when we know the type == 0 */
2095unsigned long weighted_cpuload(const int cpu)
2096{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002097 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002098}
2099
2100static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
2101{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01002102 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002103#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01002104 /*
2105 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2106 * successfuly executed on another CPU. We must ensure that updates of
2107 * per-task data have been completed by this moment.
2108 */
2109 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02002110 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02002111#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07002112}
2113
Steven Rostedtcb469842008-01-25 21:08:22 +01002114static inline void check_class_changed(struct rq *rq, struct task_struct *p,
2115 const struct sched_class *prev_class,
2116 int oldprio, int running)
2117{
2118 if (prev_class != p->sched_class) {
2119 if (prev_class->switched_from)
2120 prev_class->switched_from(rq, p, running);
2121 p->sched_class->switched_to(rq, p, running);
2122 } else
2123 p->sched_class->prio_changed(rq, p, oldprio, running);
2124}
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02002127
Ingo Molnarcc367732007-10-15 17:00:18 +02002128/*
2129 * Is this task likely cache-hot:
2130 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002131static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002132task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2133{
2134 s64 delta;
2135
Ingo Molnarf540a602008-03-15 17:10:34 +01002136 /*
2137 * Buddy candidates are cache hot:
2138 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01002139 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01002140 return 1;
2141
Ingo Molnarcc367732007-10-15 17:00:18 +02002142 if (p->sched_class != &fair_sched_class)
2143 return 0;
2144
Ingo Molnar6bc16652007-10-15 17:00:18 +02002145 if (sysctl_sched_migration_cost == -1)
2146 return 1;
2147 if (sysctl_sched_migration_cost == 0)
2148 return 0;
2149
Ingo Molnarcc367732007-10-15 17:00:18 +02002150 delta = now - p->se.exec_start;
2151
2152 return delta < (s64)sysctl_sched_migration_cost;
2153}
2154
2155
Ingo Molnardd41f592007-07-09 18:51:59 +02002156void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002157{
Ingo Molnardd41f592007-07-09 18:51:59 +02002158 int old_cpu = task_cpu(p);
2159 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002160 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
2161 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02002162 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002163
2164 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002165
2166#ifdef CONFIG_SCHEDSTATS
2167 if (p->se.wait_start)
2168 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002169 if (p->se.sleep_start)
2170 p->se.sleep_start -= clock_offset;
2171 if (p->se.block_start)
2172 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02002173 if (old_cpu != new_cpu) {
2174 schedstat_inc(p, se.nr_migrations);
2175 if (task_hot(p, old_rq->clock, NULL))
2176 schedstat_inc(p, se.nr_forced2_migrations);
2177 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002178#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002179 p->se.vruntime -= old_cfsrq->min_vruntime -
2180 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02002181
2182 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002183}
2184
Ingo Molnar70b97a72006-07-03 00:25:42 -07002185struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Ingo Molnar36c8b582006-07-03 00:25:41 -07002188 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 int dest_cpu;
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002192};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194/*
2195 * The task's runqueue lock must be held.
2196 * Returns true if you have to wait for migration thread.
2197 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002198static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002199migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002201 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /*
2204 * If the task is not on a runqueue (and not running), then
2205 * it is sufficient to simply update the task's cpu field.
2206 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002207 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 set_task_cpu(p, dest_cpu);
2209 return 0;
2210 }
2211
2212 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 req->task = p;
2214 req->dest_cpu = dest_cpu;
2215 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return 1;
2218}
2219
2220/*
2221 * wait_task_inactive - wait for a thread to unschedule.
2222 *
2223 * The caller must ensure that the task *will* unschedule sometime soon,
2224 * else this function might spin for a *long* time. This function can't
2225 * be called with interrupts off, or it may introduce deadlock with
2226 * smp_call_function() if an IPI is sent by the same process we are
2227 * waiting to become inactive.
2228 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002229void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002232 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002233 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Andi Kleen3a5c3592007-10-15 17:00:14 +02002235 for (;;) {
2236 /*
2237 * We do the initial early heuristics without holding
2238 * any task-queue locks at all. We'll only try to get
2239 * the runqueue lock when things look like they will
2240 * work out!
2241 */
2242 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002243
Andi Kleen3a5c3592007-10-15 17:00:14 +02002244 /*
2245 * If the task is actively running on another CPU
2246 * still, just relax and busy-wait without holding
2247 * any locks.
2248 *
2249 * NOTE! Since we don't hold any locks, it's not
2250 * even sure that "rq" stays as the right runqueue!
2251 * But we don't care, since "task_running()" will
2252 * return false if the runqueue has changed and p
2253 * is actually now running somewhere else!
2254 */
2255 while (task_running(rq, p))
2256 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002257
Andi Kleen3a5c3592007-10-15 17:00:14 +02002258 /*
2259 * Ok, time to look more closely! We need the rq
2260 * lock now, to be *sure*. If we're wrong, we'll
2261 * just go back and repeat.
2262 */
2263 rq = task_rq_lock(p, &flags);
2264 running = task_running(rq, p);
2265 on_rq = p->se.on_rq;
2266 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002267
Andi Kleen3a5c3592007-10-15 17:00:14 +02002268 /*
2269 * Was it really running after all now that we
2270 * checked with the proper locks actually held?
2271 *
2272 * Oops. Go back and try again..
2273 */
2274 if (unlikely(running)) {
2275 cpu_relax();
2276 continue;
2277 }
2278
2279 /*
2280 * It's not enough that it's not actively running,
2281 * it must be off the runqueue _entirely_, and not
2282 * preempted!
2283 *
2284 * So if it wa still runnable (but just not actively
2285 * running right now), it's preempted, and we should
2286 * yield - it could be a while.
2287 */
2288 if (unlikely(on_rq)) {
2289 schedule_timeout_uninterruptible(1);
2290 continue;
2291 }
2292
2293 /*
2294 * Ahh, all good. It wasn't running, and it wasn't
2295 * runnable, which means that it will never become
2296 * running in the future either. We're all done!
2297 */
2298 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
2302/***
2303 * kick_process - kick a running thread to enter/exit the kernel
2304 * @p: the to-be-kicked thread
2305 *
2306 * Cause a process which is running on another CPU to enter
2307 * kernel-mode, without any delay. (to get signals handled.)
2308 *
2309 * NOTE: this function doesnt have to take the runqueue lock,
2310 * because all it wants to ensure is that the remote task enters
2311 * the kernel. If the IPI races and the task has been migrated
2312 * to another CPU then no harm is done and the purpose has been
2313 * achieved as well.
2314 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002315void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
2317 int cpu;
2318
2319 preempt_disable();
2320 cpu = task_cpu(p);
2321 if ((cpu != smp_processor_id()) && task_curr(p))
2322 smp_send_reschedule(cpu);
2323 preempt_enable();
2324}
2325
2326/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002327 * Return a low guess at the load of a migration-source cpu weighted
2328 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 *
2330 * We want to under-estimate the load of migration sources, to
2331 * balance conservatively.
2332 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002333static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002334{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002335 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002336 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002337
Peter Williams2dd73a42006-06-27 02:54:34 -07002338 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002339 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002340
Ingo Molnardd41f592007-07-09 18:51:59 +02002341 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
2344/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002345 * Return a high guess at the load of a migration-target cpu weighted
2346 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002348static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002349{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002350 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002351 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002352
Peter Williams2dd73a42006-06-27 02:54:34 -07002353 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002354 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002355
Ingo Molnardd41f592007-07-09 18:51:59 +02002356 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002357}
2358
2359/*
2360 * Return the average load per task on the cpu's run queue
2361 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002362static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07002363{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002364 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002365 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07002366 unsigned long n = rq->nr_running;
2367
Ingo Molnardd41f592007-07-09 18:51:59 +02002368 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
Nick Piggin147cbb42005-06-25 14:57:19 -07002371/*
2372 * find_idlest_group finds and returns the least busy CPU group within the
2373 * domain.
2374 */
2375static struct sched_group *
2376find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2377{
2378 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2379 unsigned long min_load = ULONG_MAX, this_load = 0;
2380 int load_idx = sd->forkexec_idx;
2381 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2382
2383 do {
2384 unsigned long load, avg_load;
2385 int local_group;
2386 int i;
2387
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002388 /* Skip over this group if it has no CPUs allowed */
2389 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002390 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002391
Nick Piggin147cbb42005-06-25 14:57:19 -07002392 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002393
2394 /* Tally up the load of all CPUs in the group */
2395 avg_load = 0;
2396
2397 for_each_cpu_mask(i, group->cpumask) {
2398 /* Bias balancing toward cpus of our domain */
2399 if (local_group)
2400 load = source_load(i, load_idx);
2401 else
2402 load = target_load(i, load_idx);
2403
2404 avg_load += load;
2405 }
2406
2407 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002408 avg_load = sg_div_cpu_power(group,
2409 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002410
2411 if (local_group) {
2412 this_load = avg_load;
2413 this = group;
2414 } else if (avg_load < min_load) {
2415 min_load = avg_load;
2416 idlest = group;
2417 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002418 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002419
2420 if (!idlest || 100*this_load < imbalance*min_load)
2421 return NULL;
2422 return idlest;
2423}
2424
2425/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002426 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002427 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002428static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002429find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2430 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002431{
2432 unsigned long load, min_load = ULONG_MAX;
2433 int idlest = -1;
2434 int i;
2435
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002436 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002437 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002438
Mike Travis7c16ec52008-04-04 18:11:11 -07002439 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002440 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002441
2442 if (load < min_load || (load == min_load && i == this_cpu)) {
2443 min_load = load;
2444 idlest = i;
2445 }
2446 }
2447
2448 return idlest;
2449}
2450
Nick Piggin476d1392005-06-25 14:57:29 -07002451/*
2452 * sched_balance_self: balance the current task (running on cpu) in domains
2453 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2454 * SD_BALANCE_EXEC.
2455 *
2456 * Balance, ie. select the least loaded group.
2457 *
2458 * Returns the target CPU number, or the same CPU if no balancing is needed.
2459 *
2460 * preempt must be disabled.
2461 */
2462static int sched_balance_self(int cpu, int flag)
2463{
2464 struct task_struct *t = current;
2465 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002466
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002467 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002468 /*
2469 * If power savings logic is enabled for a domain, stop there.
2470 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002471 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2472 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002473 if (tmp->flags & flag)
2474 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002475 }
Nick Piggin476d1392005-06-25 14:57:29 -07002476
2477 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002478 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002479 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002480 int new_cpu, weight;
2481
2482 if (!(sd->flags & flag)) {
2483 sd = sd->child;
2484 continue;
2485 }
Nick Piggin476d1392005-06-25 14:57:29 -07002486
2487 span = sd->span;
2488 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002489 if (!group) {
2490 sd = sd->child;
2491 continue;
2492 }
Nick Piggin476d1392005-06-25 14:57:29 -07002493
Mike Travis7c16ec52008-04-04 18:11:11 -07002494 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002495 if (new_cpu == -1 || new_cpu == cpu) {
2496 /* Now try balancing at a lower domain level of cpu */
2497 sd = sd->child;
2498 continue;
2499 }
Nick Piggin476d1392005-06-25 14:57:29 -07002500
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002501 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002502 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002503 sd = NULL;
2504 weight = cpus_weight(span);
2505 for_each_domain(cpu, tmp) {
2506 if (weight <= cpus_weight(tmp->span))
2507 break;
2508 if (tmp->flags & flag)
2509 sd = tmp;
2510 }
2511 /* while loop will break here if sd == NULL */
2512 }
2513
2514 return cpu;
2515}
2516
2517#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519/***
2520 * try_to_wake_up - wake up a thread
2521 * @p: the to-be-woken-up thread
2522 * @state: the mask of task states that can be woken
2523 * @sync: do a synchronous wakeup?
2524 *
2525 * Put it on the run-queue if it's not already there. The "current"
2526 * thread is always on the run-queue (except when the actual
2527 * re-schedule is in progress), and as such you're allowed to do
2528 * the simpler "current->state = TASK_RUNNING" to mark yourself
2529 * runnable without the overhead of this.
2530 *
2531 * returns failure only if the task is already active.
2532 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002533static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Ingo Molnarcc367732007-10-15 17:00:18 +02002535 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 unsigned long flags;
2537 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002538 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Ingo Molnarb85d0662008-03-16 20:03:22 +01002540 if (!sched_feat(SYNC_WAKEUPS))
2541 sync = 0;
2542
Linus Torvalds04e2f172008-02-23 18:05:03 -08002543 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 rq = task_rq_lock(p, &flags);
2545 old_state = p->state;
2546 if (!(old_state & state))
2547 goto out;
2548
Ingo Molnardd41f592007-07-09 18:51:59 +02002549 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 goto out_running;
2551
2552 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002553 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 this_cpu = smp_processor_id();
2555
2556#ifdef CONFIG_SMP
2557 if (unlikely(task_running(rq, p)))
2558 goto out_activate;
2559
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002560 cpu = p->sched_class->select_task_rq(p, sync);
2561 if (cpu != orig_cpu) {
2562 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 task_rq_unlock(rq, &flags);
2564 /* might preempt at this point */
2565 rq = task_rq_lock(p, &flags);
2566 old_state = p->state;
2567 if (!(old_state & state))
2568 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002569 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 goto out_running;
2571
2572 this_cpu = smp_processor_id();
2573 cpu = task_cpu(p);
2574 }
2575
Gregory Haskinse7693a32008-01-25 21:08:09 +01002576#ifdef CONFIG_SCHEDSTATS
2577 schedstat_inc(rq, ttwu_count);
2578 if (cpu == this_cpu)
2579 schedstat_inc(rq, ttwu_local);
2580 else {
2581 struct sched_domain *sd;
2582 for_each_domain(this_cpu, sd) {
2583 if (cpu_isset(cpu, sd->span)) {
2584 schedstat_inc(sd, ttwu_wake_remote);
2585 break;
2586 }
2587 }
2588 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002589#endif
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591out_activate:
2592#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002593 schedstat_inc(p, se.nr_wakeups);
2594 if (sync)
2595 schedstat_inc(p, se.nr_wakeups_sync);
2596 if (orig_cpu != cpu)
2597 schedstat_inc(p, se.nr_wakeups_migrate);
2598 if (cpu == this_cpu)
2599 schedstat_inc(p, se.nr_wakeups_local);
2600 else
2601 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002602 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002603 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 success = 1;
2605
2606out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002607 check_preempt_curr(rq, p);
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002610#ifdef CONFIG_SMP
2611 if (p->sched_class->task_wake_up)
2612 p->sched_class->task_wake_up(rq, p);
2613#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614out:
2615 task_rq_unlock(rq, &flags);
2616
2617 return success;
2618}
2619
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002620int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002622 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624EXPORT_SYMBOL(wake_up_process);
2625
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002626int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
2628 return try_to_wake_up(p, state, 0);
2629}
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631/*
2632 * Perform scheduler related setup for a newly forked process p.
2633 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002634 *
2635 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002637static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
Ingo Molnardd41f592007-07-09 18:51:59 +02002639 p->se.exec_start = 0;
2640 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002641 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002642 p->se.last_wakeup = 0;
2643 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002644
2645#ifdef CONFIG_SCHEDSTATS
2646 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002647 p->se.sum_sleep_runtime = 0;
2648 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002649 p->se.block_start = 0;
2650 p->se.sleep_max = 0;
2651 p->se.block_max = 0;
2652 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002653 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002654 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002655#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002656
Peter Zijlstrafa717062008-01-25 21:08:27 +01002657 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002658 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002659 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002660
Avi Kivitye107be32007-07-26 13:40:43 +02002661#ifdef CONFIG_PREEMPT_NOTIFIERS
2662 INIT_HLIST_HEAD(&p->preempt_notifiers);
2663#endif
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 /*
2666 * We mark the process as running here, but have not actually
2667 * inserted it onto the runqueue yet. This guarantees that
2668 * nobody will actually run it, and a signal or other external
2669 * event cannot wake it up and insert it on the runqueue either.
2670 */
2671 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002672}
2673
2674/*
2675 * fork()/clone()-time setup:
2676 */
2677void sched_fork(struct task_struct *p, int clone_flags)
2678{
2679 int cpu = get_cpu();
2680
2681 __sched_fork(p);
2682
2683#ifdef CONFIG_SMP
2684 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2685#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002686 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002687
2688 /*
2689 * Make sure we do not leak PI boosting priority to the child:
2690 */
2691 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002692 if (!rt_prio(p->prio))
2693 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002694
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002695#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002696 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002697 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002699#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002700 p->oncpu = 0;
2701#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002703 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002704 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002706 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
2709/*
2710 * wake_up_new_task - wake up a newly created task for the first time.
2711 *
2712 * This function will do some initial scheduler statistics housekeeping
2713 * that must be done for every newly created context, then puts the task
2714 * on the runqueue and wakes it.
2715 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002716void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002719 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002723 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 p->prio = effective_prio(p);
2726
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002727 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002728 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002731 * Let the scheduling class do new task startup
2732 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002734 p->sched_class->task_new(rq, p);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002735 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002737 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002738#ifdef CONFIG_SMP
2739 if (p->sched_class->task_wake_up)
2740 p->sched_class->task_wake_up(rq, p);
2741#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002742 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
2744
Avi Kivitye107be32007-07-26 13:40:43 +02002745#ifdef CONFIG_PREEMPT_NOTIFIERS
2746
2747/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002748 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2749 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002750 */
2751void preempt_notifier_register(struct preempt_notifier *notifier)
2752{
2753 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2754}
2755EXPORT_SYMBOL_GPL(preempt_notifier_register);
2756
2757/**
2758 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002759 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002760 *
2761 * This is safe to call from within a preemption notifier.
2762 */
2763void preempt_notifier_unregister(struct preempt_notifier *notifier)
2764{
2765 hlist_del(&notifier->link);
2766}
2767EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2768
2769static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2770{
2771 struct preempt_notifier *notifier;
2772 struct hlist_node *node;
2773
2774 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2775 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2776}
2777
2778static void
2779fire_sched_out_preempt_notifiers(struct task_struct *curr,
2780 struct task_struct *next)
2781{
2782 struct preempt_notifier *notifier;
2783 struct hlist_node *node;
2784
2785 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2786 notifier->ops->sched_out(notifier, next);
2787}
2788
2789#else
2790
2791static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2792{
2793}
2794
2795static void
2796fire_sched_out_preempt_notifiers(struct task_struct *curr,
2797 struct task_struct *next)
2798{
2799}
2800
2801#endif
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002804 * prepare_task_switch - prepare to switch tasks
2805 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002806 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002807 * @next: the task we are going to switch to.
2808 *
2809 * This is called with the rq lock held and interrupts off. It must
2810 * be paired with a subsequent finish_task_switch after the context
2811 * switch.
2812 *
2813 * prepare_task_switch sets up locking and calls architecture specific
2814 * hooks.
2815 */
Avi Kivitye107be32007-07-26 13:40:43 +02002816static inline void
2817prepare_task_switch(struct rq *rq, struct task_struct *prev,
2818 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002819{
Avi Kivitye107be32007-07-26 13:40:43 +02002820 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002821 prepare_lock_switch(rq, next);
2822 prepare_arch_switch(next);
2823}
2824
2825/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002827 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 * @prev: the thread we just switched away from.
2829 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002830 * finish_task_switch must be called after the context switch, paired
2831 * with a prepare_task_switch call before the context switch.
2832 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2833 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 *
2835 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002836 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 * with the lock held can cause deadlocks; see schedule() for
2838 * details.)
2839 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002840static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 __releases(rq->lock)
2842{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002844 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 rq->prev_mm = NULL;
2847
2848 /*
2849 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002850 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002851 * schedule one last time. The schedule call will never return, and
2852 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002853 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 * still held, otherwise prev could be scheduled on another cpu, die
2855 * there before we look at prev->state, and then the reference would
2856 * be dropped twice.
2857 * Manfred Spraul <manfred@colorfullife.com>
2858 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002859 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002860 finish_arch_switch(prev);
2861 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002862#ifdef CONFIG_SMP
2863 if (current->sched_class->post_schedule)
2864 current->sched_class->post_schedule(rq);
2865#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002866
Avi Kivitye107be32007-07-26 13:40:43 +02002867 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (mm)
2869 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002870 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002871 /*
2872 * Remove function-return probe instances associated with this
2873 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002874 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002875 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878}
2879
2880/**
2881 * schedule_tail - first thing a freshly forked thread must call.
2882 * @prev: the thread we just switched away from.
2883 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002884asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 __releases(rq->lock)
2886{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002887 struct rq *rq = this_rq();
2888
Nick Piggin4866cde2005-06-25 14:57:23 -07002889 finish_task_switch(rq, prev);
2890#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2891 /* In this case, finish_task_switch does not reenable preemption */
2892 preempt_enable();
2893#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002895 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
2898/*
2899 * context_switch - switch to the new MM and the new
2900 * thread's register state.
2901 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002902static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002903context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002904 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
Ingo Molnardd41f592007-07-09 18:51:59 +02002906 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Avi Kivitye107be32007-07-26 13:40:43 +02002908 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002909 mm = next->mm;
2910 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002911 /*
2912 * For paravirt, this is coupled with an exit in switch_to to
2913 * combine the page table reload and the switch backend into
2914 * one hypercall.
2915 */
2916 arch_enter_lazy_cpu_mode();
2917
Ingo Molnardd41f592007-07-09 18:51:59 +02002918 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 next->active_mm = oldmm;
2920 atomic_inc(&oldmm->mm_count);
2921 enter_lazy_tlb(oldmm, next);
2922 } else
2923 switch_mm(oldmm, mm, next);
2924
Ingo Molnardd41f592007-07-09 18:51:59 +02002925 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 rq->prev_mm = oldmm;
2928 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002929 /*
2930 * Since the runqueue lock will be released by the next
2931 * task (which is an invalid locking op but in the case
2932 * of the scheduler it's an obvious special-case), so we
2933 * do an early lockdep release here:
2934 */
2935#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002936 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002937#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 /* Here we just switch the register state and the stack. */
2940 switch_to(prev, next, prev);
2941
Ingo Molnardd41f592007-07-09 18:51:59 +02002942 barrier();
2943 /*
2944 * this_rq must be evaluated again because prev may have moved
2945 * CPUs since it called schedule(), thus the 'rq' on its stack
2946 * frame will be invalid.
2947 */
2948 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
2951/*
2952 * nr_running, nr_uninterruptible and nr_context_switches:
2953 *
2954 * externally visible scheduler statistics: current number of runnable
2955 * threads, current number of uninterruptible-sleeping threads, total
2956 * number of context switches performed since bootup.
2957 */
2958unsigned long nr_running(void)
2959{
2960 unsigned long i, sum = 0;
2961
2962 for_each_online_cpu(i)
2963 sum += cpu_rq(i)->nr_running;
2964
2965 return sum;
2966}
2967
2968unsigned long nr_uninterruptible(void)
2969{
2970 unsigned long i, sum = 0;
2971
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002972 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 sum += cpu_rq(i)->nr_uninterruptible;
2974
2975 /*
2976 * Since we read the counters lockless, it might be slightly
2977 * inaccurate. Do not allow it to go below zero though:
2978 */
2979 if (unlikely((long)sum < 0))
2980 sum = 0;
2981
2982 return sum;
2983}
2984
2985unsigned long long nr_context_switches(void)
2986{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002987 int i;
2988 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002990 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 sum += cpu_rq(i)->nr_switches;
2992
2993 return sum;
2994}
2995
2996unsigned long nr_iowait(void)
2997{
2998 unsigned long i, sum = 0;
2999
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003000 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 sum += atomic_read(&cpu_rq(i)->nr_iowait);
3002
3003 return sum;
3004}
3005
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003006unsigned long nr_active(void)
3007{
3008 unsigned long i, running = 0, uninterruptible = 0;
3009
3010 for_each_online_cpu(i) {
3011 running += cpu_rq(i)->nr_running;
3012 uninterruptible += cpu_rq(i)->nr_uninterruptible;
3013 }
3014
3015 if (unlikely((long)uninterruptible < 0))
3016 uninterruptible = 0;
3017
3018 return running + uninterruptible;
3019}
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003022 * Update rq->cpu_load[] statistics. This function is usually called every
3023 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07003024 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003025static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003026{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02003027 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02003028 int i, scale;
3029
3030 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02003031
3032 /* Update our load: */
3033 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
3034 unsigned long old_load, new_load;
3035
3036 /* scale is effectively 1 << i now, and >> i divides by scale */
3037
3038 old_load = this_rq->cpu_load[i];
3039 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02003040 /*
3041 * Round up the averaging division if load is increasing. This
3042 * prevents us from getting stuck on 9 if the load is 10, for
3043 * example.
3044 */
3045 if (new_load > old_load)
3046 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003047 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3048 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003049}
3050
Ingo Molnardd41f592007-07-09 18:51:59 +02003051#ifdef CONFIG_SMP
3052
Ingo Molnar48f24c42006-07-03 00:25:40 -07003053/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 * double_rq_lock - safely lock two runqueues
3055 *
3056 * Note this does not disable interrupts like task_rq_lock,
3057 * you need to do so manually before calling.
3058 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003059static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 __acquires(rq1->lock)
3061 __acquires(rq2->lock)
3062{
Kirill Korotaev054b9102006-12-10 02:20:11 -08003063 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 if (rq1 == rq2) {
3065 spin_lock(&rq1->lock);
3066 __acquire(rq2->lock); /* Fake it out ;) */
3067 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003068 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 spin_lock(&rq1->lock);
3070 spin_lock(&rq2->lock);
3071 } else {
3072 spin_lock(&rq2->lock);
3073 spin_lock(&rq1->lock);
3074 }
3075 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003076 update_rq_clock(rq1);
3077 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
3080/*
3081 * double_rq_unlock - safely unlock two runqueues
3082 *
3083 * Note this does not restore interrupts like task_rq_unlock,
3084 * you need to do so manually after calling.
3085 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003086static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 __releases(rq1->lock)
3088 __releases(rq2->lock)
3089{
3090 spin_unlock(&rq1->lock);
3091 if (rq1 != rq2)
3092 spin_unlock(&rq2->lock);
3093 else
3094 __release(rq2->lock);
3095}
3096
3097/*
3098 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3099 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01003100static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 __releases(this_rq->lock)
3102 __acquires(busiest->lock)
3103 __acquires(this_rq->lock)
3104{
Steven Rostedte8fa1362008-01-25 21:08:05 +01003105 int ret = 0;
3106
Kirill Korotaev054b9102006-12-10 02:20:11 -08003107 if (unlikely(!irqs_disabled())) {
3108 /* printk() doesn't work good under rq->lock */
3109 spin_unlock(&this_rq->lock);
3110 BUG_ON(1);
3111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003113 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 spin_unlock(&this_rq->lock);
3115 spin_lock(&busiest->lock);
3116 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01003117 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 } else
3119 spin_lock(&busiest->lock);
3120 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01003121 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122}
3123
3124/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 * If dest_cpu is allowed for this process, migrate the task to it.
3126 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003127 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 * the cpu_allowed mask is restored.
3129 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003130static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003132 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003134 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 rq = task_rq_lock(p, &flags);
3137 if (!cpu_isset(dest_cpu, p->cpus_allowed)
3138 || unlikely(cpu_is_offline(dest_cpu)))
3139 goto out;
3140
3141 /* force the process onto the specified CPU */
3142 if (migrate_task(p, dest_cpu, &req)) {
3143 /* Need to wait for migration thread (might exit: take ref). */
3144 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 get_task_struct(mt);
3147 task_rq_unlock(rq, &flags);
3148 wake_up_process(mt);
3149 put_task_struct(mt);
3150 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return;
3153 }
3154out:
3155 task_rq_unlock(rq, &flags);
3156}
3157
3158/*
Nick Piggin476d1392005-06-25 14:57:29 -07003159 * sched_exec - execve() is a valuable balancing opportunity, because at
3160 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 */
3162void sched_exec(void)
3163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003165 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003167 if (new_cpu != this_cpu)
3168 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
3171/*
3172 * pull_task - move a task from a remote runqueue to the local runqueue.
3173 * Both runqueues must be locked.
3174 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003175static void pull_task(struct rq *src_rq, struct task_struct *p,
3176 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003178 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003180 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 /*
3182 * Note that idle threads have a prio of MAX_PRIO, for this test
3183 * to be always true for them.
3184 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003185 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186}
3187
3188/*
3189 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3190 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003191static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003192int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003193 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003194 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
3196 /*
3197 * We do not migrate tasks that are:
3198 * 1) running (obviously), or
3199 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3200 * 3) are cache-hot on their current CPU.
3201 */
Ingo Molnarcc367732007-10-15 17:00:18 +02003202 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3203 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003205 }
Nick Piggin81026792005-06-25 14:57:07 -07003206 *all_pinned = 0;
3207
Ingo Molnarcc367732007-10-15 17:00:18 +02003208 if (task_running(rq, p)) {
3209 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003210 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Ingo Molnarda84d962007-10-15 17:00:18 +02003213 /*
3214 * Aggressive migration if:
3215 * 1) task is cache cold, or
3216 * 2) too many balance attempts have failed.
3217 */
3218
Ingo Molnar6bc16652007-10-15 17:00:18 +02003219 if (!task_hot(p, rq->clock, sd) ||
3220 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003221#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02003222 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003223 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003224 schedstat_inc(p, se.nr_forced_migrations);
3225 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003226#endif
3227 return 1;
3228 }
3229
Ingo Molnarcc367732007-10-15 17:00:18 +02003230 if (task_hot(p, rq->clock, sd)) {
3231 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003232 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return 1;
3235}
3236
Peter Williamse1d14842007-10-24 18:23:51 +02003237static unsigned long
3238balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3239 unsigned long max_load_move, struct sched_domain *sd,
3240 enum cpu_idle_type idle, int *all_pinned,
3241 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003242{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003243 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02003244 struct task_struct *p;
3245 long rem_load_move = max_load_move;
3246
Peter Williamse1d14842007-10-24 18:23:51 +02003247 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003248 goto out;
3249
3250 pinned = 1;
3251
3252 /*
3253 * Start the load-balancing iterator:
3254 */
3255 p = iterator->start(iterator->arg);
3256next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003257 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003258 goto out;
3259 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003260 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02003261 * skip a task if it will be the highest priority task (i.e. smallest
3262 * prio value) on its new queue regardless of its load weight
3263 */
3264 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
3265 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003266 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003267 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003268 p = iterator->next(iterator->arg);
3269 goto next;
3270 }
3271
3272 pull_task(busiest, p, this_rq, this_cpu);
3273 pulled++;
3274 rem_load_move -= p->se.load.weight;
3275
3276 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003277 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003278 */
Peter Williamse1d14842007-10-24 18:23:51 +02003279 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003280 if (p->prio < *this_best_prio)
3281 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003282 p = iterator->next(iterator->arg);
3283 goto next;
3284 }
3285out:
3286 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003287 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003288 * so we can safely collect pull_task() stats here rather than
3289 * inside pull_task().
3290 */
3291 schedstat_add(sd, lb_gained[idle], pulled);
3292
3293 if (all_pinned)
3294 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003295
3296 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003297}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299/*
Peter Williams43010652007-08-09 11:16:46 +02003300 * move_tasks tries to move up to max_load_move weighted load from busiest to
3301 * this_rq, as part of a balancing operation within domain "sd".
3302 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 *
3304 * Called with both runqueues locked.
3305 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003306static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003307 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003308 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003309 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003311 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003312 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003313 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Ingo Molnardd41f592007-07-09 18:51:59 +02003315 do {
Peter Williams43010652007-08-09 11:16:46 +02003316 total_load_moved +=
3317 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003318 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003319 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003320 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003321 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Peter Williams43010652007-08-09 11:16:46 +02003323 return total_load_moved > 0;
3324}
3325
Peter Williamse1d14842007-10-24 18:23:51 +02003326static int
3327iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3328 struct sched_domain *sd, enum cpu_idle_type idle,
3329 struct rq_iterator *iterator)
3330{
3331 struct task_struct *p = iterator->start(iterator->arg);
3332 int pinned = 0;
3333
3334 while (p) {
3335 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3336 pull_task(busiest, p, this_rq, this_cpu);
3337 /*
3338 * Right now, this is only the second place pull_task()
3339 * is called, so we can safely collect pull_task()
3340 * stats here rather than inside pull_task().
3341 */
3342 schedstat_inc(sd, lb_gained[idle]);
3343
3344 return 1;
3345 }
3346 p = iterator->next(iterator->arg);
3347 }
3348
3349 return 0;
3350}
3351
Peter Williams43010652007-08-09 11:16:46 +02003352/*
3353 * move_one_task tries to move exactly one task from busiest to this_rq, as
3354 * part of active balancing operations within "domain".
3355 * Returns 1 if successful and 0 otherwise.
3356 *
3357 * Called with both runqueues locked.
3358 */
3359static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3360 struct sched_domain *sd, enum cpu_idle_type idle)
3361{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003362 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003363
3364 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003365 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003366 return 1;
3367
3368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
3371/*
3372 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003373 * domain. It calculates and returns the amount of weighted load which
3374 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 */
3376static struct sched_group *
3377find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003378 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003379 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
3381 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3382 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003383 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003384 unsigned long busiest_load_per_task, busiest_nr_running;
3385 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003386 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003387#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3388 int power_savings_balance = 1;
3389 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3390 unsigned long min_nr_running = ULONG_MAX;
3391 struct sched_group *group_min = NULL, *group_leader = NULL;
3392#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
3394 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003395 busiest_load_per_task = busiest_nr_running = 0;
3396 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003397 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003398 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003399 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003400 load_idx = sd->newidle_idx;
3401 else
3402 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
3404 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003405 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 int local_group;
3407 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003408 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003409 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003410 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
3412 local_group = cpu_isset(this_cpu, group->cpumask);
3413
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003414 if (local_group)
3415 balance_cpu = first_cpu(group->cpumask);
3416
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003418 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02003419 max_cpu_load = 0;
3420 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
3422 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003423 struct rq *rq;
3424
3425 if (!cpu_isset(i, *cpus))
3426 continue;
3427
3428 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003429
Suresh Siddha9439aab2007-07-19 21:28:35 +02003430 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003431 *sd_idle = 0;
3432
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003434 if (local_group) {
3435 if (idle_cpu(i) && !first_idle_cpu) {
3436 first_idle_cpu = 1;
3437 balance_cpu = i;
3438 }
3439
Nick Piggina2000572006-02-10 01:51:02 -08003440 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003441 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003442 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003443 if (load > max_cpu_load)
3444 max_cpu_load = load;
3445 if (min_cpu_load > load)
3446 min_cpu_load = load;
3447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
3449 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003450 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003451 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 }
3453
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003454 /*
3455 * First idle cpu or the first cpu(busiest) in this sched group
3456 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003457 * domains. In the newly idle case, we will allow all the cpu's
3458 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003459 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003460 if (idle != CPU_NEWLY_IDLE && local_group &&
3461 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003462 *balance = 0;
3463 goto ret;
3464 }
3465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003467 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
3469 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003470 avg_load = sg_div_cpu_power(group,
3471 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Ken Chen908a7c12007-10-17 16:55:11 +02003473 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3474 __group_imb = 1;
3475
Eric Dumazet5517d862007-05-08 00:32:57 -07003476 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 if (local_group) {
3479 this_load = avg_load;
3480 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003481 this_nr_running = sum_nr_running;
3482 this_load_per_task = sum_weighted_load;
3483 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003484 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 max_load = avg_load;
3486 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003487 busiest_nr_running = sum_nr_running;
3488 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003489 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003491
3492#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3493 /*
3494 * Busy processors will not participate in power savings
3495 * balance.
3496 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003497 if (idle == CPU_NOT_IDLE ||
3498 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3499 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003500
3501 /*
3502 * If the local group is idle or completely loaded
3503 * no need to do power savings balance at this domain
3504 */
3505 if (local_group && (this_nr_running >= group_capacity ||
3506 !this_nr_running))
3507 power_savings_balance = 0;
3508
Ingo Molnardd41f592007-07-09 18:51:59 +02003509 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003510 * If a group is already running at full capacity or idle,
3511 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003512 */
3513 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003514 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003515 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003516
Ingo Molnardd41f592007-07-09 18:51:59 +02003517 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003518 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003519 * This is the group from where we need to pick up the load
3520 * for saving power
3521 */
3522 if ((sum_nr_running < min_nr_running) ||
3523 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003524 first_cpu(group->cpumask) <
3525 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003526 group_min = group;
3527 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003528 min_load_per_task = sum_weighted_load /
3529 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003530 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003531
Ingo Molnardd41f592007-07-09 18:51:59 +02003532 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003533 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003534 * capacity but still has some space to pick up some load
3535 * from other group and save more power
3536 */
3537 if (sum_nr_running <= group_capacity - 1) {
3538 if (sum_nr_running > leader_nr_running ||
3539 (sum_nr_running == leader_nr_running &&
3540 first_cpu(group->cpumask) >
3541 first_cpu(group_leader->cpumask))) {
3542 group_leader = group;
3543 leader_nr_running = sum_nr_running;
3544 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003545 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003546group_next:
3547#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 group = group->next;
3549 } while (group != sd->groups);
3550
Peter Williams2dd73a42006-06-27 02:54:34 -07003551 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 goto out_balanced;
3553
3554 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3555
3556 if (this_load >= avg_load ||
3557 100*max_load <= sd->imbalance_pct*this_load)
3558 goto out_balanced;
3559
Peter Williams2dd73a42006-06-27 02:54:34 -07003560 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003561 if (group_imb)
3562 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3563
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 /*
3565 * We're trying to get all the cpus to the average_load, so we don't
3566 * want to push ourselves above the average load, nor do we wish to
3567 * reduce the max loaded cpu below the average load, as either of these
3568 * actions would just result in more rebalancing later, and ping-pong
3569 * tasks around. Thus we look for the minimum possible imbalance.
3570 * Negative imbalances (*we* are more loaded than anyone else) will
3571 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003572 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 * appear as very large values with unsigned longs.
3574 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003575 if (max_load <= busiest_load_per_task)
3576 goto out_balanced;
3577
3578 /*
3579 * In the presence of smp nice balancing, certain scenarios can have
3580 * max load less than avg load(as we skip the groups at or below
3581 * its cpu_power, while calculating max_load..)
3582 */
3583 if (max_load < avg_load) {
3584 *imbalance = 0;
3585 goto small_imbalance;
3586 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003587
3588 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003589 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003592 *imbalance = min(max_pull * busiest->__cpu_power,
3593 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 / SCHED_LOAD_SCALE;
3595
Peter Williams2dd73a42006-06-27 02:54:34 -07003596 /*
3597 * if *imbalance is less than the average load per runnable task
3598 * there is no gaurantee that any tasks will be moved so we'll have
3599 * a think about bumping its value to force at least one task to be
3600 * moved
3601 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003602 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003603 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003604 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Peter Williams2dd73a42006-06-27 02:54:34 -07003606small_imbalance:
3607 pwr_move = pwr_now = 0;
3608 imbn = 2;
3609 if (this_nr_running) {
3610 this_load_per_task /= this_nr_running;
3611 if (busiest_load_per_task > this_load_per_task)
3612 imbn = 1;
3613 } else
3614 this_load_per_task = SCHED_LOAD_SCALE;
3615
Ingo Molnardd41f592007-07-09 18:51:59 +02003616 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3617 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003618 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 return busiest;
3620 }
3621
3622 /*
3623 * OK, we don't have enough imbalance to justify moving tasks,
3624 * however we may be able to increase total CPU power used by
3625 * moving them.
3626 */
3627
Eric Dumazet5517d862007-05-08 00:32:57 -07003628 pwr_now += busiest->__cpu_power *
3629 min(busiest_load_per_task, max_load);
3630 pwr_now += this->__cpu_power *
3631 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 pwr_now /= SCHED_LOAD_SCALE;
3633
3634 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003635 tmp = sg_div_cpu_power(busiest,
3636 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003638 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003639 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003642 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003643 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003644 tmp = sg_div_cpu_power(this,
3645 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003647 tmp = sg_div_cpu_power(this,
3648 busiest_load_per_task * SCHED_LOAD_SCALE);
3649 pwr_move += this->__cpu_power *
3650 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 pwr_move /= SCHED_LOAD_SCALE;
3652
3653 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003654 if (pwr_move > pwr_now)
3655 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 }
3657
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 return busiest;
3659
3660out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003661#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003662 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003663 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003665 if (this == group_leader && group_leader != group_min) {
3666 *imbalance = min_load_per_task;
3667 return group_min;
3668 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003669#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003670ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 *imbalance = 0;
3672 return NULL;
3673}
3674
3675/*
3676 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3677 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003678static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003679find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003680 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003682 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003683 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 int i;
3685
3686 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003687 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003688
3689 if (!cpu_isset(i, *cpus))
3690 continue;
3691
Ingo Molnar48f24c42006-07-03 00:25:40 -07003692 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003693 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
Ingo Molnardd41f592007-07-09 18:51:59 +02003695 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003696 continue;
3697
Ingo Molnardd41f592007-07-09 18:51:59 +02003698 if (wl > max_load) {
3699 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003700 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 }
3702 }
3703
3704 return busiest;
3705}
3706
3707/*
Nick Piggin77391d72005-06-25 14:57:30 -07003708 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3709 * so long as it is large enough.
3710 */
3711#define MAX_PINNED_INTERVAL 512
3712
3713/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3715 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003717static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003718 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003719 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720{
Peter Williams43010652007-08-09 11:16:46 +02003721 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003724 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003725 unsigned long flags;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003726 int unlock_aggregate;
Nick Piggin5969fe02005-09-10 00:26:19 -07003727
Mike Travis7c16ec52008-04-04 18:11:11 -07003728 cpus_setall(*cpus);
3729
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003730 unlock_aggregate = get_aggregate(sd);
3731
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003732 /*
3733 * When power savings policy is enabled for the parent domain, idle
3734 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003735 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003736 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003737 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003738 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003739 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003740 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
Ingo Molnar2d723762007-10-15 17:00:12 +02003742 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003744redo:
3745 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003746 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003747
Chen, Kenneth W06066712006-12-10 02:20:35 -08003748 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003749 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 if (!group) {
3752 schedstat_inc(sd, lb_nobusyg[idle]);
3753 goto out_balanced;
3754 }
3755
Mike Travis7c16ec52008-04-04 18:11:11 -07003756 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 if (!busiest) {
3758 schedstat_inc(sd, lb_nobusyq[idle]);
3759 goto out_balanced;
3760 }
3761
Nick Piggindb935db2005-06-25 14:57:11 -07003762 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
3764 schedstat_add(sd, lb_imbalance[idle], imbalance);
3765
Peter Williams43010652007-08-09 11:16:46 +02003766 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 if (busiest->nr_running > 1) {
3768 /*
3769 * Attempt to move tasks. If find_busiest_group has found
3770 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003771 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 * correctly treated as an imbalance.
3773 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003774 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003775 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003776 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003777 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003778 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003779 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003780
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003781 /*
3782 * some other cpu did the load balance for us.
3783 */
Peter Williams43010652007-08-09 11:16:46 +02003784 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003785 resched_cpu(this_cpu);
3786
Nick Piggin81026792005-06-25 14:57:07 -07003787 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003788 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003789 cpu_clear(cpu_of(busiest), *cpus);
3790 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003791 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003792 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 }
Nick Piggin81026792005-06-25 14:57:07 -07003795
Peter Williams43010652007-08-09 11:16:46 +02003796 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 schedstat_inc(sd, lb_failed[idle]);
3798 sd->nr_balance_failed++;
3799
3800 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003802 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003803
3804 /* don't kick the migration_thread, if the curr
3805 * task on busiest cpu can't be moved to this_cpu
3806 */
3807 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003808 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003809 all_pinned = 1;
3810 goto out_one_pinned;
3811 }
3812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 if (!busiest->active_balance) {
3814 busiest->active_balance = 1;
3815 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003816 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003818 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003819 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 wake_up_process(busiest->migration_thread);
3821
3822 /*
3823 * We've kicked active balancing, reset the failure
3824 * counter.
3825 */
Nick Piggin39507452005-06-25 14:57:09 -07003826 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 }
Nick Piggin81026792005-06-25 14:57:07 -07003828 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 sd->nr_balance_failed = 0;
3830
Nick Piggin81026792005-06-25 14:57:07 -07003831 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 /* We were unbalanced, so reset the balancing interval */
3833 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003834 } else {
3835 /*
3836 * If we've begun active balancing, start to back off. This
3837 * case may not be covered by the all_pinned logic if there
3838 * is only 1 task on the busy runqueue (because we don't call
3839 * move_tasks).
3840 */
3841 if (sd->balance_interval < sd->max_interval)
3842 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 }
3844
Peter Williams43010652007-08-09 11:16:46 +02003845 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003846 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003847 ld_moved = -1;
3848
3849 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
3851out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 schedstat_inc(sd, lb_balanced[idle]);
3853
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003854 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003855
3856out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003858 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3859 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 sd->balance_interval *= 2;
3861
Ingo Molnar48f24c42006-07-03 00:25:40 -07003862 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003863 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003864 ld_moved = -1;
3865 else
3866 ld_moved = 0;
3867out:
3868 if (unlock_aggregate)
3869 put_aggregate(sd);
3870 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871}
3872
3873/*
3874 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3875 * tasks if there is an imbalance.
3876 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003877 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 * this_rq is locked.
3879 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003880static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003881load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3882 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
3884 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003885 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003887 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003888 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003889 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003890
3891 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003892
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003893 /*
3894 * When power savings policy is enabled for the parent domain, idle
3895 * sibling can pick up load irrespective of busy siblings. In this case,
3896 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003897 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003898 */
3899 if (sd->flags & SD_SHARE_CPUPOWER &&
3900 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003901 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
Ingo Molnar2d723762007-10-15 17:00:12 +02003903 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003904redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003905 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003906 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003908 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003909 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
3911
Mike Travis7c16ec52008-04-04 18:11:11 -07003912 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003913 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003914 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003915 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 }
3917
Nick Piggindb935db2005-06-25 14:57:11 -07003918 BUG_ON(busiest == this_rq);
3919
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003920 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003921
Peter Williams43010652007-08-09 11:16:46 +02003922 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003923 if (busiest->nr_running > 1) {
3924 /* Attempt to move tasks */
3925 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003926 /* this_rq->clock is already updated */
3927 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003928 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003929 imbalance, sd, CPU_NEWLY_IDLE,
3930 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003931 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003932
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003933 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003934 cpu_clear(cpu_of(busiest), *cpus);
3935 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003936 goto redo;
3937 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003938 }
3939
Peter Williams43010652007-08-09 11:16:46 +02003940 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003941 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003942 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3943 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003944 return -1;
3945 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003946 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
Peter Williams43010652007-08-09 11:16:46 +02003948 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003949
3950out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003951 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003952 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003953 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003954 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003955 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003956
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003957 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
3960/*
3961 * idle_balance is called by schedule() if this_cpu is about to become
3962 * idle. Attempts to pull tasks from other CPUs.
3963 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003964static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
3966 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003967 int pulled_task = -1;
3968 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003969 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003972 unsigned long interval;
3973
3974 if (!(sd->flags & SD_LOAD_BALANCE))
3975 continue;
3976
3977 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003978 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003979 pulled_task = load_balance_newidle(this_cpu, this_rq,
3980 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003981
3982 interval = msecs_to_jiffies(sd->balance_interval);
3983 if (time_after(next_balance, sd->last_balance + interval))
3984 next_balance = sd->last_balance + interval;
3985 if (pulled_task)
3986 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003988 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003989 /*
3990 * We are going idle. next_balance may be set based on
3991 * a busy processor. So reset next_balance.
3992 */
3993 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995}
3996
3997/*
3998 * active_load_balance is run by migration threads. It pushes running tasks
3999 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
4000 * running on each physical CPU where possible, and avoids physical /
4001 * logical imbalances.
4002 *
4003 * Called with busiest_rq locked.
4004 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004005static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
Nick Piggin39507452005-06-25 14:57:09 -07004007 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004008 struct sched_domain *sd;
4009 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07004010
Ingo Molnar48f24c42006-07-03 00:25:40 -07004011 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07004012 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07004013 return;
4014
4015 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
4017 /*
Nick Piggin39507452005-06-25 14:57:09 -07004018 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004019 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07004020 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 */
Nick Piggin39507452005-06-25 14:57:09 -07004022 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Nick Piggin39507452005-06-25 14:57:09 -07004024 /* move a task from busiest_rq to target_rq */
4025 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02004026 update_rq_clock(busiest_rq);
4027 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
Nick Piggin39507452005-06-25 14:57:09 -07004029 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07004030 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07004031 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004032 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07004033 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07004034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Ingo Molnar48f24c42006-07-03 00:25:40 -07004036 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004037 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Peter Williams43010652007-08-09 11:16:46 +02004039 if (move_one_task(target_rq, target_cpu, busiest_rq,
4040 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07004041 schedstat_inc(sd, alb_pushed);
4042 else
4043 schedstat_inc(sd, alb_failed);
4044 }
Nick Piggin39507452005-06-25 14:57:09 -07004045 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046}
4047
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004048#ifdef CONFIG_NO_HZ
4049static struct {
4050 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004051 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004052} nohz ____cacheline_aligned = {
4053 .load_balancer = ATOMIC_INIT(-1),
4054 .cpu_mask = CPU_MASK_NONE,
4055};
4056
Christoph Lameter7835b982006-12-10 02:20:22 -08004057/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004058 * This routine will try to nominate the ilb (idle load balancing)
4059 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
4060 * load balancing on behalf of all those cpus. If all the cpus in the system
4061 * go into this tickless mode, then there will be no ilb owner (as there is
4062 * no need for one) and all the cpus will sleep till the next wakeup event
4063 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08004064 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004065 * For the ilb owner, tick is not stopped. And this tick will be used
4066 * for idle load balancing. ilb owner will still be part of
4067 * nohz.cpu_mask..
4068 *
4069 * While stopping the tick, this cpu will become the ilb owner if there
4070 * is no other owner. And will be the owner till that cpu becomes busy
4071 * or if all cpus in the system stop their ticks at which point
4072 * there is no need for ilb owner.
4073 *
4074 * When the ilb owner becomes busy, it nominates another owner, during the
4075 * next busy scheduler_tick()
4076 */
4077int select_nohz_load_balancer(int stop_tick)
4078{
4079 int cpu = smp_processor_id();
4080
4081 if (stop_tick) {
4082 cpu_set(cpu, nohz.cpu_mask);
4083 cpu_rq(cpu)->in_nohz_recently = 1;
4084
4085 /*
4086 * If we are going offline and still the leader, give up!
4087 */
4088 if (cpu_is_offline(cpu) &&
4089 atomic_read(&nohz.load_balancer) == cpu) {
4090 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4091 BUG();
4092 return 0;
4093 }
4094
4095 /* time for ilb owner also to sleep */
4096 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4097 if (atomic_read(&nohz.load_balancer) == cpu)
4098 atomic_set(&nohz.load_balancer, -1);
4099 return 0;
4100 }
4101
4102 if (atomic_read(&nohz.load_balancer) == -1) {
4103 /* make me the ilb owner */
4104 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4105 return 1;
4106 } else if (atomic_read(&nohz.load_balancer) == cpu)
4107 return 1;
4108 } else {
4109 if (!cpu_isset(cpu, nohz.cpu_mask))
4110 return 0;
4111
4112 cpu_clear(cpu, nohz.cpu_mask);
4113
4114 if (atomic_read(&nohz.load_balancer) == cpu)
4115 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4116 BUG();
4117 }
4118 return 0;
4119}
4120#endif
4121
4122static DEFINE_SPINLOCK(balancing);
4123
4124/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004125 * It checks each scheduling domain to see if it is due to be balanced,
4126 * and initiates a balancing operation if so.
4127 *
4128 * Balancing parameters are set up in arch_init_sched_domains.
4129 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004130static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08004131{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004132 int balance = 1;
4133 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004134 unsigned long interval;
4135 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004136 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004137 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004138 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07004139 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004141 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 if (!(sd->flags & SD_LOAD_BALANCE))
4143 continue;
4144
4145 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004146 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 interval *= sd->busy_factor;
4148
4149 /* scale ms to jiffies */
4150 interval = msecs_to_jiffies(interval);
4151 if (unlikely(!interval))
4152 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004153 if (interval > HZ*NR_CPUS/10)
4154 interval = HZ*NR_CPUS/10;
4155
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Christoph Lameter08c183f2006-12-10 02:20:29 -08004157 if (sd->flags & SD_SERIALIZE) {
4158 if (!spin_trylock(&balancing))
4159 goto out;
4160 }
4161
Christoph Lameterc9819f42006-12-10 02:20:25 -08004162 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07004163 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004164 /*
4165 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004166 * longer idle, or one of our SMT siblings is
4167 * not idle.
4168 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004169 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004171 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08004173 if (sd->flags & SD_SERIALIZE)
4174 spin_unlock(&balancing);
4175out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004176 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004177 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004178 update_next_balance = 1;
4179 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004180
4181 /*
4182 * Stop the load balance at this level. There is another
4183 * CPU in our sched group which is doing load balancing more
4184 * actively.
4185 */
4186 if (!balance)
4187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004189
4190 /*
4191 * next_balance will be updated only when there is a need.
4192 * When the cpu is attached to null domain for ex, it will not be
4193 * updated.
4194 */
4195 if (likely(update_next_balance))
4196 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004197}
4198
4199/*
4200 * run_rebalance_domains is triggered when needed from the scheduler tick.
4201 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4202 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4203 */
4204static void run_rebalance_domains(struct softirq_action *h)
4205{
Ingo Molnardd41f592007-07-09 18:51:59 +02004206 int this_cpu = smp_processor_id();
4207 struct rq *this_rq = cpu_rq(this_cpu);
4208 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4209 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004210
Ingo Molnardd41f592007-07-09 18:51:59 +02004211 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004212
4213#ifdef CONFIG_NO_HZ
4214 /*
4215 * If this cpu is the owner for idle load balancing, then do the
4216 * balancing on behalf of the other idle cpus whose ticks are
4217 * stopped.
4218 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004219 if (this_rq->idle_at_tick &&
4220 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004221 cpumask_t cpus = nohz.cpu_mask;
4222 struct rq *rq;
4223 int balance_cpu;
4224
Ingo Molnardd41f592007-07-09 18:51:59 +02004225 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004226 for_each_cpu_mask(balance_cpu, cpus) {
4227 /*
4228 * If this cpu gets work to do, stop the load balancing
4229 * work being done for other cpus. Next load
4230 * balancing owner will pick it up.
4231 */
4232 if (need_resched())
4233 break;
4234
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004235 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004236
4237 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004238 if (time_after(this_rq->next_balance, rq->next_balance))
4239 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004240 }
4241 }
4242#endif
4243}
4244
4245/*
4246 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4247 *
4248 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4249 * idle load balancing owner or decide to stop the periodic load balancing,
4250 * if the whole system is idle.
4251 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004252static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004253{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004254#ifdef CONFIG_NO_HZ
4255 /*
4256 * If we were in the nohz mode recently and busy at the current
4257 * scheduler tick, then check if we need to nominate new idle
4258 * load balancer.
4259 */
4260 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4261 rq->in_nohz_recently = 0;
4262
4263 if (atomic_read(&nohz.load_balancer) == cpu) {
4264 cpu_clear(cpu, nohz.cpu_mask);
4265 atomic_set(&nohz.load_balancer, -1);
4266 }
4267
4268 if (atomic_read(&nohz.load_balancer) == -1) {
4269 /*
4270 * simple selection for now: Nominate the
4271 * first cpu in the nohz list to be the next
4272 * ilb owner.
4273 *
4274 * TBD: Traverse the sched domains and nominate
4275 * the nearest cpu in the nohz.cpu_mask.
4276 */
4277 int ilb = first_cpu(nohz.cpu_mask);
4278
Mike Travis434d53b2008-04-04 18:11:04 -07004279 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004280 resched_cpu(ilb);
4281 }
4282 }
4283
4284 /*
4285 * If this cpu is idle and doing idle load balancing for all the
4286 * cpus with ticks stopped, is it time for that to stop?
4287 */
4288 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4289 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4290 resched_cpu(cpu);
4291 return;
4292 }
4293
4294 /*
4295 * If this cpu is idle and the idle load balancing is done by
4296 * someone else, then no need raise the SCHED_SOFTIRQ
4297 */
4298 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4299 cpu_isset(cpu, nohz.cpu_mask))
4300 return;
4301#endif
4302 if (time_after_eq(jiffies, rq->next_balance))
4303 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304}
Ingo Molnardd41f592007-07-09 18:51:59 +02004305
4306#else /* CONFIG_SMP */
4307
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308/*
4309 * on UP we do not need to balance between CPUs:
4310 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004311static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312{
4313}
Ingo Molnardd41f592007-07-09 18:51:59 +02004314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315#endif
4316
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317DEFINE_PER_CPU(struct kernel_stat, kstat);
4318
4319EXPORT_PER_CPU_SYMBOL(kstat);
4320
4321/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004322 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4323 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004325unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004328 u64 ns, delta_exec;
4329 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004330
Ingo Molnar41b86e92007-07-09 18:51:58 +02004331 rq = task_rq_lock(p, &flags);
4332 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004333 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004334 update_rq_clock(rq);
4335 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004336 if ((s64)delta_exec > 0)
4337 ns += delta_exec;
4338 }
4339 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 return ns;
4342}
4343
4344/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 * Account user cpu time to a process.
4346 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 * @cputime: the cpu time spent in user space since the last update
4348 */
4349void account_user_time(struct task_struct *p, cputime_t cputime)
4350{
4351 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4352 cputime64_t tmp;
4353
4354 p->utime = cputime_add(p->utime, cputime);
4355
4356 /* Add user time to cpustat. */
4357 tmp = cputime_to_cputime64(cputime);
4358 if (TASK_NICE(p) > 0)
4359 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4360 else
4361 cpustat->user = cputime64_add(cpustat->user, tmp);
4362}
4363
4364/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004365 * Account guest cpu time to a process.
4366 * @p: the process that the cpu time gets accounted to
4367 * @cputime: the cpu time spent in virtual machine since the last update
4368 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004369static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004370{
4371 cputime64_t tmp;
4372 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4373
4374 tmp = cputime_to_cputime64(cputime);
4375
4376 p->utime = cputime_add(p->utime, cputime);
4377 p->gtime = cputime_add(p->gtime, cputime);
4378
4379 cpustat->user = cputime64_add(cpustat->user, tmp);
4380 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4381}
4382
4383/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004384 * Account scaled user cpu time to a process.
4385 * @p: the process that the cpu time gets accounted to
4386 * @cputime: the cpu time spent in user space since the last update
4387 */
4388void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4389{
4390 p->utimescaled = cputime_add(p->utimescaled, cputime);
4391}
4392
4393/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 * Account system cpu time to a process.
4395 * @p: the process that the cpu time gets accounted to
4396 * @hardirq_offset: the offset to subtract from hardirq_count()
4397 * @cputime: the cpu time spent in kernel space since the last update
4398 */
4399void account_system_time(struct task_struct *p, int hardirq_offset,
4400 cputime_t cputime)
4401{
4402 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004403 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 cputime64_t tmp;
4405
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004406 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4407 account_guest_time(p, cputime);
4408 return;
4409 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 p->stime = cputime_add(p->stime, cputime);
4412
4413 /* Add system time to cpustat. */
4414 tmp = cputime_to_cputime64(cputime);
4415 if (hardirq_count() - hardirq_offset)
4416 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4417 else if (softirq_count())
4418 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004419 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004421 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4423 else
4424 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4425 /* Account for system time used */
4426 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
4429/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004430 * Account scaled system cpu time to a process.
4431 * @p: the process that the cpu time gets accounted to
4432 * @hardirq_offset: the offset to subtract from hardirq_count()
4433 * @cputime: the cpu time spent in kernel space since the last update
4434 */
4435void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4436{
4437 p->stimescaled = cputime_add(p->stimescaled, cputime);
4438}
4439
4440/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 * Account for involuntary wait time.
4442 * @p: the process from which the cpu time has been stolen
4443 * @steal: the cpu time spent in involuntary wait
4444 */
4445void account_steal_time(struct task_struct *p, cputime_t steal)
4446{
4447 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4448 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004449 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
4451 if (p == rq->idle) {
4452 p->stime = cputime_add(p->stime, steal);
4453 if (atomic_read(&rq->nr_iowait) > 0)
4454 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4455 else
4456 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004457 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4459}
4460
Christoph Lameter7835b982006-12-10 02:20:22 -08004461/*
4462 * This function gets called by the timer code, with HZ frequency.
4463 * We call it with interrupts disabled.
4464 *
4465 * It also gets called by the fork code, when changing the parent's
4466 * timeslices.
4467 */
4468void scheduler_tick(void)
4469{
Christoph Lameter7835b982006-12-10 02:20:22 -08004470 int cpu = smp_processor_id();
4471 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004472 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02004473 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08004474
Ingo Molnardd41f592007-07-09 18:51:59 +02004475 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02004476 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02004477 /*
4478 * Let rq->clock advance by at least TICK_NSEC:
4479 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004480 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02004481 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004482 rq->clock_underflows++;
4483 }
Ingo Molnar529c7722007-08-10 23:05:11 +02004484 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02004485 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004486 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004487 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004488 spin_unlock(&rq->lock);
4489
Christoph Lametere418e1c2006-12-10 02:20:23 -08004490#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004491 rq->idle_at_tick = idle_cpu(cpu);
4492 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004493#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494}
4495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
4497
Srinivasa Ds43627582008-02-23 15:24:04 -08004498void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499{
4500 /*
4501 * Underflow?
4502 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004503 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4504 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 preempt_count() += val;
4506 /*
4507 * Spinlock count overflowing soon?
4508 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004509 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4510 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511}
4512EXPORT_SYMBOL(add_preempt_count);
4513
Srinivasa Ds43627582008-02-23 15:24:04 -08004514void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515{
4516 /*
4517 * Underflow?
4518 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004519 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4520 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 /*
4522 * Is the spinlock portion underflowing?
4523 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004524 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4525 !(preempt_count() & PREEMPT_MASK)))
4526 return;
4527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 preempt_count() -= val;
4529}
4530EXPORT_SYMBOL(sub_preempt_count);
4531
4532#endif
4533
4534/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004535 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004537static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538{
Satyam Sharma838225b2007-10-24 18:23:50 +02004539 struct pt_regs *regs = get_irq_regs();
4540
4541 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4542 prev->comm, prev->pid, preempt_count());
4543
Ingo Molnardd41f592007-07-09 18:51:59 +02004544 debug_show_held_locks(prev);
4545 if (irqs_disabled())
4546 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004547
4548 if (regs)
4549 show_regs(regs);
4550 else
4551 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004552}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
Ingo Molnardd41f592007-07-09 18:51:59 +02004554/*
4555 * Various schedule()-time debugging checks and statistics:
4556 */
4557static inline void schedule_debug(struct task_struct *prev)
4558{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004560 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 * schedule() atomically, we ignore that path for now.
4562 * Otherwise, whine if we are scheduling when we should not be.
4563 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004564 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4565 __schedule_bug(prev);
4566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4568
Ingo Molnar2d723762007-10-15 17:00:12 +02004569 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004570#ifdef CONFIG_SCHEDSTATS
4571 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004572 schedstat_inc(this_rq(), bkl_count);
4573 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004574 }
4575#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004576}
4577
4578/*
4579 * Pick up the highest-prio task:
4580 */
4581static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004582pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004583{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004584 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004585 struct task_struct *p;
4586
4587 /*
4588 * Optimization: we know that if all tasks are in
4589 * the fair class we can call that function directly:
4590 */
4591 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004592 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004593 if (likely(p))
4594 return p;
4595 }
4596
4597 class = sched_class_highest;
4598 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004599 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004600 if (p)
4601 return p;
4602 /*
4603 * Will never be NULL as the idle class always
4604 * returns a non-NULL p:
4605 */
4606 class = class->next;
4607 }
4608}
4609
4610/*
4611 * schedule() is the main scheduler function.
4612 */
4613asmlinkage void __sched schedule(void)
4614{
4615 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004616 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004617 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004618 int cpu;
4619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620need_resched:
4621 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004622 cpu = smp_processor_id();
4623 rq = cpu_rq(cpu);
4624 rcu_qsctr_inc(cpu);
4625 prev = rq->curr;
4626 switch_count = &prev->nivcsw;
4627
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 release_kernel_lock(prev);
4629need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
Ingo Molnardd41f592007-07-09 18:51:59 +02004631 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004633 hrtick_clear(rq);
4634
Ingo Molnar1e819952007-10-15 17:00:13 +02004635 /*
4636 * Do the rq-clock update outside the rq lock:
4637 */
4638 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004639 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004640 spin_lock(&rq->lock);
4641 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Ingo Molnardd41f592007-07-09 18:51:59 +02004643 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4644 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004645 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004646 prev->state = TASK_RUNNING;
4647 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004648 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004649 }
4650 switch_count = &prev->nvcsw;
4651 }
4652
Steven Rostedt9a897c52008-01-25 21:08:22 +01004653#ifdef CONFIG_SMP
4654 if (prev->sched_class->pre_schedule)
4655 prev->sched_class->pre_schedule(rq, prev);
4656#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004657
Ingo Molnardd41f592007-07-09 18:51:59 +02004658 if (unlikely(!rq->nr_running))
4659 idle_balance(cpu, rq);
4660
Ingo Molnar31ee5292007-08-09 11:16:49 +02004661 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004662 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01004665 sched_info_switch(prev, next);
4666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 rq->nr_switches++;
4668 rq->curr = next;
4669 ++*switch_count;
4670
Ingo Molnardd41f592007-07-09 18:51:59 +02004671 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004672 /*
4673 * the context switch might have flipped the stack from under
4674 * us, hence refresh the local variables.
4675 */
4676 cpu = smp_processor_id();
4677 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 } else
4679 spin_unlock_irq(&rq->lock);
4680
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004681 hrtick_set(rq);
4682
4683 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 preempt_enable_no_resched();
4687 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4688 goto need_resched;
4689}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690EXPORT_SYMBOL(schedule);
4691
4692#ifdef CONFIG_PREEMPT
4693/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004694 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004695 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 * occur there and call schedule directly.
4697 */
4698asmlinkage void __sched preempt_schedule(void)
4699{
4700 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 struct task_struct *task = current;
4702 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004703
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 /*
4705 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004706 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004708 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 return;
4710
Andi Kleen3a5c3592007-10-15 17:00:14 +02004711 do {
4712 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Andi Kleen3a5c3592007-10-15 17:00:14 +02004714 /*
4715 * We keep the big kernel semaphore locked, but we
4716 * clear ->lock_depth so that schedule() doesnt
4717 * auto-release the semaphore:
4718 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004719 saved_lock_depth = task->lock_depth;
4720 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004721 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004722 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004723 sub_preempt_count(PREEMPT_ACTIVE);
4724
4725 /*
4726 * Check again in case we missed a preemption opportunity
4727 * between schedule and now.
4728 */
4729 barrier();
4730 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732EXPORT_SYMBOL(preempt_schedule);
4733
4734/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004735 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 * off of irq context.
4737 * Note, that this is called and return with irqs disabled. This will
4738 * protect us against recursive calling from irq.
4739 */
4740asmlinkage void __sched preempt_schedule_irq(void)
4741{
4742 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 struct task_struct *task = current;
4744 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004745
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004746 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 BUG_ON(ti->preempt_count || !irqs_disabled());
4748
Andi Kleen3a5c3592007-10-15 17:00:14 +02004749 do {
4750 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Andi Kleen3a5c3592007-10-15 17:00:14 +02004752 /*
4753 * We keep the big kernel semaphore locked, but we
4754 * clear ->lock_depth so that schedule() doesnt
4755 * auto-release the semaphore:
4756 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004757 saved_lock_depth = task->lock_depth;
4758 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004759 local_irq_enable();
4760 schedule();
4761 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004762 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004763 sub_preempt_count(PREEMPT_ACTIVE);
4764
4765 /*
4766 * Check again in case we missed a preemption opportunity
4767 * between schedule and now.
4768 */
4769 barrier();
4770 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771}
4772
4773#endif /* CONFIG_PREEMPT */
4774
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004775int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4776 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004778 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780EXPORT_SYMBOL(default_wake_function);
4781
4782/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004783 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4784 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 * number) then we wake all the non-exclusive tasks and one exclusive task.
4786 *
4787 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004788 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4790 */
4791static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4792 int nr_exclusive, int sync, void *key)
4793{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004794 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004796 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004797 unsigned flags = curr->flags;
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004800 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 break;
4802 }
4803}
4804
4805/**
4806 * __wake_up - wake up threads blocked on a waitqueue.
4807 * @q: the waitqueue
4808 * @mode: which threads
4809 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004810 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004812void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004813 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814{
4815 unsigned long flags;
4816
4817 spin_lock_irqsave(&q->lock, flags);
4818 __wake_up_common(q, mode, nr_exclusive, 0, key);
4819 spin_unlock_irqrestore(&q->lock, flags);
4820}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821EXPORT_SYMBOL(__wake_up);
4822
4823/*
4824 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4825 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004826void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827{
4828 __wake_up_common(q, mode, 1, 0, NULL);
4829}
4830
4831/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004832 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 * @q: the waitqueue
4834 * @mode: which threads
4835 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4836 *
4837 * The sync wakeup differs that the waker knows that it will schedule
4838 * away soon, so while the target thread will be woken up, it will not
4839 * be migrated to another CPU - ie. the two threads are 'synchronized'
4840 * with each other. This can prevent needless bouncing between CPUs.
4841 *
4842 * On UP it can prevent extra preemption.
4843 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004844void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004845__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846{
4847 unsigned long flags;
4848 int sync = 1;
4849
4850 if (unlikely(!q))
4851 return;
4852
4853 if (unlikely(!nr_exclusive))
4854 sync = 0;
4855
4856 spin_lock_irqsave(&q->lock, flags);
4857 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4858 spin_unlock_irqrestore(&q->lock, flags);
4859}
4860EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4861
Ingo Molnarb15136e2007-10-24 18:23:48 +02004862void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863{
4864 unsigned long flags;
4865
4866 spin_lock_irqsave(&x->wait.lock, flags);
4867 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004868 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 spin_unlock_irqrestore(&x->wait.lock, flags);
4870}
4871EXPORT_SYMBOL(complete);
4872
Ingo Molnarb15136e2007-10-24 18:23:48 +02004873void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
4875 unsigned long flags;
4876
4877 spin_lock_irqsave(&x->wait.lock, flags);
4878 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004879 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 spin_unlock_irqrestore(&x->wait.lock, flags);
4881}
4882EXPORT_SYMBOL(complete_all);
4883
Andi Kleen8cbbe862007-10-15 17:00:14 +02004884static inline long __sched
4885do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 if (!x->done) {
4888 DECLARE_WAITQUEUE(wait, current);
4889
4890 wait.flags |= WQ_FLAG_EXCLUSIVE;
4891 __add_wait_queue_tail(&x->wait, &wait);
4892 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004893 if ((state == TASK_INTERRUPTIBLE &&
4894 signal_pending(current)) ||
4895 (state == TASK_KILLABLE &&
4896 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004897 __remove_wait_queue(&x->wait, &wait);
4898 return -ERESTARTSYS;
4899 }
4900 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004902 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004904 if (!timeout) {
4905 __remove_wait_queue(&x->wait, &wait);
4906 return timeout;
4907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 } while (!x->done);
4909 __remove_wait_queue(&x->wait, &wait);
4910 }
4911 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004912 return timeout;
4913}
4914
4915static long __sched
4916wait_for_common(struct completion *x, long timeout, int state)
4917{
4918 might_sleep();
4919
4920 spin_lock_irq(&x->wait.lock);
4921 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004923 return timeout;
4924}
4925
Ingo Molnarb15136e2007-10-24 18:23:48 +02004926void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004927{
4928 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929}
4930EXPORT_SYMBOL(wait_for_completion);
4931
Ingo Molnarb15136e2007-10-24 18:23:48 +02004932unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4934{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004935 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936}
4937EXPORT_SYMBOL(wait_for_completion_timeout);
4938
Andi Kleen8cbbe862007-10-15 17:00:14 +02004939int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940{
Andi Kleen51e97992007-10-18 21:32:55 +02004941 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4942 if (t == -ERESTARTSYS)
4943 return t;
4944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945}
4946EXPORT_SYMBOL(wait_for_completion_interruptible);
4947
Ingo Molnarb15136e2007-10-24 18:23:48 +02004948unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949wait_for_completion_interruptible_timeout(struct completion *x,
4950 unsigned long timeout)
4951{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004952 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953}
4954EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4955
Matthew Wilcox009e5772007-12-06 12:29:54 -05004956int __sched wait_for_completion_killable(struct completion *x)
4957{
4958 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4959 if (t == -ERESTARTSYS)
4960 return t;
4961 return 0;
4962}
4963EXPORT_SYMBOL(wait_for_completion_killable);
4964
Andi Kleen8cbbe862007-10-15 17:00:14 +02004965static long __sched
4966sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004967{
4968 unsigned long flags;
4969 wait_queue_t wait;
4970
4971 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Andi Kleen8cbbe862007-10-15 17:00:14 +02004973 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
Andi Kleen8cbbe862007-10-15 17:00:14 +02004975 spin_lock_irqsave(&q->lock, flags);
4976 __add_wait_queue(q, &wait);
4977 spin_unlock(&q->lock);
4978 timeout = schedule_timeout(timeout);
4979 spin_lock_irq(&q->lock);
4980 __remove_wait_queue(q, &wait);
4981 spin_unlock_irqrestore(&q->lock, flags);
4982
4983 return timeout;
4984}
4985
4986void __sched interruptible_sleep_on(wait_queue_head_t *q)
4987{
4988 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990EXPORT_SYMBOL(interruptible_sleep_on);
4991
Ingo Molnar0fec1712007-07-09 18:52:01 +02004992long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004993interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004995 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4998
Ingo Molnar0fec1712007-07-09 18:52:01 +02004999void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005001 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003EXPORT_SYMBOL(sleep_on);
5004
Ingo Molnar0fec1712007-07-09 18:52:01 +02005005long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005007 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009EXPORT_SYMBOL(sleep_on_timeout);
5010
Ingo Molnarb29739f2006-06-27 02:54:51 -07005011#ifdef CONFIG_RT_MUTEXES
5012
5013/*
5014 * rt_mutex_setprio - set the current priority of a task
5015 * @p: task
5016 * @prio: prio value (kernel-internal form)
5017 *
5018 * This function changes the 'effective' priority of a task. It does
5019 * not touch ->normal_prio like __setscheduler().
5020 *
5021 * Used by the rt_mutex code to implement priority inheritance logic.
5022 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005023void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07005024{
5025 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005026 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005027 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01005028 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005029
5030 BUG_ON(prio < 0 || prio > MAX_PRIO);
5031
5032 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005033 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005034
Andrew Mortond5f9f942007-05-08 20:27:06 -07005035 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005036 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005037 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005038 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005039 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005040 if (running)
5041 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02005042
5043 if (rt_prio(prio))
5044 p->sched_class = &rt_sched_class;
5045 else
5046 p->sched_class = &fair_sched_class;
5047
Ingo Molnarb29739f2006-06-27 02:54:51 -07005048 p->prio = prio;
5049
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005050 if (running)
5051 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005052 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005053 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005054
5055 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005056 }
5057 task_rq_unlock(rq, &flags);
5058}
5059
5060#endif
5061
Ingo Molnar36c8b582006-07-03 00:25:41 -07005062void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063{
Ingo Molnardd41f592007-07-09 18:51:59 +02005064 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005066 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5069 return;
5070 /*
5071 * We have to be careful, if called from sys_setpriority(),
5072 * the task might be in the middle of scheduling on another CPU.
5073 */
5074 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005075 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 /*
5077 * The RT priorities are set via sched_setscheduler(), but we still
5078 * allow the 'normal' nice value to be set - but as expected
5079 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02005080 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 */
Ingo Molnare05606d2007-07-09 18:51:59 +02005082 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 p->static_prio = NICE_TO_PRIO(nice);
5084 goto out_unlock;
5085 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005086 on_rq = p->se.on_rq;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02005087 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005088 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07005091 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005092 old_prio = p->prio;
5093 p->prio = effective_prio(p);
5094 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Ingo Molnardd41f592007-07-09 18:51:59 +02005096 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005097 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07005099 * If the task increased its priority or is running and
5100 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005102 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 resched_task(rq->curr);
5104 }
5105out_unlock:
5106 task_rq_unlock(rq, &flags);
5107}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108EXPORT_SYMBOL(set_user_nice);
5109
Matt Mackalle43379f2005-05-01 08:59:00 -07005110/*
5111 * can_nice - check if a task can reduce its nice value
5112 * @p: task
5113 * @nice: nice value
5114 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005115int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005116{
Matt Mackall024f4742005-08-18 11:24:19 -07005117 /* convert nice value [19,-20] to rlimit style value [1,40] */
5118 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005119
Matt Mackalle43379f2005-05-01 08:59:00 -07005120 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5121 capable(CAP_SYS_NICE));
5122}
5123
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124#ifdef __ARCH_WANT_SYS_NICE
5125
5126/*
5127 * sys_nice - change the priority of the current process.
5128 * @increment: priority increment
5129 *
5130 * sys_setpriority is a more generic, but much slower function that
5131 * does similar things.
5132 */
5133asmlinkage long sys_nice(int increment)
5134{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005135 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137 /*
5138 * Setpriority might change our priority at the same moment.
5139 * We don't have to worry. Conceptually one call occurs first
5140 * and we have a single winner.
5141 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005142 if (increment < -40)
5143 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 if (increment > 40)
5145 increment = 40;
5146
5147 nice = PRIO_TO_NICE(current->static_prio) + increment;
5148 if (nice < -20)
5149 nice = -20;
5150 if (nice > 19)
5151 nice = 19;
5152
Matt Mackalle43379f2005-05-01 08:59:00 -07005153 if (increment < 0 && !can_nice(current, nice))
5154 return -EPERM;
5155
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 retval = security_task_setnice(current, nice);
5157 if (retval)
5158 return retval;
5159
5160 set_user_nice(current, nice);
5161 return 0;
5162}
5163
5164#endif
5165
5166/**
5167 * task_prio - return the priority value of a given task.
5168 * @p: the task in question.
5169 *
5170 * This is the priority value as seen by users in /proc.
5171 * RT tasks are offset by -200. Normal tasks are centered
5172 * around 0, value goes from -16 to +15.
5173 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005174int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175{
5176 return p->prio - MAX_RT_PRIO;
5177}
5178
5179/**
5180 * task_nice - return the nice value of a given task.
5181 * @p: the task in question.
5182 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005183int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184{
5185 return TASK_NICE(p);
5186}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005187EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
5189/**
5190 * idle_cpu - is a given cpu idle currently?
5191 * @cpu: the processor in question.
5192 */
5193int idle_cpu(int cpu)
5194{
5195 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5196}
5197
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198/**
5199 * idle_task - return the idle task for a given cpu.
5200 * @cpu: the processor in question.
5201 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005202struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203{
5204 return cpu_rq(cpu)->idle;
5205}
5206
5207/**
5208 * find_process_by_pid - find a process with a matching PID value.
5209 * @pid: the pid in question.
5210 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005211static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005213 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214}
5215
5216/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005217static void
5218__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219{
Ingo Molnardd41f592007-07-09 18:51:59 +02005220 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005221
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005223 switch (p->policy) {
5224 case SCHED_NORMAL:
5225 case SCHED_BATCH:
5226 case SCHED_IDLE:
5227 p->sched_class = &fair_sched_class;
5228 break;
5229 case SCHED_FIFO:
5230 case SCHED_RR:
5231 p->sched_class = &rt_sched_class;
5232 break;
5233 }
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005236 p->normal_prio = normal_prio(p);
5237 /* we are holding p->pi_lock already */
5238 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005239 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240}
5241
5242/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005243 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 * @p: the task in question.
5245 * @policy: new policy.
5246 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005247 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005248 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005250int sched_setscheduler(struct task_struct *p, int policy,
5251 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005253 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005255 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005256 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
Steven Rostedt66e53932006-06-27 02:54:44 -07005258 /* may grab non-irq protected spin_locks */
5259 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260recheck:
5261 /* double check policy once rq lock held */
5262 if (policy < 0)
5263 policy = oldpolicy = p->policy;
5264 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005265 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5266 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005267 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 /*
5269 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005270 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5271 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 */
5273 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005274 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005275 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005277 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 return -EINVAL;
5279
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005280 /*
5281 * Allow unprivileged RT tasks to decrease priority:
5282 */
5283 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005284 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005285 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005286
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005287 if (!lock_task_sighand(p, &flags))
5288 return -ESRCH;
5289 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5290 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005291
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005292 /* can't set/change the rt policy */
5293 if (policy != p->policy && !rlim_rtprio)
5294 return -EPERM;
5295
5296 /* can't increase priority */
5297 if (param->sched_priority > p->rt_priority &&
5298 param->sched_priority > rlim_rtprio)
5299 return -EPERM;
5300 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005301 /*
5302 * Like positive nice levels, dont allow tasks to
5303 * move out of SCHED_IDLE either:
5304 */
5305 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5306 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005307
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005308 /* can't change other user's priorities */
5309 if ((current->euid != p->euid) &&
5310 (current->euid != p->uid))
5311 return -EPERM;
5312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005314#ifdef CONFIG_RT_GROUP_SCHED
5315 /*
5316 * Do not allow realtime tasks into groups that have no runtime
5317 * assigned.
5318 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02005319 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005320 return -EPERM;
5321#endif
5322
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 retval = security_task_setscheduler(p, policy, param);
5324 if (retval)
5325 return retval;
5326 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005327 * make sure no PI-waiters arrive (or leave) while we are
5328 * changing the priority of the task:
5329 */
5330 spin_lock_irqsave(&p->pi_lock, flags);
5331 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 * To be able to change p->policy safely, the apropriate
5333 * runqueue lock must be held.
5334 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005335 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 /* recheck policy now with rq lock held */
5337 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5338 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005339 __task_rq_unlock(rq);
5340 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 goto recheck;
5342 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005343 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005344 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005345 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005346 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005347 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005348 if (running)
5349 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005352 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005353
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005354 if (running)
5355 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005356 if (on_rq) {
5357 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005358
5359 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005361 __task_rq_unlock(rq);
5362 spin_unlock_irqrestore(&p->pi_lock, flags);
5363
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005364 rt_mutex_adjust_pi(p);
5365
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 return 0;
5367}
5368EXPORT_SYMBOL_GPL(sched_setscheduler);
5369
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005370static int
5371do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 struct sched_param lparam;
5374 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005375 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377 if (!param || pid < 0)
5378 return -EINVAL;
5379 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5380 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005381
5382 rcu_read_lock();
5383 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005385 if (p != NULL)
5386 retval = sched_setscheduler(p, policy, &lparam);
5387 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005388
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 return retval;
5390}
5391
5392/**
5393 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5394 * @pid: the pid in question.
5395 * @policy: new policy.
5396 * @param: structure containing the new RT priority.
5397 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005398asmlinkage long
5399sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400{
Jason Baronc21761f2006-01-18 17:43:03 -08005401 /* negative values for policy are not valid */
5402 if (policy < 0)
5403 return -EINVAL;
5404
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 return do_sched_setscheduler(pid, policy, param);
5406}
5407
5408/**
5409 * sys_sched_setparam - set/change the RT priority of a thread
5410 * @pid: the pid in question.
5411 * @param: structure containing the new RT priority.
5412 */
5413asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5414{
5415 return do_sched_setscheduler(pid, -1, param);
5416}
5417
5418/**
5419 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5420 * @pid: the pid in question.
5421 */
5422asmlinkage long sys_sched_getscheduler(pid_t pid)
5423{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005424 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005425 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
5427 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005428 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
5430 retval = -ESRCH;
5431 read_lock(&tasklist_lock);
5432 p = find_process_by_pid(pid);
5433 if (p) {
5434 retval = security_task_getscheduler(p);
5435 if (!retval)
5436 retval = p->policy;
5437 }
5438 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 return retval;
5440}
5441
5442/**
5443 * sys_sched_getscheduler - get the RT priority of a thread
5444 * @pid: the pid in question.
5445 * @param: structure containing the RT priority.
5446 */
5447asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5448{
5449 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005450 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005451 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452
5453 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005454 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
5456 read_lock(&tasklist_lock);
5457 p = find_process_by_pid(pid);
5458 retval = -ESRCH;
5459 if (!p)
5460 goto out_unlock;
5461
5462 retval = security_task_getscheduler(p);
5463 if (retval)
5464 goto out_unlock;
5465
5466 lp.sched_priority = p->rt_priority;
5467 read_unlock(&tasklist_lock);
5468
5469 /*
5470 * This one might sleep, we cannot do it with a spinlock held ...
5471 */
5472 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5473
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 return retval;
5475
5476out_unlock:
5477 read_unlock(&tasklist_lock);
5478 return retval;
5479}
5480
Mike Travisb53e9212008-04-04 18:11:08 -07005481long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005484 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005485 struct task_struct *p;
5486 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005488 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 read_lock(&tasklist_lock);
5490
5491 p = find_process_by_pid(pid);
5492 if (!p) {
5493 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005494 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 return -ESRCH;
5496 }
5497
5498 /*
5499 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005500 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 * usage count and then drop tasklist_lock.
5502 */
5503 get_task_struct(p);
5504 read_unlock(&tasklist_lock);
5505
5506 retval = -EPERM;
5507 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5508 !capable(CAP_SYS_NICE))
5509 goto out_unlock;
5510
David Quigleye7834f82006-06-23 02:03:59 -07005511 retval = security_task_setscheduler(p, 0, NULL);
5512 if (retval)
5513 goto out_unlock;
5514
Mike Travisf9a86fc2008-04-04 18:11:07 -07005515 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005517 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005518 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519
Paul Menage8707d8b2007-10-18 23:40:22 -07005520 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005521 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005522 if (!cpus_subset(new_mask, cpus_allowed)) {
5523 /*
5524 * We must have raced with a concurrent cpuset
5525 * update. Just reset the cpus_allowed to the
5526 * cpuset's cpus_allowed
5527 */
5528 new_mask = cpus_allowed;
5529 goto again;
5530 }
5531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532out_unlock:
5533 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005534 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 return retval;
5536}
5537
5538static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5539 cpumask_t *new_mask)
5540{
5541 if (len < sizeof(cpumask_t)) {
5542 memset(new_mask, 0, sizeof(cpumask_t));
5543 } else if (len > sizeof(cpumask_t)) {
5544 len = sizeof(cpumask_t);
5545 }
5546 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5547}
5548
5549/**
5550 * sys_sched_setaffinity - set the cpu affinity of a process
5551 * @pid: pid of the process
5552 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5553 * @user_mask_ptr: user-space pointer to the new cpu mask
5554 */
5555asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5556 unsigned long __user *user_mask_ptr)
5557{
5558 cpumask_t new_mask;
5559 int retval;
5560
5561 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5562 if (retval)
5563 return retval;
5564
Mike Travisb53e9212008-04-04 18:11:08 -07005565 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566}
5567
5568/*
5569 * Represents all cpu's present in the system
5570 * In systems capable of hotplug, this map could dynamically grow
5571 * as new cpu's are detected in the system via any platform specific
5572 * method, such as ACPI for e.g.
5573 */
5574
Andi Kleen4cef0c62006-01-11 22:44:57 +01005575cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576EXPORT_SYMBOL(cpu_present_map);
5577
5578#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005579cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005580EXPORT_SYMBOL(cpu_online_map);
5581
Andi Kleen4cef0c62006-01-11 22:44:57 +01005582cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005583EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584#endif
5585
5586long sched_getaffinity(pid_t pid, cpumask_t *mask)
5587{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005588 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005591 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 read_lock(&tasklist_lock);
5593
5594 retval = -ESRCH;
5595 p = find_process_by_pid(pid);
5596 if (!p)
5597 goto out_unlock;
5598
David Quigleye7834f82006-06-23 02:03:59 -07005599 retval = security_task_getscheduler(p);
5600 if (retval)
5601 goto out_unlock;
5602
Jack Steiner2f7016d2006-02-01 03:05:18 -08005603 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
5605out_unlock:
5606 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005607 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
Ulrich Drepper9531b622007-08-09 11:16:46 +02005609 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610}
5611
5612/**
5613 * sys_sched_getaffinity - get the cpu affinity of a process
5614 * @pid: pid of the process
5615 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5616 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5617 */
5618asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5619 unsigned long __user *user_mask_ptr)
5620{
5621 int ret;
5622 cpumask_t mask;
5623
5624 if (len < sizeof(cpumask_t))
5625 return -EINVAL;
5626
5627 ret = sched_getaffinity(pid, &mask);
5628 if (ret < 0)
5629 return ret;
5630
5631 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5632 return -EFAULT;
5633
5634 return sizeof(cpumask_t);
5635}
5636
5637/**
5638 * sys_sched_yield - yield the current processor to other threads.
5639 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005640 * This function yields the current CPU to other tasks. If there are no
5641 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 */
5643asmlinkage long sys_sched_yield(void)
5644{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005645 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
Ingo Molnar2d723762007-10-15 17:00:12 +02005647 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005648 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
5650 /*
5651 * Since we are going to call schedule() anyway, there's
5652 * no need to preempt or enable interrupts:
5653 */
5654 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005655 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 _raw_spin_unlock(&rq->lock);
5657 preempt_enable_no_resched();
5658
5659 schedule();
5660
5661 return 0;
5662}
5663
Andrew Mortone7b38402006-06-30 01:56:00 -07005664static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005666#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5667 __might_sleep(__FILE__, __LINE__);
5668#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005669 /*
5670 * The BKS might be reacquired before we have dropped
5671 * PREEMPT_ACTIVE, which could trigger a second
5672 * cond_resched() call.
5673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 do {
5675 add_preempt_count(PREEMPT_ACTIVE);
5676 schedule();
5677 sub_preempt_count(PREEMPT_ACTIVE);
5678 } while (need_resched());
5679}
5680
Herbert Xu02b67cc32008-01-25 21:08:28 +01005681#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5682int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683{
Ingo Molnar94142322006-12-29 16:48:13 -08005684 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5685 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 __cond_resched();
5687 return 1;
5688 }
5689 return 0;
5690}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005691EXPORT_SYMBOL(_cond_resched);
5692#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693
5694/*
5695 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5696 * call schedule, and on return reacquire the lock.
5697 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005698 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 * operations here to prevent schedule() from being called twice (once via
5700 * spin_unlock(), once by hand).
5701 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005702int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703{
Nick Piggin95c354f2008-01-30 13:31:20 +01005704 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005705 int ret = 0;
5706
Nick Piggin95c354f2008-01-30 13:31:20 +01005707 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005709 if (resched && need_resched())
5710 __cond_resched();
5711 else
5712 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005713 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005716 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718EXPORT_SYMBOL(cond_resched_lock);
5719
5720int __sched cond_resched_softirq(void)
5721{
5722 BUG_ON(!in_softirq());
5723
Ingo Molnar94142322006-12-29 16:48:13 -08005724 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005725 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 __cond_resched();
5727 local_bh_disable();
5728 return 1;
5729 }
5730 return 0;
5731}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732EXPORT_SYMBOL(cond_resched_softirq);
5733
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734/**
5735 * yield - yield the current processor to other threads.
5736 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005737 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 * thread runnable and calls sys_sched_yield().
5739 */
5740void __sched yield(void)
5741{
5742 set_current_state(TASK_RUNNING);
5743 sys_sched_yield();
5744}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745EXPORT_SYMBOL(yield);
5746
5747/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005748 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 * that process accounting knows that this is a task in IO wait state.
5750 *
5751 * But don't do that if it is a deliberate, throttling IO wait (this task
5752 * has set its backing_dev_info: the queue against which it should throttle)
5753 */
5754void __sched io_schedule(void)
5755{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005756 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005758 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 atomic_inc(&rq->nr_iowait);
5760 schedule();
5761 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005762 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764EXPORT_SYMBOL(io_schedule);
5765
5766long __sched io_schedule_timeout(long timeout)
5767{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005768 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 long ret;
5770
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005771 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 atomic_inc(&rq->nr_iowait);
5773 ret = schedule_timeout(timeout);
5774 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005775 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 return ret;
5777}
5778
5779/**
5780 * sys_sched_get_priority_max - return maximum RT priority.
5781 * @policy: scheduling class.
5782 *
5783 * this syscall returns the maximum rt_priority that can be used
5784 * by a given scheduling class.
5785 */
5786asmlinkage long sys_sched_get_priority_max(int policy)
5787{
5788 int ret = -EINVAL;
5789
5790 switch (policy) {
5791 case SCHED_FIFO:
5792 case SCHED_RR:
5793 ret = MAX_USER_RT_PRIO-1;
5794 break;
5795 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005796 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005797 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 ret = 0;
5799 break;
5800 }
5801 return ret;
5802}
5803
5804/**
5805 * sys_sched_get_priority_min - return minimum RT priority.
5806 * @policy: scheduling class.
5807 *
5808 * this syscall returns the minimum rt_priority that can be used
5809 * by a given scheduling class.
5810 */
5811asmlinkage long sys_sched_get_priority_min(int policy)
5812{
5813 int ret = -EINVAL;
5814
5815 switch (policy) {
5816 case SCHED_FIFO:
5817 case SCHED_RR:
5818 ret = 1;
5819 break;
5820 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005821 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005822 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 ret = 0;
5824 }
5825 return ret;
5826}
5827
5828/**
5829 * sys_sched_rr_get_interval - return the default timeslice of a process.
5830 * @pid: pid of the process.
5831 * @interval: userspace pointer to the timeslice value.
5832 *
5833 * this syscall writes the default timeslice value of a given process
5834 * into the user-space timespec buffer. A value of '0' means infinity.
5835 */
5836asmlinkage
5837long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5838{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005839 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005840 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005841 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
5844 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005845 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
5847 retval = -ESRCH;
5848 read_lock(&tasklist_lock);
5849 p = find_process_by_pid(pid);
5850 if (!p)
5851 goto out_unlock;
5852
5853 retval = security_task_getscheduler(p);
5854 if (retval)
5855 goto out_unlock;
5856
Ingo Molnar77034932007-12-04 17:04:39 +01005857 /*
5858 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5859 * tasks that are on an otherwise idle runqueue:
5860 */
5861 time_slice = 0;
5862 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005863 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005864 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005865 struct sched_entity *se = &p->se;
5866 unsigned long flags;
5867 struct rq *rq;
5868
5869 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005870 if (rq->cfs.load.weight)
5871 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005872 task_rq_unlock(rq, &flags);
5873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005875 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005878
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879out_unlock:
5880 read_unlock(&tasklist_lock);
5881 return retval;
5882}
5883
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005884static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005885
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005886void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005889 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005892 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005893 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005894#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005896 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005898 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899#else
5900 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005901 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005903 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904#endif
5905#ifdef CONFIG_DEBUG_STACK_USAGE
5906 {
Al Viro10ebffd2005-11-13 16:06:56 -08005907 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 while (!*n)
5909 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005910 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 }
5912#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005913 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005914 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005916 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917}
5918
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005919void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005921 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
Ingo Molnar4bd77322007-07-11 21:21:47 +02005923#if BITS_PER_LONG == 32
5924 printk(KERN_INFO
5925 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005927 printk(KERN_INFO
5928 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929#endif
5930 read_lock(&tasklist_lock);
5931 do_each_thread(g, p) {
5932 /*
5933 * reset the NMI-timeout, listing all files on a slow
5934 * console might take alot of time:
5935 */
5936 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005937 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005938 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 } while_each_thread(g, p);
5940
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005941 touch_all_softlockup_watchdogs();
5942
Ingo Molnardd41f592007-07-09 18:51:59 +02005943#ifdef CONFIG_SCHED_DEBUG
5944 sysrq_sched_debug_show();
5945#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005947 /*
5948 * Only show locks if all tasks are dumped:
5949 */
5950 if (state_filter == -1)
5951 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952}
5953
Ingo Molnar1df21052007-07-09 18:51:58 +02005954void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5955{
Ingo Molnardd41f592007-07-09 18:51:59 +02005956 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005957}
5958
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005959/**
5960 * init_idle - set up an idle thread for a given CPU
5961 * @idle: task in question
5962 * @cpu: cpu the idle task belongs to
5963 *
5964 * NOTE: this function does not set the idle thread's NEED_RESCHED
5965 * flag, to make booting more robust.
5966 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005967void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005969 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 unsigned long flags;
5971
Ingo Molnardd41f592007-07-09 18:51:59 +02005972 __sched_fork(idle);
5973 idle->se.exec_start = sched_clock();
5974
Ingo Molnarb29739f2006-06-27 02:54:51 -07005975 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005977 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978
5979 spin_lock_irqsave(&rq->lock, flags);
5980 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005981#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5982 idle->oncpu = 1;
5983#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 spin_unlock_irqrestore(&rq->lock, flags);
5985
5986 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f542005-11-13 16:06:55 -08005987 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005988
Ingo Molnardd41f592007-07-09 18:51:59 +02005989 /*
5990 * The idle tasks have their own, simple scheduling class:
5991 */
5992 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993}
5994
5995/*
5996 * In a system that switches off the HZ timer nohz_cpu_mask
5997 * indicates which cpus entered this state. This is used
5998 * in the rcu update to wait only for active cpus. For system
5999 * which do not switch off the HZ timer nohz_cpu_mask should
6000 * always be CPU_MASK_NONE.
6001 */
6002cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
6003
Ingo Molnar19978ca2007-11-09 22:39:38 +01006004/*
6005 * Increase the granularity value when there are more CPUs,
6006 * because with more CPUs the 'effective latency' as visible
6007 * to users decreases. But the relationship is not linear,
6008 * so pick a second-best guess by going with the log2 of the
6009 * number of CPUs.
6010 *
6011 * This idea comes from the SD scheduler of Con Kolivas:
6012 */
6013static inline void sched_init_granularity(void)
6014{
6015 unsigned int factor = 1 + ilog2(num_online_cpus());
6016 const unsigned long limit = 200000000;
6017
6018 sysctl_sched_min_granularity *= factor;
6019 if (sysctl_sched_min_granularity > limit)
6020 sysctl_sched_min_granularity = limit;
6021
6022 sysctl_sched_latency *= factor;
6023 if (sysctl_sched_latency > limit)
6024 sysctl_sched_latency = limit;
6025
6026 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01006027}
6028
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029#ifdef CONFIG_SMP
6030/*
6031 * This is how migration works:
6032 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07006033 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 * runqueue and wake up that CPU's migration thread.
6035 * 2) we down() the locked semaphore => thread blocks.
6036 * 3) migration thread wakes up (implicitly it forces the migrated
6037 * thread off the CPU)
6038 * 4) it gets the migration request and checks whether the migrated
6039 * task is still in the wrong runqueue.
6040 * 5) if it's in the wrong runqueue then the migration thread removes
6041 * it and puts it into the right queue.
6042 * 6) migration thread up()s the semaphore.
6043 * 7) we wake up and the migration is done.
6044 */
6045
6046/*
6047 * Change a given task's CPU affinity. Migrate the thread to a
6048 * proper CPU and schedule it away if the CPU it's executing on
6049 * is removed from the allowed bitmask.
6050 *
6051 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006052 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 * call is not atomic; no spinlocks may be held.
6054 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006055int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006057 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006059 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006060 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
6062 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006063 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 ret = -EINVAL;
6065 goto out;
6066 }
6067
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006068 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006069 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006070 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006071 p->cpus_allowed = *new_mask;
6072 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006073 }
6074
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006076 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 goto out;
6078
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006079 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 /* Need help from migration thread: drop lock and wait. */
6081 task_rq_unlock(rq, &flags);
6082 wake_up_process(rq->migration_thread);
6083 wait_for_completion(&req.done);
6084 tlb_migrate_finish(p->mm);
6085 return 0;
6086 }
6087out:
6088 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006089
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 return ret;
6091}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006092EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
6094/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006095 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 * this because either it can't run here any more (set_cpus_allowed()
6097 * away from this CPU, or CPU going down), or because we're
6098 * attempting to rebalance this task on exec (sched_exec).
6099 *
6100 * So we race with normal scheduler movements, but that's OK, as long
6101 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006102 *
6103 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006105static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006107 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006108 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
6110 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112
6113 rq_src = cpu_rq(src_cpu);
6114 rq_dest = cpu_rq(dest_cpu);
6115
6116 double_rq_lock(rq_src, rq_dest);
6117 /* Already moved. */
6118 if (task_cpu(p) != src_cpu)
6119 goto out;
6120 /* Affinity changed (again). */
6121 if (!cpu_isset(dest_cpu, p->cpus_allowed))
6122 goto out;
6123
Ingo Molnardd41f592007-07-09 18:51:59 +02006124 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006125 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006126 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006127
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006129 if (on_rq) {
6130 activate_task(rq_dest, p, 0);
6131 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07006133 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134out:
6135 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006136 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137}
6138
6139/*
6140 * migration_thread - this is a highprio system thread that performs
6141 * thread migration by bumping thread off CPU then 'pushing' onto
6142 * another runqueue.
6143 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006144static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006147 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
6149 rq = cpu_rq(cpu);
6150 BUG_ON(rq->migration_thread != current);
6151
6152 set_current_state(TASK_INTERRUPTIBLE);
6153 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006154 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 spin_lock_irq(&rq->lock);
6158
6159 if (cpu_is_offline(cpu)) {
6160 spin_unlock_irq(&rq->lock);
6161 goto wait_to_die;
6162 }
6163
6164 if (rq->active_balance) {
6165 active_load_balance(rq, cpu);
6166 rq->active_balance = 0;
6167 }
6168
6169 head = &rq->migration_queue;
6170
6171 if (list_empty(head)) {
6172 spin_unlock_irq(&rq->lock);
6173 schedule();
6174 set_current_state(TASK_INTERRUPTIBLE);
6175 continue;
6176 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006177 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 list_del_init(head->next);
6179
Nick Piggin674311d2005-06-25 14:57:27 -07006180 spin_unlock(&rq->lock);
6181 __migrate_task(req->task, cpu, req->dest_cpu);
6182 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183
6184 complete(&req->done);
6185 }
6186 __set_current_state(TASK_RUNNING);
6187 return 0;
6188
6189wait_to_die:
6190 /* Wait for kthread_stop */
6191 set_current_state(TASK_INTERRUPTIBLE);
6192 while (!kthread_should_stop()) {
6193 schedule();
6194 set_current_state(TASK_INTERRUPTIBLE);
6195 }
6196 __set_current_state(TASK_RUNNING);
6197 return 0;
6198}
6199
6200#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006201
6202static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6203{
6204 int ret;
6205
6206 local_irq_disable();
6207 ret = __migrate_task(p, src_cpu, dest_cpu);
6208 local_irq_enable();
6209 return ret;
6210}
6211
Kirill Korotaev054b9102006-12-10 02:20:11 -08006212/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006213 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006214 * NOTE: interrupts should be disabled by the caller
6215 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006216static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006218 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006220 struct rq *rq;
6221 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222
Andi Kleen3a5c3592007-10-15 17:00:14 +02006223 do {
6224 /* On same node? */
6225 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6226 cpus_and(mask, mask, p->cpus_allowed);
6227 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
Andi Kleen3a5c3592007-10-15 17:00:14 +02006229 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006230 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006231 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232
Andi Kleen3a5c3592007-10-15 17:00:14 +02006233 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006234 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006235 cpumask_t cpus_allowed;
6236
6237 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006238 /*
6239 * Try to stay on the same cpuset, where the
6240 * current cpuset may be a subset of all cpus.
6241 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006242 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006243 * called within calls to cpuset_lock/cpuset_unlock.
6244 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006245 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006246 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006247 dest_cpu = any_online_cpu(p->cpus_allowed);
6248 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249
Andi Kleen3a5c3592007-10-15 17:00:14 +02006250 /*
6251 * Don't tell them about moving exiting tasks or
6252 * kernel threads (both mm NULL), since they never
6253 * leave kernel.
6254 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006255 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006256 printk(KERN_INFO "process %d (%s) no "
6257 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006258 task_pid_nr(p), p->comm, dead_cpu);
6259 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006260 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006261 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262}
6263
6264/*
6265 * While a dead CPU has no uninterruptible tasks queued at this point,
6266 * it might still have a nonzero ->nr_uninterruptible counter, because
6267 * for performance reasons the counter is not stricly tracking tasks to
6268 * their home CPUs. So we just add the counter to another CPU's counter,
6269 * to keep the global sum constant after CPU-down:
6270 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006271static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272{
Mike Travis7c16ec52008-04-04 18:11:11 -07006273 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 unsigned long flags;
6275
6276 local_irq_save(flags);
6277 double_rq_lock(rq_src, rq_dest);
6278 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6279 rq_src->nr_uninterruptible = 0;
6280 double_rq_unlock(rq_src, rq_dest);
6281 local_irq_restore(flags);
6282}
6283
6284/* Run through task list and migrate tasks from the dead cpu. */
6285static void migrate_live_tasks(int src_cpu)
6286{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006287 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006289 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290
Ingo Molnar48f24c42006-07-03 00:25:40 -07006291 do_each_thread(t, p) {
6292 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 continue;
6294
Ingo Molnar48f24c42006-07-03 00:25:40 -07006295 if (task_cpu(p) == src_cpu)
6296 move_task_off_dead_cpu(src_cpu, p);
6297 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006299 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300}
6301
Ingo Molnardd41f592007-07-09 18:51:59 +02006302/*
6303 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006304 * It does so by boosting its priority to highest possible.
6305 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 */
6307void sched_idle_next(void)
6308{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006309 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006310 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 struct task_struct *p = rq->idle;
6312 unsigned long flags;
6313
6314 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006315 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316
Ingo Molnar48f24c42006-07-03 00:25:40 -07006317 /*
6318 * Strictly not necessary since rest of the CPUs are stopped by now
6319 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 */
6321 spin_lock_irqsave(&rq->lock, flags);
6322
Ingo Molnardd41f592007-07-09 18:51:59 +02006323 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006324
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006325 update_rq_clock(rq);
6326 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327
6328 spin_unlock_irqrestore(&rq->lock, flags);
6329}
6330
Ingo Molnar48f24c42006-07-03 00:25:40 -07006331/*
6332 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 * offline.
6334 */
6335void idle_task_exit(void)
6336{
6337 struct mm_struct *mm = current->active_mm;
6338
6339 BUG_ON(cpu_online(smp_processor_id()));
6340
6341 if (mm != &init_mm)
6342 switch_mm(mm, &init_mm, current);
6343 mmdrop(mm);
6344}
6345
Kirill Korotaev054b9102006-12-10 02:20:11 -08006346/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006347static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006349 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350
6351 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006352 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353
6354 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006355 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
Ingo Molnar48f24c42006-07-03 00:25:40 -07006357 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
6359 /*
6360 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006361 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 * fine.
6363 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006364 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006365 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006366 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367
Ingo Molnar48f24c42006-07-03 00:25:40 -07006368 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369}
6370
6371/* release_task() removes task from tasklist, so we won't find dead tasks. */
6372static void migrate_dead_tasks(unsigned int dead_cpu)
6373{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006374 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006375 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376
Ingo Molnardd41f592007-07-09 18:51:59 +02006377 for ( ; ; ) {
6378 if (!rq->nr_running)
6379 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006380 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006381 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006382 if (!next)
6383 break;
6384 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006385
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 }
6387}
6388#endif /* CONFIG_HOTPLUG_CPU */
6389
Nick Piggine692ab52007-07-26 13:40:43 +02006390#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6391
6392static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006393 {
6394 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006395 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006396 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006397 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006398};
6399
6400static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006401 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006402 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006403 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006404 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006405 .child = sd_ctl_dir,
6406 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006407 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006408};
6409
6410static struct ctl_table *sd_alloc_ctl_entry(int n)
6411{
6412 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006413 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006414
Nick Piggine692ab52007-07-26 13:40:43 +02006415 return entry;
6416}
6417
Milton Miller6382bc92007-10-15 17:00:19 +02006418static void sd_free_ctl_entry(struct ctl_table **tablep)
6419{
Milton Millercd7900762007-10-17 16:55:11 +02006420 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006421
Milton Millercd7900762007-10-17 16:55:11 +02006422 /*
6423 * In the intermediate directories, both the child directory and
6424 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006425 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006426 * static strings and all have proc handlers.
6427 */
6428 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006429 if (entry->child)
6430 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006431 if (entry->proc_handler == NULL)
6432 kfree(entry->procname);
6433 }
Milton Miller6382bc92007-10-15 17:00:19 +02006434
6435 kfree(*tablep);
6436 *tablep = NULL;
6437}
6438
Nick Piggine692ab52007-07-26 13:40:43 +02006439static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006440set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006441 const char *procname, void *data, int maxlen,
6442 mode_t mode, proc_handler *proc_handler)
6443{
Nick Piggine692ab52007-07-26 13:40:43 +02006444 entry->procname = procname;
6445 entry->data = data;
6446 entry->maxlen = maxlen;
6447 entry->mode = mode;
6448 entry->proc_handler = proc_handler;
6449}
6450
6451static struct ctl_table *
6452sd_alloc_ctl_domain_table(struct sched_domain *sd)
6453{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006454 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006455
Milton Millerad1cdc12007-10-15 17:00:19 +02006456 if (table == NULL)
6457 return NULL;
6458
Alexey Dobriyane0361852007-08-09 11:16:46 +02006459 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006460 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006461 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006462 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006463 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006464 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006465 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006466 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006467 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006468 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006469 set_table_entry(&table[5], "wake_idx", &sd->wake_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[6], "forkexec_idx", &sd->forkexec_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[7], "busy_factor", &sd->busy_factor,
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[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006476 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006477 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006478 &sd->cache_nice_tries,
6479 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006480 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006481 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006482 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006483
6484 return table;
6485}
6486
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006487static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006488{
6489 struct ctl_table *entry, *table;
6490 struct sched_domain *sd;
6491 int domain_num = 0, i;
6492 char buf[32];
6493
6494 for_each_domain(cpu, sd)
6495 domain_num++;
6496 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006497 if (table == NULL)
6498 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006499
6500 i = 0;
6501 for_each_domain(cpu, sd) {
6502 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006503 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006504 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006505 entry->child = sd_alloc_ctl_domain_table(sd);
6506 entry++;
6507 i++;
6508 }
6509 return table;
6510}
6511
6512static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006513static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006514{
6515 int i, cpu_num = num_online_cpus();
6516 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6517 char buf[32];
6518
Milton Miller73785472007-10-24 18:23:48 +02006519 WARN_ON(sd_ctl_dir[0].child);
6520 sd_ctl_dir[0].child = entry;
6521
Milton Millerad1cdc12007-10-15 17:00:19 +02006522 if (entry == NULL)
6523 return;
6524
Milton Miller97b6ea72007-10-15 17:00:19 +02006525 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006526 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006527 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006528 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006529 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006530 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006531 }
Milton Miller73785472007-10-24 18:23:48 +02006532
6533 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006534 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6535}
Milton Miller6382bc92007-10-15 17:00:19 +02006536
Milton Miller73785472007-10-24 18:23:48 +02006537/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006538static void unregister_sched_domain_sysctl(void)
6539{
Milton Miller73785472007-10-24 18:23:48 +02006540 if (sd_sysctl_header)
6541 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006542 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006543 if (sd_ctl_dir[0].child)
6544 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006545}
Nick Piggine692ab52007-07-26 13:40:43 +02006546#else
Milton Miller6382bc92007-10-15 17:00:19 +02006547static void register_sched_domain_sysctl(void)
6548{
6549}
6550static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006551{
6552}
6553#endif
6554
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555/*
6556 * migration_call - callback that gets triggered when a CPU is added.
6557 * Here we can start up the necessary migration thread for the new CPU.
6558 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006559static int __cpuinit
6560migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006563 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006565 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566
6567 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006568
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006570 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006571 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 if (IS_ERR(p))
6573 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 kthread_bind(p, cpu);
6575 /* Must be high prio: stop_machine expects to yield to it. */
6576 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006577 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 task_rq_unlock(rq, &flags);
6579 cpu_rq(cpu)->migration_thread = p;
6580 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006581
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006583 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006584 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006586
6587 /* Update our root-domain */
6588 rq = cpu_rq(cpu);
6589 spin_lock_irqsave(&rq->lock, flags);
6590 if (rq->rd) {
6591 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6592 cpu_set(cpu, rq->rd->online);
6593 }
6594 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006596
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597#ifdef CONFIG_HOTPLUG_CPU
6598 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006599 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006600 if (!cpu_rq(cpu)->migration_thread)
6601 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006602 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006603 kthread_bind(cpu_rq(cpu)->migration_thread,
6604 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 kthread_stop(cpu_rq(cpu)->migration_thread);
6606 cpu_rq(cpu)->migration_thread = NULL;
6607 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006608
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006610 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006611 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 migrate_live_tasks(cpu);
6613 rq = cpu_rq(cpu);
6614 kthread_stop(rq->migration_thread);
6615 rq->migration_thread = NULL;
6616 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006617 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006618 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006619 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006621 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6622 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006624 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006625 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 migrate_nr_uninterruptible(rq);
6627 BUG_ON(rq->nr_running != 0);
6628
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006629 /*
6630 * No need to migrate the tasks: it was best-effort if
6631 * they didn't take sched_hotcpu_mutex. Just wake up
6632 * the requestors.
6633 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 spin_lock_irq(&rq->lock);
6635 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006636 struct migration_req *req;
6637
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006639 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 list_del_init(&req->list);
6641 complete(&req->done);
6642 }
6643 spin_unlock_irq(&rq->lock);
6644 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006645
Gregory Haskins08f503b2008-03-10 17:59:11 -04006646 case CPU_DYING:
6647 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006648 /* Update our root-domain */
6649 rq = cpu_rq(cpu);
6650 spin_lock_irqsave(&rq->lock, flags);
6651 if (rq->rd) {
6652 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6653 cpu_clear(cpu, rq->rd->online);
6654 }
6655 spin_unlock_irqrestore(&rq->lock, flags);
6656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657#endif
6658 }
6659 return NOTIFY_OK;
6660}
6661
6662/* Register at highest priority so that task migration (migrate_all_tasks)
6663 * happens before everything else.
6664 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006665static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 .notifier_call = migration_call,
6667 .priority = 10
6668};
6669
Adrian Bunke6fe6642007-11-09 22:39:39 +01006670void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671{
6672 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006673 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006674
6675 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006676 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6677 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6679 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680}
6681#endif
6682
6683#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006684
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006685#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006686
Mike Travis7c16ec52008-04-04 18:11:11 -07006687static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6688 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006689{
6690 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006691 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006692
Mike Travis434d53b2008-04-04 18:11:04 -07006693 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006694 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006695
6696 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6697
6698 if (!(sd->flags & SD_LOAD_BALANCE)) {
6699 printk("does not load-balance\n");
6700 if (sd->parent)
6701 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6702 " has parent");
6703 return -1;
6704 }
6705
6706 printk(KERN_CONT "span %s\n", str);
6707
6708 if (!cpu_isset(cpu, sd->span)) {
6709 printk(KERN_ERR "ERROR: domain->span does not contain "
6710 "CPU%d\n", cpu);
6711 }
6712 if (!cpu_isset(cpu, group->cpumask)) {
6713 printk(KERN_ERR "ERROR: domain->groups does not contain"
6714 " CPU%d\n", cpu);
6715 }
6716
6717 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6718 do {
6719 if (!group) {
6720 printk("\n");
6721 printk(KERN_ERR "ERROR: group is NULL\n");
6722 break;
6723 }
6724
6725 if (!group->__cpu_power) {
6726 printk(KERN_CONT "\n");
6727 printk(KERN_ERR "ERROR: domain->cpu_power not "
6728 "set\n");
6729 break;
6730 }
6731
6732 if (!cpus_weight(group->cpumask)) {
6733 printk(KERN_CONT "\n");
6734 printk(KERN_ERR "ERROR: empty group\n");
6735 break;
6736 }
6737
Mike Travis7c16ec52008-04-04 18:11:11 -07006738 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006739 printk(KERN_CONT "\n");
6740 printk(KERN_ERR "ERROR: repeated CPUs\n");
6741 break;
6742 }
6743
Mike Travis7c16ec52008-04-04 18:11:11 -07006744 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006745
Mike Travis434d53b2008-04-04 18:11:04 -07006746 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006747 printk(KERN_CONT " %s", str);
6748
6749 group = group->next;
6750 } while (group != sd->groups);
6751 printk(KERN_CONT "\n");
6752
Mike Travis7c16ec52008-04-04 18:11:11 -07006753 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006754 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6755
Mike Travis7c16ec52008-04-04 18:11:11 -07006756 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006757 printk(KERN_ERR "ERROR: parent span is not a superset "
6758 "of domain->span\n");
6759 return 0;
6760}
6761
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762static void sched_domain_debug(struct sched_domain *sd, int cpu)
6763{
Mike Travis7c16ec52008-04-04 18:11:11 -07006764 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 int level = 0;
6766
Nick Piggin41c7ce92005-06-25 14:57:24 -07006767 if (!sd) {
6768 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6769 return;
6770 }
6771
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6773
Mike Travis7c16ec52008-04-04 18:11:11 -07006774 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6775 if (!groupmask) {
6776 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6777 return;
6778 }
6779
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006780 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006781 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 level++;
6784 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006785 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006786 break;
6787 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006788 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789}
6790#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006791# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792#endif
6793
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006794static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006795{
6796 if (cpus_weight(sd->span) == 1)
6797 return 1;
6798
6799 /* Following flags need at least 2 groups */
6800 if (sd->flags & (SD_LOAD_BALANCE |
6801 SD_BALANCE_NEWIDLE |
6802 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006803 SD_BALANCE_EXEC |
6804 SD_SHARE_CPUPOWER |
6805 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006806 if (sd->groups != sd->groups->next)
6807 return 0;
6808 }
6809
6810 /* Following flags don't use groups */
6811 if (sd->flags & (SD_WAKE_IDLE |
6812 SD_WAKE_AFFINE |
6813 SD_WAKE_BALANCE))
6814 return 0;
6815
6816 return 1;
6817}
6818
Ingo Molnar48f24c42006-07-03 00:25:40 -07006819static int
6820sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006821{
6822 unsigned long cflags = sd->flags, pflags = parent->flags;
6823
6824 if (sd_degenerate(parent))
6825 return 1;
6826
6827 if (!cpus_equal(sd->span, parent->span))
6828 return 0;
6829
6830 /* Does parent contain flags not in child? */
6831 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6832 if (cflags & SD_WAKE_AFFINE)
6833 pflags &= ~SD_WAKE_BALANCE;
6834 /* Flags needing groups don't count if only 1 group in parent */
6835 if (parent->groups == parent->groups->next) {
6836 pflags &= ~(SD_LOAD_BALANCE |
6837 SD_BALANCE_NEWIDLE |
6838 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006839 SD_BALANCE_EXEC |
6840 SD_SHARE_CPUPOWER |
6841 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006842 }
6843 if (~cflags & pflags)
6844 return 0;
6845
6846 return 1;
6847}
6848
Gregory Haskins57d885f2008-01-25 21:08:18 +01006849static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6850{
6851 unsigned long flags;
6852 const struct sched_class *class;
6853
6854 spin_lock_irqsave(&rq->lock, flags);
6855
6856 if (rq->rd) {
6857 struct root_domain *old_rd = rq->rd;
6858
Ingo Molnar0eab9142008-01-25 21:08:19 +01006859 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006860 if (class->leave_domain)
6861 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006862 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006863
Gregory Haskinsdc938522008-01-25 21:08:26 +01006864 cpu_clear(rq->cpu, old_rd->span);
6865 cpu_clear(rq->cpu, old_rd->online);
6866
Gregory Haskins57d885f2008-01-25 21:08:18 +01006867 if (atomic_dec_and_test(&old_rd->refcount))
6868 kfree(old_rd);
6869 }
6870
6871 atomic_inc(&rd->refcount);
6872 rq->rd = rd;
6873
Gregory Haskinsdc938522008-01-25 21:08:26 +01006874 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006875 if (cpu_isset(rq->cpu, cpu_online_map))
6876 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006877
Ingo Molnar0eab9142008-01-25 21:08:19 +01006878 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006879 if (class->join_domain)
6880 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006881 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006882
6883 spin_unlock_irqrestore(&rq->lock, flags);
6884}
6885
Gregory Haskinsdc938522008-01-25 21:08:26 +01006886static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006887{
6888 memset(rd, 0, sizeof(*rd));
6889
Gregory Haskinsdc938522008-01-25 21:08:26 +01006890 cpus_clear(rd->span);
6891 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006892}
6893
6894static void init_defrootdomain(void)
6895{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006896 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006897 atomic_set(&def_root_domain.refcount, 1);
6898}
6899
Gregory Haskinsdc938522008-01-25 21:08:26 +01006900static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006901{
6902 struct root_domain *rd;
6903
6904 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6905 if (!rd)
6906 return NULL;
6907
Gregory Haskinsdc938522008-01-25 21:08:26 +01006908 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006909
6910 return rd;
6911}
6912
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006914 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 * hold the hotplug lock.
6916 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006917static void
6918cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006920 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006921 struct sched_domain *tmp;
6922
6923 /* Remove the sched domains which do not contribute to scheduling. */
6924 for (tmp = sd; tmp; tmp = tmp->parent) {
6925 struct sched_domain *parent = tmp->parent;
6926 if (!parent)
6927 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006928 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006929 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006930 if (parent->parent)
6931 parent->parent->child = tmp;
6932 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006933 }
6934
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006935 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006936 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006937 if (sd)
6938 sd->child = NULL;
6939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940
6941 sched_domain_debug(sd, cpu);
6942
Gregory Haskins57d885f2008-01-25 21:08:18 +01006943 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006944 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945}
6946
6947/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006948static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949
6950/* Setup the mask of cpus configured for isolated domains */
6951static int __init isolated_cpu_setup(char *str)
6952{
6953 int ints[NR_CPUS], i;
6954
6955 str = get_options(str, ARRAY_SIZE(ints), ints);
6956 cpus_clear(cpu_isolated_map);
6957 for (i = 1; i <= ints[0]; i++)
6958 if (ints[i] < NR_CPUS)
6959 cpu_set(ints[i], cpu_isolated_map);
6960 return 1;
6961}
6962
Ingo Molnar8927f492007-10-15 17:00:13 +02006963__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964
6965/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006966 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6967 * to a function which identifies what group(along with sched group) a CPU
6968 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6969 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 *
6971 * init_sched_build_groups will build a circular linked list of the groups
6972 * covered by the given span, and will set each group's ->cpumask correctly,
6973 * and ->cpu_power to 0.
6974 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006975static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006976init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006977 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006978 struct sched_group **sg,
6979 cpumask_t *tmpmask),
6980 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981{
6982 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 int i;
6984
Mike Travis7c16ec52008-04-04 18:11:11 -07006985 cpus_clear(*covered);
6986
6987 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006988 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006989 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 int j;
6991
Mike Travis7c16ec52008-04-04 18:11:11 -07006992 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 continue;
6994
Mike Travis7c16ec52008-04-04 18:11:11 -07006995 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006996 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997
Mike Travis7c16ec52008-04-04 18:11:11 -07006998 for_each_cpu_mask(j, *span) {
6999 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 continue;
7001
Mike Travis7c16ec52008-04-04 18:11:11 -07007002 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 cpu_set(j, sg->cpumask);
7004 }
7005 if (!first)
7006 first = sg;
7007 if (last)
7008 last->next = sg;
7009 last = sg;
7010 }
7011 last->next = first;
7012}
7013
John Hawkes9c1cfda2005-09-06 15:18:14 -07007014#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015
John Hawkes9c1cfda2005-09-06 15:18:14 -07007016#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08007017
John Hawkes9c1cfda2005-09-06 15:18:14 -07007018/**
7019 * find_next_best_node - find the next node to include in a sched_domain
7020 * @node: node whose sched_domain we're building
7021 * @used_nodes: nodes already in the sched_domain
7022 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007023 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07007024 * finds the closest node not already in the @used_nodes map.
7025 *
7026 * Should use nodemask_t.
7027 */
Mike Travisc5f59f02008-04-04 18:11:10 -07007028static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007029{
7030 int i, n, val, min_val, best_node = 0;
7031
7032 min_val = INT_MAX;
7033
7034 for (i = 0; i < MAX_NUMNODES; i++) {
7035 /* Start at @node */
7036 n = (node + i) % MAX_NUMNODES;
7037
7038 if (!nr_cpus_node(n))
7039 continue;
7040
7041 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07007042 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007043 continue;
7044
7045 /* Simple min distance search */
7046 val = node_distance(node, n);
7047
7048 if (val < min_val) {
7049 min_val = val;
7050 best_node = n;
7051 }
7052 }
7053
Mike Travisc5f59f02008-04-04 18:11:10 -07007054 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007055 return best_node;
7056}
7057
7058/**
7059 * sched_domain_node_span - get a cpumask for a node's sched_domain
7060 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07007061 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007062 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007063 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007064 * should be one that prevents unnecessary balancing, but also spreads tasks
7065 * out optimally.
7066 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07007067static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007068{
Mike Travisc5f59f02008-04-04 18:11:10 -07007069 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07007070 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007071 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007072
Mike Travis4bdbaad32008-04-15 16:35:52 -07007073 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007074 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007075
Mike Travis4bdbaad32008-04-15 16:35:52 -07007076 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07007077 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007078
7079 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007080 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007081
Mike Travisc5f59f02008-04-04 18:11:10 -07007082 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007083 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007084 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007085}
7086#endif
7087
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007088int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007089
John Hawkes9c1cfda2005-09-06 15:18:14 -07007090/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07007091 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07007092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093#ifdef CONFIG_SCHED_SMT
7094static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007095static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007096
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007097static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007098cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7099 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007101 if (sg)
7102 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 return cpu;
7104}
7105#endif
7106
Ingo Molnar48f24c42006-07-03 00:25:40 -07007107/*
7108 * multi-core sched-domains:
7109 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007110#ifdef CONFIG_SCHED_MC
7111static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007112static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007113#endif
7114
7115#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007116static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007117cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7118 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007119{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007120 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007121
7122 *mask = per_cpu(cpu_sibling_map, cpu);
7123 cpus_and(*mask, *mask, *cpu_map);
7124 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007125 if (sg)
7126 *sg = &per_cpu(sched_group_core, group);
7127 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007128}
7129#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007130static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007131cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7132 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007133{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007134 if (sg)
7135 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007136 return cpu;
7137}
7138#endif
7139
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007141static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007142
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007143static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007144cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7145 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007147 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007148#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007149 *mask = cpu_coregroup_map(cpu);
7150 cpus_and(*mask, *mask, *cpu_map);
7151 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007152#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007153 *mask = per_cpu(cpu_sibling_map, cpu);
7154 cpus_and(*mask, *mask, *cpu_map);
7155 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007157 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007159 if (sg)
7160 *sg = &per_cpu(sched_group_phys, group);
7161 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162}
7163
7164#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007165/*
7166 * The init_sched_build_groups can't handle what we want to do with node
7167 * groups, so roll our own. Now each node has its own list of groups which
7168 * gets dynamically allocated.
7169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007171static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007172
7173static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007174static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007175
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007176static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007177 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007179 int group;
7180
Mike Travis7c16ec52008-04-04 18:11:11 -07007181 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7182 cpus_and(*nodemask, *nodemask, *cpu_map);
7183 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007184
7185 if (sg)
7186 *sg = &per_cpu(sched_group_allnodes, group);
7187 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007189
Siddha, Suresh B08069032006-03-27 01:15:23 -08007190static void init_numa_sched_groups_power(struct sched_group *group_head)
7191{
7192 struct sched_group *sg = group_head;
7193 int j;
7194
7195 if (!sg)
7196 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007197 do {
7198 for_each_cpu_mask(j, sg->cpumask) {
7199 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007200
Andi Kleen3a5c3592007-10-15 17:00:14 +02007201 sd = &per_cpu(phys_domains, j);
7202 if (j != first_cpu(sd->groups->cpumask)) {
7203 /*
7204 * Only add "power" once for each
7205 * physical package.
7206 */
7207 continue;
7208 }
7209
7210 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007211 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007212 sg = sg->next;
7213 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007214}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215#endif
7216
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007217#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007218/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007219static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007220{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007221 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007222
7223 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007224 struct sched_group **sched_group_nodes
7225 = sched_group_nodes_bycpu[cpu];
7226
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007227 if (!sched_group_nodes)
7228 continue;
7229
7230 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007231 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7232
Mike Travis7c16ec52008-04-04 18:11:11 -07007233 *nodemask = node_to_cpumask(i);
7234 cpus_and(*nodemask, *nodemask, *cpu_map);
7235 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007236 continue;
7237
7238 if (sg == NULL)
7239 continue;
7240 sg = sg->next;
7241next_sg:
7242 oldsg = sg;
7243 sg = sg->next;
7244 kfree(oldsg);
7245 if (oldsg != sched_group_nodes[i])
7246 goto next_sg;
7247 }
7248 kfree(sched_group_nodes);
7249 sched_group_nodes_bycpu[cpu] = NULL;
7250 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007251}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007252#else
Mike Travis7c16ec52008-04-04 18:11:11 -07007253static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007254{
7255}
7256#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007257
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007259 * Initialize sched groups cpu_power.
7260 *
7261 * cpu_power indicates the capacity of sched group, which is used while
7262 * distributing the load between different sched groups in a sched domain.
7263 * Typically cpu_power for all the groups in a sched domain will be same unless
7264 * there are asymmetries in the topology. If there are asymmetries, group
7265 * having more cpu_power will pickup more load compared to the group having
7266 * less cpu_power.
7267 *
7268 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7269 * the maximum number of tasks a group can handle in the presence of other idle
7270 * or lightly loaded groups in the same sched domain.
7271 */
7272static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7273{
7274 struct sched_domain *child;
7275 struct sched_group *group;
7276
7277 WARN_ON(!sd || !sd->groups);
7278
7279 if (cpu != first_cpu(sd->groups->cpumask))
7280 return;
7281
7282 child = sd->child;
7283
Eric Dumazet5517d862007-05-08 00:32:57 -07007284 sd->groups->__cpu_power = 0;
7285
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007286 /*
7287 * For perf policy, if the groups in child domain share resources
7288 * (for example cores sharing some portions of the cache hierarchy
7289 * or SMT), then set this domain groups cpu_power such that each group
7290 * can handle only one task, when there are other idle groups in the
7291 * same sched domain.
7292 */
7293 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7294 (child->flags &
7295 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007296 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007297 return;
7298 }
7299
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007300 /*
7301 * add cpu_power of each child group to this groups cpu_power
7302 */
7303 group = child->groups;
7304 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007305 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007306 group = group->next;
7307 } while (group != child->groups);
7308}
7309
7310/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007311 * Initializers for schedule domains
7312 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7313 */
7314
7315#define SD_INIT(sd, type) sd_init_##type(sd)
7316#define SD_INIT_FUNC(type) \
7317static noinline void sd_init_##type(struct sched_domain *sd) \
7318{ \
7319 memset(sd, 0, sizeof(*sd)); \
7320 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007321 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007322}
7323
7324SD_INIT_FUNC(CPU)
7325#ifdef CONFIG_NUMA
7326 SD_INIT_FUNC(ALLNODES)
7327 SD_INIT_FUNC(NODE)
7328#endif
7329#ifdef CONFIG_SCHED_SMT
7330 SD_INIT_FUNC(SIBLING)
7331#endif
7332#ifdef CONFIG_SCHED_MC
7333 SD_INIT_FUNC(MC)
7334#endif
7335
7336/*
7337 * To minimize stack usage kmalloc room for cpumasks and share the
7338 * space as the usage in build_sched_domains() dictates. Used only
7339 * if the amount of space is significant.
7340 */
7341struct allmasks {
7342 cpumask_t tmpmask; /* make this one first */
7343 union {
7344 cpumask_t nodemask;
7345 cpumask_t this_sibling_map;
7346 cpumask_t this_core_map;
7347 };
7348 cpumask_t send_covered;
7349
7350#ifdef CONFIG_NUMA
7351 cpumask_t domainspan;
7352 cpumask_t covered;
7353 cpumask_t notcovered;
7354#endif
7355};
7356
7357#if NR_CPUS > 128
7358#define SCHED_CPUMASK_ALLOC 1
7359#define SCHED_CPUMASK_FREE(v) kfree(v)
7360#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7361#else
7362#define SCHED_CPUMASK_ALLOC 0
7363#define SCHED_CPUMASK_FREE(v)
7364#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7365#endif
7366
7367#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7368 ((unsigned long)(a) + offsetof(struct allmasks, v))
7369
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007370static int default_relax_domain_level = -1;
7371
7372static int __init setup_relax_domain_level(char *str)
7373{
7374 default_relax_domain_level = simple_strtoul(str, NULL, 0);
7375 return 1;
7376}
7377__setup("relax_domain_level=", setup_relax_domain_level);
7378
7379static void set_domain_attribute(struct sched_domain *sd,
7380 struct sched_domain_attr *attr)
7381{
7382 int request;
7383
7384 if (!attr || attr->relax_domain_level < 0) {
7385 if (default_relax_domain_level < 0)
7386 return;
7387 else
7388 request = default_relax_domain_level;
7389 } else
7390 request = attr->relax_domain_level;
7391 if (request < sd->level) {
7392 /* turn off idle balance on this domain */
7393 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7394 } else {
7395 /* turn on idle balance on this domain */
7396 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7397 }
7398}
7399
Mike Travis7c16ec52008-04-04 18:11:11 -07007400/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007401 * Build sched domains for a given set of cpus and attach the sched domains
7402 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007404static int __build_sched_domains(const cpumask_t *cpu_map,
7405 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406{
7407 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007408 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007409 SCHED_CPUMASK_DECLARE(allmasks);
7410 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007411#ifdef CONFIG_NUMA
7412 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007413 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007414
7415 /*
7416 * Allocate the per-node list of sched groups
7417 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007418 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007419 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007420 if (!sched_group_nodes) {
7421 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007422 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007423 }
John Hawkesd1b55132005-09-06 15:18:14 -07007424#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425
Gregory Haskinsdc938522008-01-25 21:08:26 +01007426 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007427 if (!rd) {
7428 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007429#ifdef CONFIG_NUMA
7430 kfree(sched_group_nodes);
7431#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007432 return -ENOMEM;
7433 }
7434
Mike Travis7c16ec52008-04-04 18:11:11 -07007435#if SCHED_CPUMASK_ALLOC
7436 /* get space for all scratch cpumask variables */
7437 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7438 if (!allmasks) {
7439 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7440 kfree(rd);
7441#ifdef CONFIG_NUMA
7442 kfree(sched_group_nodes);
7443#endif
7444 return -ENOMEM;
7445 }
7446#endif
7447 tmpmask = (cpumask_t *)allmasks;
7448
7449
7450#ifdef CONFIG_NUMA
7451 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7452#endif
7453
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007455 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007457 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007459 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460
Mike Travis7c16ec52008-04-04 18:11:11 -07007461 *nodemask = node_to_cpumask(cpu_to_node(i));
7462 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463
7464#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007465 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007466 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007467 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007468 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007469 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007470 sd->span = *cpu_map;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007471 sd->first_cpu = first_cpu(sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07007472 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007473 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007474 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007475 } else
7476 p = NULL;
7477
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007479 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007480 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007481 sched_domain_node_span(cpu_to_node(i), &sd->span);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007482 sd->first_cpu = first_cpu(sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007483 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007484 if (p)
7485 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007486 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487#endif
7488
7489 p = sd;
7490 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007491 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007492 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007493 sd->span = *nodemask;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007494 sd->first_cpu = first_cpu(sd->span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007496 if (p)
7497 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007498 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007500#ifdef CONFIG_SCHED_MC
7501 p = sd;
7502 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007503 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007504 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007505 sd->span = cpu_coregroup_map(i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007506 sd->first_cpu = first_cpu(sd->span);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007507 cpus_and(sd->span, sd->span, *cpu_map);
7508 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007509 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007510 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007511#endif
7512
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513#ifdef CONFIG_SCHED_SMT
7514 p = sd;
7515 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007516 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007517 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007518 sd->span = per_cpu(cpu_sibling_map, i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007519 sd->first_cpu = first_cpu(sd->span);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007520 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007522 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007523 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524#endif
7525 }
7526
7527#ifdef CONFIG_SCHED_SMT
7528 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007529 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007530 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7531 SCHED_CPUMASK_VAR(send_covered, allmasks);
7532
7533 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7534 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7535 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 continue;
7537
Ingo Molnardd41f592007-07-09 18:51:59 +02007538 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007539 &cpu_to_cpu_group,
7540 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541 }
7542#endif
7543
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007544#ifdef CONFIG_SCHED_MC
7545 /* Set up multi-core groups */
7546 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007547 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7548 SCHED_CPUMASK_VAR(send_covered, allmasks);
7549
7550 *this_core_map = cpu_coregroup_map(i);
7551 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7552 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007553 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007554
Ingo Molnardd41f592007-07-09 18:51:59 +02007555 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007556 &cpu_to_core_group,
7557 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007558 }
7559#endif
7560
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 /* Set up physical groups */
7562 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007563 SCHED_CPUMASK_VAR(nodemask, allmasks);
7564 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565
Mike Travis7c16ec52008-04-04 18:11:11 -07007566 *nodemask = node_to_cpumask(i);
7567 cpus_and(*nodemask, *nodemask, *cpu_map);
7568 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 continue;
7570
Mike Travis7c16ec52008-04-04 18:11:11 -07007571 init_sched_build_groups(nodemask, cpu_map,
7572 &cpu_to_phys_group,
7573 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 }
7575
7576#ifdef CONFIG_NUMA
7577 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007578 if (sd_allnodes) {
7579 SCHED_CPUMASK_VAR(send_covered, allmasks);
7580
7581 init_sched_build_groups(cpu_map, cpu_map,
7582 &cpu_to_allnodes_group,
7583 send_covered, tmpmask);
7584 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007585
7586 for (i = 0; i < MAX_NUMNODES; i++) {
7587 /* Set up node groups */
7588 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007589 SCHED_CPUMASK_VAR(nodemask, allmasks);
7590 SCHED_CPUMASK_VAR(domainspan, allmasks);
7591 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007592 int j;
7593
Mike Travis7c16ec52008-04-04 18:11:11 -07007594 *nodemask = node_to_cpumask(i);
7595 cpus_clear(*covered);
7596
7597 cpus_and(*nodemask, *nodemask, *cpu_map);
7598 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007599 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007600 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007601 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007602
Mike Travis4bdbaad32008-04-15 16:35:52 -07007603 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007604 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007605
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007606 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007607 if (!sg) {
7608 printk(KERN_WARNING "Can not alloc domain group for "
7609 "node %d\n", i);
7610 goto error;
7611 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007612 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007613 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007614 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007615
John Hawkes9c1cfda2005-09-06 15:18:14 -07007616 sd = &per_cpu(node_domains, j);
7617 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007618 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007619 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007620 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007621 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007622 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007623 prev = sg;
7624
7625 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007626 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007627 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007628 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007629
Mike Travis7c16ec52008-04-04 18:11:11 -07007630 cpus_complement(*notcovered, *covered);
7631 cpus_and(*tmpmask, *notcovered, *cpu_map);
7632 cpus_and(*tmpmask, *tmpmask, *domainspan);
7633 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007634 break;
7635
Mike Travis7c16ec52008-04-04 18:11:11 -07007636 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7637 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007638 continue;
7639
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007640 sg = kmalloc_node(sizeof(struct sched_group),
7641 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007642 if (!sg) {
7643 printk(KERN_WARNING
7644 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007645 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007646 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007647 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007648 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007649 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007650 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007651 prev->next = sg;
7652 prev = sg;
7653 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655#endif
7656
7657 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007658#ifdef CONFIG_SCHED_SMT
7659 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007660 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7661
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007662 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007663 }
7664#endif
7665#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007666 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007667 struct sched_domain *sd = &per_cpu(core_domains, i);
7668
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007669 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007670 }
7671#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007673 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007674 struct sched_domain *sd = &per_cpu(phys_domains, i);
7675
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007676 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 }
7678
John Hawkes9c1cfda2005-09-06 15:18:14 -07007679#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007680 for (i = 0; i < MAX_NUMNODES; i++)
7681 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007682
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007683 if (sd_allnodes) {
7684 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007685
Mike Travis7c16ec52008-04-04 18:11:11 -07007686 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7687 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007688 init_numa_sched_groups_power(sg);
7689 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007690#endif
7691
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007693 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 struct sched_domain *sd;
7695#ifdef CONFIG_SCHED_SMT
7696 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007697#elif defined(CONFIG_SCHED_MC)
7698 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699#else
7700 sd = &per_cpu(phys_domains, i);
7701#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007702 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007704
Mike Travis7c16ec52008-04-04 18:11:11 -07007705 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007706 return 0;
7707
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007708#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007709error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007710 free_sched_groups(cpu_map, tmpmask);
7711 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007712 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007713#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714}
Paul Jackson029190c2007-10-18 23:40:20 -07007715
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007716static int build_sched_domains(const cpumask_t *cpu_map)
7717{
7718 return __build_sched_domains(cpu_map, NULL);
7719}
7720
Paul Jackson029190c2007-10-18 23:40:20 -07007721static cpumask_t *doms_cur; /* current sched domains */
7722static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007723static struct sched_domain_attr *dattr_cur; /* attribues of custom domains
7724 in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007725
7726/*
7727 * Special case: If a kmalloc of a doms_cur partition (array of
7728 * cpumask_t) fails, then fallback to a single sched domain,
7729 * as determined by the single cpumask_t fallback_doms.
7730 */
7731static cpumask_t fallback_doms;
7732
Heiko Carstens22e52b02008-03-12 18:31:59 +01007733void __attribute__((weak)) arch_update_cpu_topology(void)
7734{
7735}
7736
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007737/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007738 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007739 * For now this just excludes isolated cpus, but could be used to
7740 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007741 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007742static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007743{
Milton Miller73785472007-10-24 18:23:48 +02007744 int err;
7745
Heiko Carstens22e52b02008-03-12 18:31:59 +01007746 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007747 ndoms_cur = 1;
7748 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7749 if (!doms_cur)
7750 doms_cur = &fallback_doms;
7751 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007752 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007753 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007754 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007755
7756 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007757}
7758
Mike Travis7c16ec52008-04-04 18:11:11 -07007759static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7760 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761{
Mike Travis7c16ec52008-04-04 18:11:11 -07007762 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007763}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007765/*
7766 * Detach sched domains from a group of cpus specified in cpu_map
7767 * These cpus will now be attached to the NULL domain
7768 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007769static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007770{
Mike Travis7c16ec52008-04-04 18:11:11 -07007771 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007772 int i;
7773
Milton Miller6382bc92007-10-15 17:00:19 +02007774 unregister_sched_domain_sysctl();
7775
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007776 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007777 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007778 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007779 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007780}
7781
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007782/* handle null as "default" */
7783static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7784 struct sched_domain_attr *new, int idx_new)
7785{
7786 struct sched_domain_attr tmp;
7787
7788 /* fast path */
7789 if (!new && !cur)
7790 return 1;
7791
7792 tmp = SD_ATTR_INIT;
7793 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7794 new ? (new + idx_new) : &tmp,
7795 sizeof(struct sched_domain_attr));
7796}
7797
Paul Jackson029190c2007-10-18 23:40:20 -07007798/*
7799 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007800 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007801 * doms_new[] to the current sched domain partitioning, doms_cur[].
7802 * It destroys each deleted domain and builds each new domain.
7803 *
7804 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007805 * The masks don't intersect (don't overlap.) We should setup one
7806 * sched domain for each mask. CPUs not in any of the cpumasks will
7807 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007808 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7809 * it as it is.
7810 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007811 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7812 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007813 * failed the kmalloc call, then it can pass in doms_new == NULL,
7814 * and partition_sched_domains() will fallback to the single partition
7815 * 'fallback_doms'.
7816 *
7817 * Call with hotplug lock held
7818 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007819void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7820 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007821{
7822 int i, j;
7823
Heiko Carstens712555e2008-04-28 11:33:07 +02007824 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007825
Milton Miller73785472007-10-24 18:23:48 +02007826 /* always unregister in case we don't destroy any domains */
7827 unregister_sched_domain_sysctl();
7828
Paul Jackson029190c2007-10-18 23:40:20 -07007829 if (doms_new == NULL) {
7830 ndoms_new = 1;
7831 doms_new = &fallback_doms;
7832 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007833 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007834 }
7835
7836 /* Destroy deleted domains */
7837 for (i = 0; i < ndoms_cur; i++) {
7838 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007839 if (cpus_equal(doms_cur[i], doms_new[j])
7840 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007841 goto match1;
7842 }
7843 /* no match - a current sched domain not in new doms_new[] */
7844 detach_destroy_domains(doms_cur + i);
7845match1:
7846 ;
7847 }
7848
7849 /* Build new domains */
7850 for (i = 0; i < ndoms_new; i++) {
7851 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007852 if (cpus_equal(doms_new[i], doms_cur[j])
7853 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007854 goto match2;
7855 }
7856 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007857 __build_sched_domains(doms_new + i,
7858 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007859match2:
7860 ;
7861 }
7862
7863 /* Remember the new sched domains */
7864 if (doms_cur != &fallback_doms)
7865 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007866 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007867 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007868 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007869 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007870
7871 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007872
Heiko Carstens712555e2008-04-28 11:33:07 +02007873 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007874}
7875
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007876#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007877int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007878{
7879 int err;
7880
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007881 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007882 mutex_lock(&sched_domains_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007883 detach_destroy_domains(&cpu_online_map);
7884 err = arch_init_sched_domains(&cpu_online_map);
Heiko Carstens712555e2008-04-28 11:33:07 +02007885 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007886 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007887
7888 return err;
7889}
7890
7891static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7892{
7893 int ret;
7894
7895 if (buf[0] != '0' && buf[0] != '1')
7896 return -EINVAL;
7897
7898 if (smt)
7899 sched_smt_power_savings = (buf[0] == '1');
7900 else
7901 sched_mc_power_savings = (buf[0] == '1');
7902
7903 ret = arch_reinit_sched_domains();
7904
7905 return ret ? ret : count;
7906}
7907
Adrian Bunk6707de002007-08-12 18:08:19 +02007908#ifdef CONFIG_SCHED_MC
7909static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7910{
7911 return sprintf(page, "%u\n", sched_mc_power_savings);
7912}
7913static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7914 const char *buf, size_t count)
7915{
7916 return sched_power_savings_store(buf, count, 0);
7917}
7918static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7919 sched_mc_power_savings_store);
7920#endif
7921
7922#ifdef CONFIG_SCHED_SMT
7923static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7924{
7925 return sprintf(page, "%u\n", sched_smt_power_savings);
7926}
7927static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7928 const char *buf, size_t count)
7929{
7930 return sched_power_savings_store(buf, count, 1);
7931}
7932static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7933 sched_smt_power_savings_store);
7934#endif
7935
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007936int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7937{
7938 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007939
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007940#ifdef CONFIG_SCHED_SMT
7941 if (smt_capable())
7942 err = sysfs_create_file(&cls->kset.kobj,
7943 &attr_sched_smt_power_savings.attr);
7944#endif
7945#ifdef CONFIG_SCHED_MC
7946 if (!err && mc_capable())
7947 err = sysfs_create_file(&cls->kset.kobj,
7948 &attr_sched_mc_power_savings.attr);
7949#endif
7950 return err;
7951}
7952#endif
7953
Linus Torvalds1da177e2005-04-16 15:20:36 -07007954/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007955 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007957 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958 * which will prevent rebalancing while the sched domains are recalculated.
7959 */
7960static int update_sched_domains(struct notifier_block *nfb,
7961 unsigned long action, void *hcpu)
7962{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963 switch (action) {
7964 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007965 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007967 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007968 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969 return NOTIFY_OK;
7970
7971 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007972 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007974 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007975 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007976 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007977 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007978 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979 /*
7980 * Fall through and re-initialise the domains.
7981 */
7982 break;
7983 default:
7984 return NOTIFY_DONE;
7985 }
7986
7987 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007988 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989
7990 return NOTIFY_OK;
7991}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992
7993void __init sched_init_smp(void)
7994{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007995 cpumask_t non_isolated_cpus;
7996
Mike Travis434d53b2008-04-04 18:11:04 -07007997#if defined(CONFIG_NUMA)
7998 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7999 GFP_KERNEL);
8000 BUG_ON(sched_group_nodes_bycpu == NULL);
8001#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008002 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02008003 mutex_lock(&sched_domains_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008004 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08008005 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07008006 if (cpus_empty(non_isolated_cpus))
8007 cpu_set(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02008008 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008009 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010 /* XXX: Theoretical race here - CPU may be hotplugged now */
8011 hotcpu_notifier(update_sched_domains, 0);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02008012 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07008013
8014 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07008015 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07008016 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01008017 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008018}
8019#else
8020void __init sched_init_smp(void)
8021{
Ingo Molnar19978ca2007-11-09 22:39:38 +01008022 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023}
8024#endif /* CONFIG_SMP */
8025
8026int in_sched_functions(unsigned long addr)
8027{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028 return in_lock_functions(addr) ||
8029 (addr >= (unsigned long)__sched_text_start
8030 && addr < (unsigned long)__sched_text_end);
8031}
8032
Alexey Dobriyana9957442007-10-15 17:00:13 +02008033static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02008034{
8035 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02008036 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02008037#ifdef CONFIG_FAIR_GROUP_SCHED
8038 cfs_rq->rq = rq;
8039#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02008040 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02008041}
8042
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008043static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
8044{
8045 struct rt_prio_array *array;
8046 int i;
8047
8048 array = &rt_rq->active;
8049 for (i = 0; i < MAX_RT_PRIO; i++) {
8050 INIT_LIST_HEAD(array->queue + i);
8051 __clear_bit(i, array->bitmap);
8052 }
8053 /* delimiter for bitsearch: */
8054 __set_bit(MAX_RT_PRIO, array->bitmap);
8055
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008056#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01008057 rt_rq->highest_prio = MAX_RT_PRIO;
8058#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008059#ifdef CONFIG_SMP
8060 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008061 rt_rq->overloaded = 0;
8062#endif
8063
8064 rt_rq->rt_time = 0;
8065 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008066 rt_rq->rt_runtime = 0;
8067 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008068
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008069#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01008070 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008071 rt_rq->rq = rq;
8072#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008073}
8074
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008075#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008076static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
8077 struct sched_entity *se, int cpu, int add,
8078 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008079{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008080 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008081 tg->cfs_rq[cpu] = cfs_rq;
8082 init_cfs_rq(cfs_rq, rq);
8083 cfs_rq->tg = tg;
8084 if (add)
8085 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
8086
8087 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008088 /* se could be NULL for init_task_group */
8089 if (!se)
8090 return;
8091
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008092 if (!parent)
8093 se->cfs_rq = &rq->cfs;
8094 else
8095 se->cfs_rq = parent->my_q;
8096
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008097 se->my_q = cfs_rq;
8098 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008099 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008100 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008101}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008102#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008103
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008104#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008105static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8106 struct sched_rt_entity *rt_se, int cpu, int add,
8107 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008108{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008109 struct rq *rq = cpu_rq(cpu);
8110
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008111 tg->rt_rq[cpu] = rt_rq;
8112 init_rt_rq(rt_rq, rq);
8113 rt_rq->tg = tg;
8114 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008115 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008116 if (add)
8117 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8118
8119 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008120 if (!rt_se)
8121 return;
8122
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008123 if (!parent)
8124 rt_se->rt_rq = &rq->rt;
8125 else
8126 rt_se->rt_rq = parent->my_q;
8127
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008128 rt_se->rt_rq = &rq->rt;
8129 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008130 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008131 INIT_LIST_HEAD(&rt_se->run_list);
8132}
8133#endif
8134
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135void __init sched_init(void)
8136{
Ingo Molnardd41f592007-07-09 18:51:59 +02008137 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008138 unsigned long alloc_size = 0, ptr;
8139
8140#ifdef CONFIG_FAIR_GROUP_SCHED
8141 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8142#endif
8143#ifdef CONFIG_RT_GROUP_SCHED
8144 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8145#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008146#ifdef CONFIG_USER_SCHED
8147 alloc_size *= 2;
8148#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008149 /*
8150 * As sched_init() is called before page_alloc is setup,
8151 * we use alloc_bootmem().
8152 */
8153 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07008154 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07008155
8156#ifdef CONFIG_FAIR_GROUP_SCHED
8157 init_task_group.se = (struct sched_entity **)ptr;
8158 ptr += nr_cpu_ids * sizeof(void **);
8159
8160 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8161 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008162
8163#ifdef CONFIG_USER_SCHED
8164 root_task_group.se = (struct sched_entity **)ptr;
8165 ptr += nr_cpu_ids * sizeof(void **);
8166
8167 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8168 ptr += nr_cpu_ids * sizeof(void **);
8169#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008170#endif
8171#ifdef CONFIG_RT_GROUP_SCHED
8172 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8173 ptr += nr_cpu_ids * sizeof(void **);
8174
8175 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008176 ptr += nr_cpu_ids * sizeof(void **);
8177
8178#ifdef CONFIG_USER_SCHED
8179 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8180 ptr += nr_cpu_ids * sizeof(void **);
8181
8182 root_task_group.rt_rq = (struct rt_rq **)ptr;
8183 ptr += nr_cpu_ids * sizeof(void **);
8184#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008185#endif
8186 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008187
Gregory Haskins57d885f2008-01-25 21:08:18 +01008188#ifdef CONFIG_SMP
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008189 init_aggregate();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008190 init_defrootdomain();
8191#endif
8192
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008193 init_rt_bandwidth(&def_rt_bandwidth,
8194 global_rt_period(), global_rt_runtime());
8195
8196#ifdef CONFIG_RT_GROUP_SCHED
8197 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8198 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008199#ifdef CONFIG_USER_SCHED
8200 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8201 global_rt_period(), RUNTIME_INF);
8202#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008203#endif
8204
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008205#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008206 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008207 INIT_LIST_HEAD(&init_task_group.children);
8208
8209#ifdef CONFIG_USER_SCHED
8210 INIT_LIST_HEAD(&root_task_group.children);
8211 init_task_group.parent = &root_task_group;
8212 list_add(&init_task_group.siblings, &root_task_group.children);
8213#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008214#endif
8215
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008216 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008217 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218
8219 rq = cpu_rq(i);
8220 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07008221 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07008222 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008223 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02008224 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02008225 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008226 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008227#ifdef CONFIG_FAIR_GROUP_SCHED
8228 init_task_group.shares = init_task_group_load;
8229 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008230#ifdef CONFIG_CGROUP_SCHED
8231 /*
8232 * How much cpu bandwidth does init_task_group get?
8233 *
8234 * In case of task-groups formed thr' the cgroup filesystem, it
8235 * gets 100% of the cpu resources in the system. This overall
8236 * system cpu resource is divided among the tasks of
8237 * init_task_group and its child task-groups in a fair manner,
8238 * based on each entity's (task or task-group's) weight
8239 * (se->load.weight).
8240 *
8241 * In other words, if init_task_group has 10 tasks of weight
8242 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8243 * then A0's share of the cpu resource is:
8244 *
8245 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8246 *
8247 * We achieve this by letting init_task_group's tasks sit
8248 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8249 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008250 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008251#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008252 root_task_group.shares = NICE_0_LOAD;
8253 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008254 /*
8255 * In case of task-groups formed thr' the user id of tasks,
8256 * init_task_group represents tasks belonging to root user.
8257 * Hence it forms a sibling of all subsequent groups formed.
8258 * In this case, init_task_group gets only a fraction of overall
8259 * system cpu resource, based on the weight assigned to root
8260 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8261 * by letting tasks of init_task_group sit in a separate cfs_rq
8262 * (init_cfs_rq) and having one entity represent this group of
8263 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8264 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008265 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008266 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008267 &per_cpu(init_sched_entity, i), i, 1,
8268 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008269
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008270#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008271#endif /* CONFIG_FAIR_GROUP_SCHED */
8272
8273 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008274#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008275 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008276#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008277 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008278#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008279 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008280 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008281 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008282 &per_cpu(init_sched_rt_entity, i), i, 1,
8283 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008284#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008286
Ingo Molnardd41f592007-07-09 18:51:59 +02008287 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8288 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008290 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008291 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008293 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008295 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008296 rq->migration_thread = NULL;
8297 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008298 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008300 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302 }
8303
Peter Williams2dd73a42006-06-27 02:54:34 -07008304 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008305
Avi Kivitye107be32007-07-26 13:40:43 +02008306#ifdef CONFIG_PREEMPT_NOTIFIERS
8307 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8308#endif
8309
Christoph Lameterc9819f42006-12-10 02:20:25 -08008310#ifdef CONFIG_SMP
8311 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8312#endif
8313
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008314#ifdef CONFIG_RT_MUTEXES
8315 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8316#endif
8317
Linus Torvalds1da177e2005-04-16 15:20:36 -07008318 /*
8319 * The boot idle thread does lazy MMU switching as well:
8320 */
8321 atomic_inc(&init_mm.mm_count);
8322 enter_lazy_tlb(&init_mm, current);
8323
8324 /*
8325 * Make us the idle thread. Technically, schedule() should not be
8326 * called from this thread, however somewhere below it might be,
8327 * but because we are the idle thread, we just pick up running again
8328 * when this runqueue becomes "idle".
8329 */
8330 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008331 /*
8332 * During early bootup we pretend to be a normal task:
8333 */
8334 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008335
8336 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008337}
8338
8339#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8340void __might_sleep(char *file, int line)
8341{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008342#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343 static unsigned long prev_jiffy; /* ratelimiting */
8344
8345 if ((in_atomic() || irqs_disabled()) &&
8346 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8347 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8348 return;
8349 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008350 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351 " context at %s:%d\n", file, line);
8352 printk("in_atomic():%d, irqs_disabled():%d\n",
8353 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008354 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008355 if (irqs_disabled())
8356 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357 dump_stack();
8358 }
8359#endif
8360}
8361EXPORT_SYMBOL(__might_sleep);
8362#endif
8363
8364#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008365static void normalize_task(struct rq *rq, struct task_struct *p)
8366{
8367 int on_rq;
8368 update_rq_clock(rq);
8369 on_rq = p->se.on_rq;
8370 if (on_rq)
8371 deactivate_task(rq, p, 0);
8372 __setscheduler(rq, p, SCHED_NORMAL, 0);
8373 if (on_rq) {
8374 activate_task(rq, p, 0);
8375 resched_task(rq->curr);
8376 }
8377}
8378
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379void normalize_rt_tasks(void)
8380{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008381 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008383 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008385 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008386 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008387 /*
8388 * Only normalize user tasks:
8389 */
8390 if (!p->mm)
8391 continue;
8392
Ingo Molnardd41f592007-07-09 18:51:59 +02008393 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008394#ifdef CONFIG_SCHEDSTATS
8395 p->se.wait_start = 0;
8396 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008397 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008398#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008399 task_rq(p)->clock = 0;
8400
8401 if (!rt_task(p)) {
8402 /*
8403 * Renice negative nice level userspace
8404 * tasks back to 0:
8405 */
8406 if (TASK_NICE(p) < 0 && p->mm)
8407 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008411 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008412 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413
Ingo Molnar178be792007-10-15 17:00:18 +02008414 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008415
Ingo Molnarb29739f2006-06-27 02:54:51 -07008416 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008417 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008418 } while_each_thread(g, p);
8419
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008420 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421}
8422
8423#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008424
8425#ifdef CONFIG_IA64
8426/*
8427 * These functions are only useful for the IA64 MCA handling.
8428 *
8429 * They can only be called when the whole system has been
8430 * stopped - every CPU needs to be quiescent, and no scheduling
8431 * activity can take place. Using them for anything else would
8432 * be a serious bug, and as a result, they aren't even visible
8433 * under any other configuration.
8434 */
8435
8436/**
8437 * curr_task - return the current task for a given cpu.
8438 * @cpu: the processor in question.
8439 *
8440 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8441 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008442struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008443{
8444 return cpu_curr(cpu);
8445}
8446
8447/**
8448 * set_curr_task - set the current task for a given cpu.
8449 * @cpu: the processor in question.
8450 * @p: the task pointer to set.
8451 *
8452 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008453 * are serviced on a separate stack. It allows the architecture to switch the
8454 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008455 * must be called with all CPU's synchronized, and interrupts disabled, the
8456 * and caller must save the original value of the current task (see
8457 * curr_task() above) and restore that value before reenabling interrupts and
8458 * re-starting the system.
8459 *
8460 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8461 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008462void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008463{
8464 cpu_curr(cpu) = p;
8465}
8466
8467#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008468
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008469#ifdef CONFIG_FAIR_GROUP_SCHED
8470static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008471{
8472 int i;
8473
8474 for_each_possible_cpu(i) {
8475 if (tg->cfs_rq)
8476 kfree(tg->cfs_rq[i]);
8477 if (tg->se)
8478 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008479 }
8480
8481 kfree(tg->cfs_rq);
8482 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008483}
8484
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008485static
8486int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008487{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008488 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008489 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008490 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008491 int i;
8492
Mike Travis434d53b2008-04-04 18:11:04 -07008493 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008494 if (!tg->cfs_rq)
8495 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008496 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008497 if (!tg->se)
8498 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008499
8500 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008501
8502 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008503 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008504
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008505 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8506 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008507 if (!cfs_rq)
8508 goto err;
8509
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008510 se = kmalloc_node(sizeof(struct sched_entity),
8511 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008512 if (!se)
8513 goto err;
8514
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008515 parent_se = parent ? parent->se[i] : NULL;
8516 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008517 }
8518
8519 return 1;
8520
8521 err:
8522 return 0;
8523}
8524
8525static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8526{
8527 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8528 &cpu_rq(cpu)->leaf_cfs_rq_list);
8529}
8530
8531static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8532{
8533 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8534}
8535#else
8536static inline void free_fair_sched_group(struct task_group *tg)
8537{
8538}
8539
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008540static inline
8541int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008542{
8543 return 1;
8544}
8545
8546static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8547{
8548}
8549
8550static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8551{
8552}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008553#endif
8554
8555#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008556static void free_rt_sched_group(struct task_group *tg)
8557{
8558 int i;
8559
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008560 destroy_rt_bandwidth(&tg->rt_bandwidth);
8561
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008562 for_each_possible_cpu(i) {
8563 if (tg->rt_rq)
8564 kfree(tg->rt_rq[i]);
8565 if (tg->rt_se)
8566 kfree(tg->rt_se[i]);
8567 }
8568
8569 kfree(tg->rt_rq);
8570 kfree(tg->rt_se);
8571}
8572
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008573static
8574int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008575{
8576 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008577 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008578 struct rq *rq;
8579 int i;
8580
Mike Travis434d53b2008-04-04 18:11:04 -07008581 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008582 if (!tg->rt_rq)
8583 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008584 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008585 if (!tg->rt_se)
8586 goto err;
8587
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008588 init_rt_bandwidth(&tg->rt_bandwidth,
8589 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008590
8591 for_each_possible_cpu(i) {
8592 rq = cpu_rq(i);
8593
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008594 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8595 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8596 if (!rt_rq)
8597 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008598
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008599 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8600 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8601 if (!rt_se)
8602 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008603
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008604 parent_se = parent ? parent->rt_se[i] : NULL;
8605 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008606 }
8607
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008608 return 1;
8609
8610 err:
8611 return 0;
8612}
8613
8614static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8615{
8616 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8617 &cpu_rq(cpu)->leaf_rt_rq_list);
8618}
8619
8620static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8621{
8622 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8623}
8624#else
8625static inline void free_rt_sched_group(struct task_group *tg)
8626{
8627}
8628
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008629static inline
8630int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008631{
8632 return 1;
8633}
8634
8635static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8636{
8637}
8638
8639static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8640{
8641}
8642#endif
8643
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008644#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008645static void free_sched_group(struct task_group *tg)
8646{
8647 free_fair_sched_group(tg);
8648 free_rt_sched_group(tg);
8649 kfree(tg);
8650}
8651
8652/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008653struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008654{
8655 struct task_group *tg;
8656 unsigned long flags;
8657 int i;
8658
8659 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8660 if (!tg)
8661 return ERR_PTR(-ENOMEM);
8662
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008663 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008664 goto err;
8665
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008666 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008667 goto err;
8668
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008669 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008670 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008671 register_fair_sched_group(tg, i);
8672 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008673 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008674 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008675
8676 WARN_ON(!parent); /* root should already exist */
8677
8678 tg->parent = parent;
8679 list_add_rcu(&tg->siblings, &parent->children);
8680 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008681 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008682
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008683 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008684
8685err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008686 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008687 return ERR_PTR(-ENOMEM);
8688}
8689
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008690/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008691static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008692{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008693 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008694 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008695}
8696
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008697/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008698void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008699{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008700 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008701 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008702
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008703 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008704 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008705 unregister_fair_sched_group(tg, i);
8706 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008707 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008708 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008709 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008710 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008711
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008712 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008713 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008714}
8715
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008716/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008717 * The caller of this function should have put the task in its new group
8718 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8719 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008720 */
8721void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008722{
8723 int on_rq, running;
8724 unsigned long flags;
8725 struct rq *rq;
8726
8727 rq = task_rq_lock(tsk, &flags);
8728
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008729 update_rq_clock(rq);
8730
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008731 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008732 on_rq = tsk->se.on_rq;
8733
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008734 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008735 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008736 if (unlikely(running))
8737 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008738
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008739 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008740
Peter Zijlstra810b3812008-02-29 15:21:01 -05008741#ifdef CONFIG_FAIR_GROUP_SCHED
8742 if (tsk->sched_class->moved_group)
8743 tsk->sched_class->moved_group(tsk);
8744#endif
8745
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008746 if (unlikely(running))
8747 tsk->sched_class->set_curr_task(rq);
8748 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008749 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008750
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008751 task_rq_unlock(rq, &flags);
8752}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008753#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008754
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008755#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008756static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008757{
8758 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008759 int on_rq;
8760
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008761 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008762 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008763 dequeue_entity(cfs_rq, se, 0);
8764
8765 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008766 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008767
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008768 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008769 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008770}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008771
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008772static void set_se_shares(struct sched_entity *se, unsigned long shares)
8773{
8774 struct cfs_rq *cfs_rq = se->cfs_rq;
8775 struct rq *rq = cfs_rq->rq;
8776 unsigned long flags;
8777
8778 spin_lock_irqsave(&rq->lock, flags);
8779 __set_se_shares(se, shares);
8780 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008781}
8782
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008783static DEFINE_MUTEX(shares_mutex);
8784
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008785int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008786{
8787 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008788 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008789
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008790 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008791 * We can't change the weight of the root cgroup.
8792 */
8793 if (!tg->se[0])
8794 return -EINVAL;
8795
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008796 if (shares < MIN_SHARES)
8797 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008798 else if (shares > MAX_SHARES)
8799 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008800
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008801 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008802 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008803 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008804
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008805 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008806 for_each_possible_cpu(i)
8807 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008808 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008809 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008810
8811 /* wait for any ongoing reference to this group to finish */
8812 synchronize_sched();
8813
8814 /*
8815 * Now we are free to modify the group's share on each cpu
8816 * w/o tripping rebalance_share or load_balance_fair.
8817 */
8818 tg->shares = shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008819 for_each_possible_cpu(i) {
8820 /*
8821 * force a rebalance
8822 */
8823 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008824 set_se_shares(tg->se[i], shares);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008825 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008826
8827 /*
8828 * Enable load balance activity on this group, by inserting it back on
8829 * each cpu's rq->leaf_cfs_rq_list.
8830 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008831 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008832 for_each_possible_cpu(i)
8833 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008834 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008835 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008836done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008837 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008838 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008839}
8840
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008841unsigned long sched_group_shares(struct task_group *tg)
8842{
8843 return tg->shares;
8844}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008845#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008846
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008847#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008848/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008849 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008850 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008851static DEFINE_MUTEX(rt_constraints_mutex);
8852
8853static unsigned long to_ratio(u64 period, u64 runtime)
8854{
8855 if (runtime == RUNTIME_INF)
8856 return 1ULL << 16;
8857
Roman Zippel6f6d6a12008-05-01 04:34:28 -07008858 return div64_u64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008859}
8860
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008861#ifdef CONFIG_CGROUP_SCHED
8862static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8863{
8864 struct task_group *tgi, *parent = tg->parent;
8865 unsigned long total = 0;
8866
8867 if (!parent) {
8868 if (global_rt_period() < period)
8869 return 0;
8870
8871 return to_ratio(period, runtime) <
8872 to_ratio(global_rt_period(), global_rt_runtime());
8873 }
8874
8875 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8876 return 0;
8877
8878 rcu_read_lock();
8879 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8880 if (tgi == tg)
8881 continue;
8882
8883 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8884 tgi->rt_bandwidth.rt_runtime);
8885 }
8886 rcu_read_unlock();
8887
8888 return total + to_ratio(period, runtime) <
8889 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8890 parent->rt_bandwidth.rt_runtime);
8891}
8892#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008893static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008894{
8895 struct task_group *tgi;
8896 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008897 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008898 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008899
8900 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008901 list_for_each_entry_rcu(tgi, &task_groups, list) {
8902 if (tgi == tg)
8903 continue;
8904
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008905 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8906 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008907 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008908 rcu_read_unlock();
8909
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008910 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008911}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008912#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008913
Dhaval Giani521f1a242008-02-28 15:21:56 +05308914/* Must be called with tasklist_lock held */
8915static inline int tg_has_rt_tasks(struct task_group *tg)
8916{
8917 struct task_struct *g, *p;
8918 do_each_thread(g, p) {
8919 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8920 return 1;
8921 } while_each_thread(g, p);
8922 return 0;
8923}
8924
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008925static int tg_set_bandwidth(struct task_group *tg,
8926 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008927{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008928 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008929
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008930 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308931 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008932 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308933 err = -EBUSY;
8934 goto unlock;
8935 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008936 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8937 err = -EINVAL;
8938 goto unlock;
8939 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008940
8941 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008942 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8943 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008944
8945 for_each_possible_cpu(i) {
8946 struct rt_rq *rt_rq = tg->rt_rq[i];
8947
8948 spin_lock(&rt_rq->rt_runtime_lock);
8949 rt_rq->rt_runtime = rt_runtime;
8950 spin_unlock(&rt_rq->rt_runtime_lock);
8951 }
8952 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008953 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308954 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008955 mutex_unlock(&rt_constraints_mutex);
8956
8957 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008958}
8959
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008960int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8961{
8962 u64 rt_runtime, rt_period;
8963
8964 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8965 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8966 if (rt_runtime_us < 0)
8967 rt_runtime = RUNTIME_INF;
8968
8969 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8970}
8971
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008972long sched_group_rt_runtime(struct task_group *tg)
8973{
8974 u64 rt_runtime_us;
8975
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008976 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008977 return -1;
8978
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008979 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008980 do_div(rt_runtime_us, NSEC_PER_USEC);
8981 return rt_runtime_us;
8982}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008983
8984int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8985{
8986 u64 rt_runtime, rt_period;
8987
8988 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8989 rt_runtime = tg->rt_bandwidth.rt_runtime;
8990
8991 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8992}
8993
8994long sched_group_rt_period(struct task_group *tg)
8995{
8996 u64 rt_period_us;
8997
8998 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8999 do_div(rt_period_us, NSEC_PER_USEC);
9000 return rt_period_us;
9001}
9002
9003static int sched_rt_global_constraints(void)
9004{
9005 int ret = 0;
9006
9007 mutex_lock(&rt_constraints_mutex);
9008 if (!__rt_schedulable(NULL, 1, 0))
9009 ret = -EINVAL;
9010 mutex_unlock(&rt_constraints_mutex);
9011
9012 return ret;
9013}
9014#else
9015static int sched_rt_global_constraints(void)
9016{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009017 unsigned long flags;
9018 int i;
9019
9020 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
9021 for_each_possible_cpu(i) {
9022 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
9023
9024 spin_lock(&rt_rq->rt_runtime_lock);
9025 rt_rq->rt_runtime = global_rt_runtime();
9026 spin_unlock(&rt_rq->rt_runtime_lock);
9027 }
9028 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
9029
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009030 return 0;
9031}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009032#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009033
9034int sched_rt_handler(struct ctl_table *table, int write,
9035 struct file *filp, void __user *buffer, size_t *lenp,
9036 loff_t *ppos)
9037{
9038 int ret;
9039 int old_period, old_runtime;
9040 static DEFINE_MUTEX(mutex);
9041
9042 mutex_lock(&mutex);
9043 old_period = sysctl_sched_rt_period;
9044 old_runtime = sysctl_sched_rt_runtime;
9045
9046 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
9047
9048 if (!ret && write) {
9049 ret = sched_rt_global_constraints();
9050 if (ret) {
9051 sysctl_sched_rt_period = old_period;
9052 sysctl_sched_rt_runtime = old_runtime;
9053 } else {
9054 def_rt_bandwidth.rt_runtime = global_rt_runtime();
9055 def_rt_bandwidth.rt_period =
9056 ns_to_ktime(global_rt_period());
9057 }
9058 }
9059 mutex_unlock(&mutex);
9060
9061 return ret;
9062}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009063
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009064#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009065
9066/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009067static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009068{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009069 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
9070 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009071}
9072
9073static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02009074cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009075{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009076 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009077
Paul Menage2b01dfe2007-10-24 18:23:50 +02009078 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009079 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009080 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009081 return &init_task_group.css;
9082 }
9083
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009084 parent = cgroup_tg(cgrp->parent);
9085 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009086 if (IS_ERR(tg))
9087 return ERR_PTR(-ENOMEM);
9088
9089 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009090 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009091
9092 return &tg->css;
9093}
9094
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009095static void
9096cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009097{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009098 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009099
9100 sched_destroy_group(tg);
9101}
9102
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009103static int
9104cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9105 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009106{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009107#ifdef CONFIG_RT_GROUP_SCHED
9108 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009109 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009110 return -EINVAL;
9111#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009112 /* We don't support RT-tasks being in separate groups */
9113 if (tsk->sched_class != &fair_sched_class)
9114 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009115#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009116
9117 return 0;
9118}
9119
9120static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009121cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009122 struct cgroup *old_cont, struct task_struct *tsk)
9123{
9124 sched_move_task(tsk);
9125}
9126
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009127#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009128static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009129 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009130{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009131 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009132}
9133
Paul Menagef4c753b2008-04-29 00:59:56 -07009134static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009135{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009136 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009137
9138 return (u64) tg->shares;
9139}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009140#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009141
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009142#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009143static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009144 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009145{
Paul Menage06ecb272008-04-29 01:00:06 -07009146 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009147}
9148
Paul Menage06ecb272008-04-29 01:00:06 -07009149static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009150{
Paul Menage06ecb272008-04-29 01:00:06 -07009151 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009152}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009153
9154static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9155 u64 rt_period_us)
9156{
9157 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9158}
9159
9160static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9161{
9162 return sched_group_rt_period(cgroup_tg(cgrp));
9163}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009164#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009165
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009166static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009167#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009168 {
9169 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009170 .read_u64 = cpu_shares_read_u64,
9171 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009172 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009173#endif
9174#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009175 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009176 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009177 .read_s64 = cpu_rt_runtime_read,
9178 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009179 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009180 {
9181 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009182 .read_u64 = cpu_rt_period_read_uint,
9183 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009184 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009185#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009186};
9187
9188static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9189{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009190 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009191}
9192
9193struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009194 .name = "cpu",
9195 .create = cpu_cgroup_create,
9196 .destroy = cpu_cgroup_destroy,
9197 .can_attach = cpu_cgroup_can_attach,
9198 .attach = cpu_cgroup_attach,
9199 .populate = cpu_cgroup_populate,
9200 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009201 .early_init = 1,
9202};
9203
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009204#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009205
9206#ifdef CONFIG_CGROUP_CPUACCT
9207
9208/*
9209 * CPU accounting code for task groups.
9210 *
9211 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9212 * (balbir@in.ibm.com).
9213 */
9214
9215/* track cpu usage of a group of tasks */
9216struct cpuacct {
9217 struct cgroup_subsys_state css;
9218 /* cpuusage holds pointer to a u64-type object on every cpu */
9219 u64 *cpuusage;
9220};
9221
9222struct cgroup_subsys cpuacct_subsys;
9223
9224/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309225static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009226{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309227 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009228 struct cpuacct, css);
9229}
9230
9231/* return cpu accounting group to which this task belongs */
9232static inline struct cpuacct *task_ca(struct task_struct *tsk)
9233{
9234 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9235 struct cpuacct, css);
9236}
9237
9238/* create a new cpu accounting group */
9239static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309240 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009241{
9242 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9243
9244 if (!ca)
9245 return ERR_PTR(-ENOMEM);
9246
9247 ca->cpuusage = alloc_percpu(u64);
9248 if (!ca->cpuusage) {
9249 kfree(ca);
9250 return ERR_PTR(-ENOMEM);
9251 }
9252
9253 return &ca->css;
9254}
9255
9256/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009257static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309258cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009259{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309260 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009261
9262 free_percpu(ca->cpuusage);
9263 kfree(ca);
9264}
9265
9266/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309267static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009268{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309269 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009270 u64 totalcpuusage = 0;
9271 int i;
9272
9273 for_each_possible_cpu(i) {
9274 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9275
9276 /*
9277 * Take rq->lock to make 64-bit addition safe on 32-bit
9278 * platforms.
9279 */
9280 spin_lock_irq(&cpu_rq(i)->lock);
9281 totalcpuusage += *cpuusage;
9282 spin_unlock_irq(&cpu_rq(i)->lock);
9283 }
9284
9285 return totalcpuusage;
9286}
9287
Dhaval Giani0297b802008-02-29 10:02:44 +05309288static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9289 u64 reset)
9290{
9291 struct cpuacct *ca = cgroup_ca(cgrp);
9292 int err = 0;
9293 int i;
9294
9295 if (reset) {
9296 err = -EINVAL;
9297 goto out;
9298 }
9299
9300 for_each_possible_cpu(i) {
9301 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9302
9303 spin_lock_irq(&cpu_rq(i)->lock);
9304 *cpuusage = 0;
9305 spin_unlock_irq(&cpu_rq(i)->lock);
9306 }
9307out:
9308 return err;
9309}
9310
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009311static struct cftype files[] = {
9312 {
9313 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009314 .read_u64 = cpuusage_read,
9315 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009316 },
9317};
9318
Dhaval Giani32cd7562008-02-29 10:02:43 +05309319static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009320{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309321 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009322}
9323
9324/*
9325 * charge this task's execution time to its accounting group.
9326 *
9327 * called with rq->lock held.
9328 */
9329static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9330{
9331 struct cpuacct *ca;
9332
9333 if (!cpuacct_subsys.active)
9334 return;
9335
9336 ca = task_ca(tsk);
9337 if (ca) {
9338 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9339
9340 *cpuusage += cputime;
9341 }
9342}
9343
9344struct cgroup_subsys cpuacct_subsys = {
9345 .name = "cpuacct",
9346 .create = cpuacct_create,
9347 .destroy = cpuacct_destroy,
9348 .populate = cpuacct_populate,
9349 .subsys_id = cpuacct_subsys_id,
9350};
9351#endif /* CONFIG_CGROUP_CPUACCT */