blob: ad95cca4e42e19520a01f20fb2c3f8d55c78d0c6 [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>
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020073#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Eric Dumazet5517d862007-05-08 00:32:57 -070075#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020076#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * Convert user-nice values [ -20 ... 0 ... 19 ]
80 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
81 * and back.
82 */
83#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
84#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
85#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
86
87/*
88 * 'User priority' is the nice value converted to something we
89 * can work with better when scaling various scheduler parameters,
90 * it's a [ 0 ... 39 ] range.
91 */
92#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
93#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
94#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
95
96/*
Ingo Molnard7876a02008-01-25 21:08:19 +010097 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +010099#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200101#define NICE_0_LOAD SCHED_LOAD_SCALE
102#define NICE_0_SHIFT SCHED_LOAD_SHIFT
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * These are the 'tuning knobs' of the scheduler:
106 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200107 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * Timeslices get refilled after they expire.
109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700111
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200112/*
113 * single value that denotes runtime == period, ie unlimited time.
114 */
115#define RUNTIME_INF ((u64)~0ULL)
116
Eric Dumazet5517d862007-05-08 00:32:57 -0700117#ifdef CONFIG_SMP
118/*
119 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
120 * Since cpu_power is a 'constant', we can use a reciprocal divide.
121 */
122static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
123{
124 return reciprocal_divide(load, sg->reciprocal_cpu_power);
125}
126
127/*
128 * Each time a sched group cpu_power is changed,
129 * we must compute its reciprocal value
130 */
131static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
132{
133 sg->__cpu_power += val;
134 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
135}
136#endif
137
Ingo Molnare05606d2007-07-09 18:51:59 +0200138static inline int rt_policy(int policy)
139{
140 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
141 return 1;
142 return 0;
143}
144
145static inline int task_has_rt_policy(struct task_struct *p)
146{
147 return rt_policy(p->policy);
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200151 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200153struct rt_prio_array {
154 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
155 struct list_head queue[MAX_RT_PRIO];
156};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200158struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100159 /* nests inside the rq lock: */
160 spinlock_t rt_runtime_lock;
161 ktime_t rt_period;
162 u64 rt_runtime;
163 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200164};
165
166static struct rt_bandwidth def_rt_bandwidth;
167
168static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
169
170static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
171{
172 struct rt_bandwidth *rt_b =
173 container_of(timer, struct rt_bandwidth, rt_period_timer);
174 ktime_t now;
175 int overrun;
176 int idle = 0;
177
178 for (;;) {
179 now = hrtimer_cb_get_time(timer);
180 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
181
182 if (!overrun)
183 break;
184
185 idle = do_sched_rt_period_timer(rt_b, overrun);
186 }
187
188 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
189}
190
191static
192void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
193{
194 rt_b->rt_period = ns_to_ktime(period);
195 rt_b->rt_runtime = runtime;
196
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200197 spin_lock_init(&rt_b->rt_runtime_lock);
198
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200199 hrtimer_init(&rt_b->rt_period_timer,
200 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
201 rt_b->rt_period_timer.function = sched_rt_period_timer;
202 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
203}
204
205static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
206{
207 ktime_t now;
208
209 if (rt_b->rt_runtime == RUNTIME_INF)
210 return;
211
212 if (hrtimer_active(&rt_b->rt_period_timer))
213 return;
214
215 spin_lock(&rt_b->rt_runtime_lock);
216 for (;;) {
217 if (hrtimer_active(&rt_b->rt_period_timer))
218 break;
219
220 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
221 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
222 hrtimer_start(&rt_b->rt_period_timer,
223 rt_b->rt_period_timer.expires,
224 HRTIMER_MODE_ABS);
225 }
226 spin_unlock(&rt_b->rt_runtime_lock);
227}
228
229#ifdef CONFIG_RT_GROUP_SCHED
230static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
231{
232 hrtimer_cancel(&rt_b->rt_period_timer);
233}
234#endif
235
Heiko Carstens712555e2008-04-28 11:33:07 +0200236/*
237 * sched_domains_mutex serializes calls to arch_init_sched_domains,
238 * detach_destroy_domains and partition_sched_domains.
239 */
240static DEFINE_MUTEX(sched_domains_mutex);
241
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100242#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200243
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700244#include <linux/cgroup.h>
245
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200246struct cfs_rq;
247
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100248static LIST_HEAD(task_groups);
249
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200250/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200251struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100252#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700253 struct cgroup_subsys_state css;
254#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100255
256#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200257 /* schedulable entities of this group on each cpu */
258 struct sched_entity **se;
259 /* runqueue "owned" by this group on each cpu */
260 struct cfs_rq **cfs_rq;
261 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100262#endif
263
264#ifdef CONFIG_RT_GROUP_SCHED
265 struct sched_rt_entity **rt_se;
266 struct rt_rq **rt_rq;
267
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200268 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100269#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100270
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100271 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100272 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200273
274 struct task_group *parent;
275 struct list_head siblings;
276 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200277};
278
Dhaval Giani354d60c2008-04-19 19:44:59 +0200279#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200280
281/*
282 * Root task group.
283 * Every UID task group (including init_task_group aka UID-0) will
284 * be a child to this group.
285 */
286struct task_group root_task_group;
287
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100288#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200289/* Default task group's sched entity on each cpu */
290static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
291/* Default task group's cfs_rq on each cpu */
292static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100293#endif
294
295#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100296static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
297static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100298#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200299#else
300#define root_task_group init_task_group
Dhaval Giani354d60c2008-04-19 19:44:59 +0200301#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100302
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100303/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100304 * a task group's cpu shares.
305 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100306static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100307
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100308#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100309#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100310# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200311#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100312# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200313#endif
314
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800315/*
316 * A weight of 0, 1 or ULONG_MAX can cause arithmetics problems.
317 * (The default weight is 1024 - so there's no practical
318 * limitation from this.)
319 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200320#define MIN_SHARES 2
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800321#define MAX_SHARES (ULONG_MAX - 1)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200322
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100323static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100324#endif
325
326/* Default task group.
327 * Every task in system belong to this group at bootup.
328 */
Mike Travis434d53b2008-04-04 18:11:04 -0700329struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200330
331/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200332static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200333{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200334 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200335
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100336#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200337 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100338#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700339 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
340 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200341#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100342 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200343#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200344 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200345}
346
347/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100348static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200349{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100350#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100351 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
352 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100353#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100354
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100355#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100356 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
357 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100358#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200359}
360
361#else
362
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100363static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200364
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100365#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200366
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200367/* CFS-related fields in a runqueue */
368struct cfs_rq {
369 struct load_weight load;
370 unsigned long nr_running;
371
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200372 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200373 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200374
375 struct rb_root tasks_timeline;
376 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200377
378 struct list_head tasks;
379 struct list_head *balance_iterator;
380
381 /*
382 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200383 * It is set to NULL otherwise (i.e when none are currently running).
384 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100385 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200386
387 unsigned long nr_spread_over;
388
Ingo Molnar62160e3f2007-10-15 17:00:03 +0200389#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200390 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
391
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100392 /*
393 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200394 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
395 * (like users, containers etc.)
396 *
397 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
398 * list is used during load balance.
399 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100400 struct list_head leaf_cfs_rq_list;
401 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200402
403#ifdef CONFIG_SMP
404 unsigned long task_weight;
405 unsigned long shares;
406 /*
407 * We need space to build a sched_domain wide view of the full task
408 * group tree, in order to avoid depending on dynamic memory allocation
409 * during the load balancing we place this in the per cpu task group
410 * hierarchy. This limits the load balancing to one instance per cpu,
411 * but more should not be needed anyway.
412 */
413 struct aggregate_struct {
414 /*
415 * load = weight(cpus) * f(tg)
416 *
417 * Where f(tg) is the recursive weight fraction assigned to
418 * this group.
419 */
420 unsigned long load;
421
422 /*
423 * part of the group weight distributed to this span.
424 */
425 unsigned long shares;
426
427 /*
428 * The sum of all runqueue weights within this span.
429 */
430 unsigned long rq_weight;
431
432 /*
433 * Weight contributed by tasks; this is the part we can
434 * influence by moving tasks around.
435 */
436 unsigned long task_weight;
437 } aggregate;
438#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200439#endif
440};
441
442/* Real-Time classes' related field in a runqueue: */
443struct rt_rq {
444 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100445 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100446#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100447 int highest_prio; /* highest queued rt task prio */
448#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100449#ifdef CONFIG_SMP
Gregory Haskins73fe6aae2008-01-25 21:08:07 +0100450 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc12008-01-25 21:08:12 +0100451 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100452#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100453 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100454 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200455 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100456 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200457 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100458
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100459#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100460 unsigned long rt_nr_boosted;
461
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100462 struct rq *rq;
463 struct list_head leaf_rt_rq_list;
464 struct task_group *tg;
465 struct sched_rt_entity *rt_se;
466#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200467};
468
Gregory Haskins57d885f2008-01-25 21:08:18 +0100469#ifdef CONFIG_SMP
470
471/*
472 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100473 * variables. Each exclusive cpuset essentially defines an island domain by
474 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100475 * exclusive cpuset is created, we also create and attach a new root-domain
476 * object.
477 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100478 */
479struct root_domain {
480 atomic_t refcount;
481 cpumask_t span;
482 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100483
Ingo Molnar0eab9142008-01-25 21:08:19 +0100484 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100485 * The "RT overload" flag: it gets set if a CPU has more than
486 * one runnable RT task.
487 */
488 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100489 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100490};
491
Gregory Haskinsdc938522008-01-25 21:08:26 +0100492/*
493 * By default the system creates a single root-domain with all cpus as
494 * members (mimicking the global state we have today).
495 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100496static struct root_domain def_root_domain;
497
498#endif
499
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200500/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * This is the main, per-CPU runqueue data structure.
502 *
503 * Locking rule: those places that want to lock multiple runqueues
504 * (such as the load balancing or the thread migration code), lock
505 * acquire operations must be ordered by ascending &runqueue.
506 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700507struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200508 /* runqueue lock: */
509 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /*
512 * nr_running and cpu_load should be in the same cacheline because
513 * remote CPUs use both these fields when doing load calculation.
514 */
515 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200516 #define CPU_LOAD_IDX_MAX 5
517 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700518 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700519#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200520 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700521 unsigned char in_nohz_recently;
522#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200523 /* capture load from *all* tasks on this cpu: */
524 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200525 unsigned long nr_load_updates;
526 u64 nr_switches;
527
528 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100529 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100530
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200531#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200532 /* list of leaf cfs_rq on this cpu: */
533 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100534#endif
535#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100536 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /*
540 * This is part of a global counter where only the total sum
541 * over all CPUs matters. A task can increase this counter on
542 * one CPU and if it got migrated afterwards it may decrease
543 * it on another CPU. Always updated under the runqueue lock:
544 */
545 unsigned long nr_uninterruptible;
546
Ingo Molnar36c8b582006-07-03 00:25:41 -0700547 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800548 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200550
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200551 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 atomic_t nr_iowait;
554
555#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100556 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct sched_domain *sd;
558
559 /* For active balancing */
560 int active_balance;
561 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200562 /* cpu of this runqueue: */
563 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Ingo Molnar36c8b582006-07-03 00:25:41 -0700565 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct list_head migration_queue;
567#endif
568
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100569#ifdef CONFIG_SCHED_HRTICK
570 unsigned long hrtick_flags;
571 ktime_t hrtick_expire;
572 struct hrtimer hrtick_timer;
573#endif
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575#ifdef CONFIG_SCHEDSTATS
576 /* latency stats */
577 struct sched_info rq_sched_info;
578
579 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200580 unsigned int yld_exp_empty;
581 unsigned int yld_act_empty;
582 unsigned int yld_both_empty;
583 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200586 unsigned int sched_switch;
587 unsigned int sched_count;
588 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200591 unsigned int ttwu_count;
592 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200593
594 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200595 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700597 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598};
599
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700600static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Ingo Molnardd41f592007-07-09 18:51:59 +0200602static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
603{
604 rq->curr->sched_class->check_preempt_curr(rq, p);
605}
606
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700607static inline int cpu_of(struct rq *rq)
608{
609#ifdef CONFIG_SMP
610 return rq->cpu;
611#else
612 return 0;
613#endif
614}
615
Ingo Molnar20d315d2007-07-09 18:51:58 +0200616/*
Nick Piggin674311d2005-06-25 14:57:27 -0700617 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700618 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700619 *
620 * The domain tree of any CPU may only be accessed from within
621 * preempt-disabled sections.
622 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700623#define for_each_domain(cpu, __sd) \
624 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
627#define this_rq() (&__get_cpu_var(runqueues))
628#define task_rq(p) cpu_rq(task_cpu(p))
629#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
630
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200631static inline void update_rq_clock(struct rq *rq)
632{
633 rq->clock = sched_clock_cpu(cpu_of(rq));
634}
635
Ingo Molnare436d802007-07-19 21:28:35 +0200636/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200637 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
638 */
639#ifdef CONFIG_SCHED_DEBUG
640# define const_debug __read_mostly
641#else
642# define const_debug static const
643#endif
644
Ingo Molnar017730c2008-05-12 21:20:52 +0200645/**
646 * runqueue_is_locked
647 *
648 * Returns true if the current cpu runqueue is locked.
649 * This interface allows printk to be called with the runqueue lock
650 * held and know whether or not it is OK to wake up the klogd.
651 */
652int runqueue_is_locked(void)
653{
654 int cpu = get_cpu();
655 struct rq *rq = cpu_rq(cpu);
656 int ret;
657
658 ret = spin_is_locked(&rq->lock);
659 put_cpu();
660 return ret;
661}
662
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200663/*
664 * Debugging: various feature bits
665 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200666
667#define SCHED_FEAT(name, enabled) \
668 __SCHED_FEAT_##name ,
669
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200670enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200671#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200672};
673
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200674#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200675
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200676#define SCHED_FEAT(name, enabled) \
677 (1UL << __SCHED_FEAT_##name) * enabled |
678
679const_debug unsigned int sysctl_sched_features =
680#include "sched_features.h"
681 0;
682
683#undef SCHED_FEAT
684
685#ifdef CONFIG_SCHED_DEBUG
686#define SCHED_FEAT(name, enabled) \
687 #name ,
688
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700689static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200690#include "sched_features.h"
691 NULL
692};
693
694#undef SCHED_FEAT
695
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700696static int sched_feat_open(struct inode *inode, struct file *filp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200697{
698 filp->private_data = inode->i_private;
699 return 0;
700}
701
702static ssize_t
703sched_feat_read(struct file *filp, char __user *ubuf,
704 size_t cnt, loff_t *ppos)
705{
706 char *buf;
707 int r = 0;
708 int len = 0;
709 int i;
710
711 for (i = 0; sched_feat_names[i]; i++) {
712 len += strlen(sched_feat_names[i]);
713 len += 4;
714 }
715
716 buf = kmalloc(len + 2, GFP_KERNEL);
717 if (!buf)
718 return -ENOMEM;
719
720 for (i = 0; sched_feat_names[i]; i++) {
721 if (sysctl_sched_features & (1UL << i))
722 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
723 else
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200724 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200725 }
726
727 r += sprintf(buf + r, "\n");
728 WARN_ON(r >= len + 2);
729
730 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
731
732 kfree(buf);
733
734 return r;
735}
736
737static ssize_t
738sched_feat_write(struct file *filp, const char __user *ubuf,
739 size_t cnt, loff_t *ppos)
740{
741 char buf[64];
742 char *cmp = buf;
743 int neg = 0;
744 int i;
745
746 if (cnt > 63)
747 cnt = 63;
748
749 if (copy_from_user(&buf, ubuf, cnt))
750 return -EFAULT;
751
752 buf[cnt] = 0;
753
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200754 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200755 neg = 1;
756 cmp += 3;
757 }
758
759 for (i = 0; sched_feat_names[i]; i++) {
760 int len = strlen(sched_feat_names[i]);
761
762 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
763 if (neg)
764 sysctl_sched_features &= ~(1UL << i);
765 else
766 sysctl_sched_features |= (1UL << i);
767 break;
768 }
769 }
770
771 if (!sched_feat_names[i])
772 return -EINVAL;
773
774 filp->f_pos += cnt;
775
776 return cnt;
777}
778
779static struct file_operations sched_feat_fops = {
780 .open = sched_feat_open,
781 .read = sched_feat_read,
782 .write = sched_feat_write,
783};
784
785static __init int sched_init_debug(void)
786{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200787 debugfs_create_file("sched_features", 0644, NULL, NULL,
788 &sched_feat_fops);
789
790 return 0;
791}
792late_initcall(sched_init_debug);
793
794#endif
795
796#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200797
798/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100799 * Number of tasks to iterate in a single balance run.
800 * Limited because this is done with IRQs disabled.
801 */
802const_debug unsigned int sysctl_sched_nr_migrate = 32;
803
804/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100805 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100806 * default: 1s
807 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100808unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100809
Ingo Molnar6892b752008-02-13 14:02:36 +0100810static __read_mostly int scheduler_running;
811
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100812/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100813 * part of the period that we allow rt tasks to run in us.
814 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100815 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100816int sysctl_sched_rt_runtime = 950000;
817
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200818static inline u64 global_rt_period(void)
819{
820 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
821}
822
823static inline u64 global_rt_runtime(void)
824{
825 if (sysctl_sched_rt_period < 0)
826 return RUNTIME_INF;
827
828 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
829}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100830
Ingo Molnar690229a2008-04-23 09:31:35 +0200831unsigned long long time_sync_thresh = 100000;
Ingo Molnar27ec4402008-02-28 21:00:21 +0100832
833static DEFINE_PER_CPU(unsigned long long, time_offset);
834static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
835
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100836/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100837 * Global lock which we take every now and then to synchronize
838 * the CPUs time. This method is not warp-safe, but it's good
839 * enough to synchronize slowly diverging time sources and thus
840 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200841 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100842static DEFINE_SPINLOCK(time_sync_lock);
843static unsigned long long prev_global_time;
844
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200845static unsigned long long __sync_cpu_clock(unsigned long long time, int cpu)
Ingo Molnar27ec4402008-02-28 21:00:21 +0100846{
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200847 /*
848 * We want this inlined, to not get tracer function calls
849 * in this critical section:
850 */
851 spin_acquire(&time_sync_lock.dep_map, 0, 0, _THIS_IP_);
852 __raw_spin_lock(&time_sync_lock.raw_lock);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100853
854 if (time < prev_global_time) {
855 per_cpu(time_offset, cpu) += prev_global_time - time;
856 time = prev_global_time;
857 } else {
858 prev_global_time = time;
859 }
860
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200861 __raw_spin_unlock(&time_sync_lock.raw_lock);
862 spin_release(&time_sync_lock.dep_map, 1, _THIS_IP_);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100863
864 return time;
865}
866
867static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200868{
Ingo Molnare436d802007-07-19 21:28:35 +0200869 unsigned long long now;
Ingo Molnare436d802007-07-19 21:28:35 +0200870
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100871 /*
872 * Only call sched_clock() if the scheduler has already been
873 * initialized (some code might call cpu_clock() very early):
874 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100875 if (unlikely(!scheduler_running))
876 return 0;
877
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200878 now = sched_clock_cpu(cpu);
Ingo Molnare436d802007-07-19 21:28:35 +0200879
880 return now;
881}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100882
883/*
884 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
885 * clock constructed from sched_clock():
886 */
887unsigned long long cpu_clock(int cpu)
888{
889 unsigned long long prev_cpu_time, time, delta_time;
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200890 unsigned long flags;
Ingo Molnar27ec4402008-02-28 21:00:21 +0100891
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200892 local_irq_save(flags);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100893 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
894 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
895 delta_time = time-prev_cpu_time;
896
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200897 if (unlikely(delta_time > time_sync_thresh)) {
Ingo Molnar27ec4402008-02-28 21:00:21 +0100898 time = __sync_cpu_clock(time, cpu);
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200899 per_cpu(prev_cpu_time, cpu) = time;
900 }
901 local_irq_restore(flags);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100902
903 return time;
904}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200905EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700908# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700910#ifndef finish_arch_switch
911# define finish_arch_switch(prev) do { } while (0)
912#endif
913
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100914static inline int task_current(struct rq *rq, struct task_struct *p)
915{
916 return rq->curr == p;
917}
918
Nick Piggin4866cde2005-06-25 14:57:23 -0700919#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700920static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700921{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100922 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700923}
924
Ingo Molnar70b97a72006-07-03 00:25:42 -0700925static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700926{
927}
928
Ingo Molnar70b97a72006-07-03 00:25:42 -0700929static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700930{
Ingo Molnarda04c032005-09-13 11:17:59 +0200931#ifdef CONFIG_DEBUG_SPINLOCK
932 /* this is a valid case when another task releases the spinlock */
933 rq->lock.owner = current;
934#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700935 /*
936 * If we are tracking spinlock dependencies then we have to
937 * fix up the runqueue lock - which gets 'carried over' from
938 * prev into current:
939 */
940 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
941
Nick Piggin4866cde2005-06-25 14:57:23 -0700942 spin_unlock_irq(&rq->lock);
943}
944
945#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700946static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700947{
948#ifdef CONFIG_SMP
949 return p->oncpu;
950#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100951 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700952#endif
953}
954
Ingo Molnar70b97a72006-07-03 00:25:42 -0700955static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700956{
957#ifdef CONFIG_SMP
958 /*
959 * We can optimise this out completely for !SMP, because the
960 * SMP rebalancing from interrupt is the only thing that cares
961 * here.
962 */
963 next->oncpu = 1;
964#endif
965#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
966 spin_unlock_irq(&rq->lock);
967#else
968 spin_unlock(&rq->lock);
969#endif
970}
971
Ingo Molnar70b97a72006-07-03 00:25:42 -0700972static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700973{
974#ifdef CONFIG_SMP
975 /*
976 * After ->oncpu is cleared, the task can be moved to a different CPU.
977 * We must ensure this doesn't happen until the switch is completely
978 * finished.
979 */
980 smp_wmb();
981 prev->oncpu = 0;
982#endif
983#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
984 local_irq_enable();
985#endif
986}
987#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700990 * __task_rq_lock - lock the runqueue a given task resides on.
991 * Must be called interrupts disabled.
992 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700993static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700994 __acquires(rq->lock)
995{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200996 for (;;) {
997 struct rq *rq = task_rq(p);
998 spin_lock(&rq->lock);
999 if (likely(rq == task_rq(p)))
1000 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -07001001 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001002 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07001003}
1004
1005/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001007 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 * explicitly disabling preemption.
1009 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001010static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 __acquires(rq->lock)
1012{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001013 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Andi Kleen3a5c3592007-10-15 17:00:14 +02001015 for (;;) {
1016 local_irq_save(*flags);
1017 rq = task_rq(p);
1018 spin_lock(&rq->lock);
1019 if (likely(rq == task_rq(p)))
1020 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Alexey Dobriyana9957442007-10-15 17:00:13 +02001025static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001026 __releases(rq->lock)
1027{
1028 spin_unlock(&rq->lock);
1029}
1030
Ingo Molnar70b97a72006-07-03 00:25:42 -07001031static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 __releases(rq->lock)
1033{
1034 spin_unlock_irqrestore(&rq->lock, *flags);
1035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001038 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001040static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 __acquires(rq->lock)
1042{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001043 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 local_irq_disable();
1046 rq = this_rq();
1047 spin_lock(&rq->lock);
1048
1049 return rq;
1050}
1051
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001052static void __resched_task(struct task_struct *p, int tif_bit);
1053
1054static inline void resched_task(struct task_struct *p)
1055{
1056 __resched_task(p, TIF_NEED_RESCHED);
1057}
1058
1059#ifdef CONFIG_SCHED_HRTICK
1060/*
1061 * Use HR-timers to deliver accurate preemption points.
1062 *
1063 * Its all a bit involved since we cannot program an hrt while holding the
1064 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1065 * reschedule event.
1066 *
1067 * When we get rescheduled we reprogram the hrtick_timer outside of the
1068 * rq->lock.
1069 */
1070static inline void resched_hrt(struct task_struct *p)
1071{
1072 __resched_task(p, TIF_HRTICK_RESCHED);
1073}
1074
1075static inline void resched_rq(struct rq *rq)
1076{
1077 unsigned long flags;
1078
1079 spin_lock_irqsave(&rq->lock, flags);
1080 resched_task(rq->curr);
1081 spin_unlock_irqrestore(&rq->lock, flags);
1082}
1083
1084enum {
1085 HRTICK_SET, /* re-programm hrtick_timer */
1086 HRTICK_RESET, /* not a new slice */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001087 HRTICK_BLOCK, /* stop hrtick operations */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001088};
1089
1090/*
1091 * Use hrtick when:
1092 * - enabled by features
1093 * - hrtimer is actually high res
1094 */
1095static inline int hrtick_enabled(struct rq *rq)
1096{
1097 if (!sched_feat(HRTICK))
1098 return 0;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001099 if (unlikely(test_bit(HRTICK_BLOCK, &rq->hrtick_flags)))
1100 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001101 return hrtimer_is_hres_active(&rq->hrtick_timer);
1102}
1103
1104/*
1105 * Called to set the hrtick timer state.
1106 *
1107 * called with rq->lock held and irqs disabled
1108 */
1109static void hrtick_start(struct rq *rq, u64 delay, int reset)
1110{
1111 assert_spin_locked(&rq->lock);
1112
1113 /*
1114 * preempt at: now + delay
1115 */
1116 rq->hrtick_expire =
1117 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1118 /*
1119 * indicate we need to program the timer
1120 */
1121 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1122 if (reset)
1123 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1124
1125 /*
1126 * New slices are called from the schedule path and don't need a
1127 * forced reschedule.
1128 */
1129 if (reset)
1130 resched_hrt(rq->curr);
1131}
1132
1133static void hrtick_clear(struct rq *rq)
1134{
1135 if (hrtimer_active(&rq->hrtick_timer))
1136 hrtimer_cancel(&rq->hrtick_timer);
1137}
1138
1139/*
1140 * Update the timer from the possible pending state.
1141 */
1142static void hrtick_set(struct rq *rq)
1143{
1144 ktime_t time;
1145 int set, reset;
1146 unsigned long flags;
1147
1148 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1149
1150 spin_lock_irqsave(&rq->lock, flags);
1151 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1152 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1153 time = rq->hrtick_expire;
1154 clear_thread_flag(TIF_HRTICK_RESCHED);
1155 spin_unlock_irqrestore(&rq->lock, flags);
1156
1157 if (set) {
1158 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1159 if (reset && !hrtimer_active(&rq->hrtick_timer))
1160 resched_rq(rq);
1161 } else
1162 hrtick_clear(rq);
1163}
1164
1165/*
1166 * High-resolution timer tick.
1167 * Runs from hardirq context with interrupts disabled.
1168 */
1169static enum hrtimer_restart hrtick(struct hrtimer *timer)
1170{
1171 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1172
1173 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1174
1175 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001176 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001177 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1178 spin_unlock(&rq->lock);
1179
1180 return HRTIMER_NORESTART;
1181}
1182
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001183static void hotplug_hrtick_disable(int cpu)
1184{
1185 struct rq *rq = cpu_rq(cpu);
1186 unsigned long flags;
1187
1188 spin_lock_irqsave(&rq->lock, flags);
1189 rq->hrtick_flags = 0;
1190 __set_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1191 spin_unlock_irqrestore(&rq->lock, flags);
1192
1193 hrtick_clear(rq);
1194}
1195
1196static void hotplug_hrtick_enable(int cpu)
1197{
1198 struct rq *rq = cpu_rq(cpu);
1199 unsigned long flags;
1200
1201 spin_lock_irqsave(&rq->lock, flags);
1202 __clear_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1203 spin_unlock_irqrestore(&rq->lock, flags);
1204}
1205
1206static int
1207hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1208{
1209 int cpu = (int)(long)hcpu;
1210
1211 switch (action) {
1212 case CPU_UP_CANCELED:
1213 case CPU_UP_CANCELED_FROZEN:
1214 case CPU_DOWN_PREPARE:
1215 case CPU_DOWN_PREPARE_FROZEN:
1216 case CPU_DEAD:
1217 case CPU_DEAD_FROZEN:
1218 hotplug_hrtick_disable(cpu);
1219 return NOTIFY_OK;
1220
1221 case CPU_UP_PREPARE:
1222 case CPU_UP_PREPARE_FROZEN:
1223 case CPU_DOWN_FAILED:
1224 case CPU_DOWN_FAILED_FROZEN:
1225 case CPU_ONLINE:
1226 case CPU_ONLINE_FROZEN:
1227 hotplug_hrtick_enable(cpu);
1228 return NOTIFY_OK;
1229 }
1230
1231 return NOTIFY_DONE;
1232}
1233
1234static void init_hrtick(void)
1235{
1236 hotcpu_notifier(hotplug_hrtick, 0);
1237}
1238
1239static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001240{
1241 rq->hrtick_flags = 0;
1242 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1243 rq->hrtick_timer.function = hrtick;
1244 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1245}
1246
1247void hrtick_resched(void)
1248{
1249 struct rq *rq;
1250 unsigned long flags;
1251
1252 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1253 return;
1254
1255 local_irq_save(flags);
1256 rq = cpu_rq(smp_processor_id());
1257 hrtick_set(rq);
1258 local_irq_restore(flags);
1259}
1260#else
1261static inline void hrtick_clear(struct rq *rq)
1262{
1263}
1264
1265static inline void hrtick_set(struct rq *rq)
1266{
1267}
1268
1269static inline void init_rq_hrtick(struct rq *rq)
1270{
1271}
1272
1273void hrtick_resched(void)
1274{
1275}
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001276
1277static inline void init_hrtick(void)
1278{
1279}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001280#endif
1281
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001282/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001283 * resched_task - mark a task 'to be rescheduled now'.
1284 *
1285 * On UP this means the setting of the need_resched flag, on SMP it
1286 * might also involve a cross-CPU call to trigger the scheduler on
1287 * the target CPU.
1288 */
1289#ifdef CONFIG_SMP
1290
1291#ifndef tsk_is_polling
1292#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1293#endif
1294
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001295static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001296{
1297 int cpu;
1298
1299 assert_spin_locked(&task_rq(p)->lock);
1300
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001301 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001302 return;
1303
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001304 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001305
1306 cpu = task_cpu(p);
1307 if (cpu == smp_processor_id())
1308 return;
1309
1310 /* NEED_RESCHED must be visible before we test polling */
1311 smp_mb();
1312 if (!tsk_is_polling(p))
1313 smp_send_reschedule(cpu);
1314}
1315
1316static void resched_cpu(int cpu)
1317{
1318 struct rq *rq = cpu_rq(cpu);
1319 unsigned long flags;
1320
1321 if (!spin_trylock_irqsave(&rq->lock, flags))
1322 return;
1323 resched_task(cpu_curr(cpu));
1324 spin_unlock_irqrestore(&rq->lock, flags);
1325}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001326
1327#ifdef CONFIG_NO_HZ
1328/*
1329 * When add_timer_on() enqueues a timer into the timer wheel of an
1330 * idle CPU then this timer might expire before the next timer event
1331 * which is scheduled to wake up that CPU. In case of a completely
1332 * idle system the next event might even be infinite time into the
1333 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1334 * leaves the inner idle loop so the newly added timer is taken into
1335 * account when the CPU goes back to idle and evaluates the timer
1336 * wheel for the next timer event.
1337 */
1338void wake_up_idle_cpu(int cpu)
1339{
1340 struct rq *rq = cpu_rq(cpu);
1341
1342 if (cpu == smp_processor_id())
1343 return;
1344
1345 /*
1346 * This is safe, as this function is called with the timer
1347 * wheel base lock of (cpu) held. When the CPU is on the way
1348 * to idle and has not yet set rq->curr to idle then it will
1349 * be serialized on the timer wheel base lock and take the new
1350 * timer into account automatically.
1351 */
1352 if (rq->curr != rq->idle)
1353 return;
1354
1355 /*
1356 * We can set TIF_RESCHED on the idle task of the other CPU
1357 * lockless. The worst case is that the other CPU runs the
1358 * idle task through an additional NOOP schedule()
1359 */
1360 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1361
1362 /* NEED_RESCHED must be visible before we test polling */
1363 smp_mb();
1364 if (!tsk_is_polling(rq->idle))
1365 smp_send_reschedule(cpu);
1366}
1367#endif
1368
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001369#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001370static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001371{
1372 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001373 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001374}
1375#endif
1376
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001377#if BITS_PER_LONG == 32
1378# define WMULT_CONST (~0UL)
1379#else
1380# define WMULT_CONST (1UL << 32)
1381#endif
1382
1383#define WMULT_SHIFT 32
1384
Ingo Molnar194081e2007-08-09 11:16:51 +02001385/*
1386 * Shift right and round:
1387 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001388#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001389
Peter Zijlstra8f1bc3852008-04-19 19:45:00 +02001390/*
1391 * delta *= weight / lw
1392 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001393static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001394calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1395 struct load_weight *lw)
1396{
1397 u64 tmp;
1398
Peter Zijlstrae05510d2008-05-05 23:56:17 +02001399 if (!lw->inv_weight)
1400 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)/(lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001401
1402 tmp = (u64)delta_exec * weight;
1403 /*
1404 * Check whether we'd overflow the 64-bit multiplication:
1405 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001406 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001407 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001408 WMULT_SHIFT/2);
1409 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001410 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001411
Ingo Molnarecf691d2007-08-02 17:41:40 +02001412 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001413}
1414
Ingo Molnar10919852007-10-15 17:00:04 +02001415static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001416{
1417 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001418 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001419}
1420
Ingo Molnar10919852007-10-15 17:00:04 +02001421static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001422{
1423 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001424 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001425}
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001428 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1429 * of tasks with abnormal "nice" values across CPUs the contribution that
1430 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001431 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001432 * scaled version of the new time slice allocation that they receive on time
1433 * slice expiry etc.
1434 */
1435
Ingo Molnardd41f592007-07-09 18:51:59 +02001436#define WEIGHT_IDLEPRIO 2
1437#define WMULT_IDLEPRIO (1 << 31)
1438
1439/*
1440 * Nice levels are multiplicative, with a gentle 10% change for every
1441 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1442 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1443 * that remained on nice 0.
1444 *
1445 * The "10% effect" is relative and cumulative: from _any_ nice level,
1446 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee62007-07-16 09:46:30 +02001447 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1448 * If a task goes up by ~10% and another task goes down by ~10% then
1449 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001450 */
1451static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001452 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1453 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1454 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1455 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1456 /* 0 */ 1024, 820, 655, 526, 423,
1457 /* 5 */ 335, 272, 215, 172, 137,
1458 /* 10 */ 110, 87, 70, 56, 45,
1459 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001460};
1461
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001462/*
1463 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1464 *
1465 * In cases where the weight does not change often, we can use the
1466 * precalculated inverse to speed up arithmetics by turning divisions
1467 * into multiplications:
1468 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001469static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001470 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1471 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1472 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1473 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1474 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1475 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1476 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1477 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001478};
Peter Williams2dd73a42006-06-27 02:54:34 -07001479
Ingo Molnardd41f592007-07-09 18:51:59 +02001480static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1481
1482/*
1483 * runqueue iterator, to support SMP load-balancing between different
1484 * scheduling classes, without having to expose their internal data
1485 * structures to the load-balancing proper:
1486 */
1487struct rq_iterator {
1488 void *arg;
1489 struct task_struct *(*start)(void *);
1490 struct task_struct *(*next)(void *);
1491};
1492
Peter Williamse1d14842007-10-24 18:23:51 +02001493#ifdef CONFIG_SMP
1494static unsigned long
1495balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1496 unsigned long max_load_move, struct sched_domain *sd,
1497 enum cpu_idle_type idle, int *all_pinned,
1498 int *this_best_prio, struct rq_iterator *iterator);
1499
1500static int
1501iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1502 struct sched_domain *sd, enum cpu_idle_type idle,
1503 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001504#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001505
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001506#ifdef CONFIG_CGROUP_CPUACCT
1507static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1508#else
1509static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1510#endif
1511
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001512static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1513{
1514 update_load_add(&rq->load, load);
1515}
1516
1517static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1518{
1519 update_load_sub(&rq->load, load);
1520}
1521
Gregory Haskinse7693a32008-01-25 21:08:09 +01001522#ifdef CONFIG_SMP
1523static unsigned long source_load(int cpu, int type);
1524static unsigned long target_load(int cpu, int type);
1525static unsigned long cpu_avg_load_per_task(int cpu);
1526static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001527
1528#ifdef CONFIG_FAIR_GROUP_SCHED
1529
1530/*
1531 * Group load balancing.
1532 *
1533 * We calculate a few balance domain wide aggregate numbers; load and weight.
1534 * Given the pictures below, and assuming each item has equal weight:
1535 *
1536 * root 1 - thread
1537 * / | \ A - group
1538 * A 1 B
1539 * /|\ / \
1540 * C 2 D 3 4
1541 * | |
1542 * 5 6
1543 *
1544 * load:
1545 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1546 * which equals 1/9-th of the total load.
1547 *
1548 * shares:
1549 * The weight of this group on the selected cpus.
1550 *
1551 * rq_weight:
1552 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1553 * B would get 2.
1554 *
1555 * task_weight:
1556 * Part of the rq_weight contributed by tasks; all groups except B would
1557 * get 1, B gets 2.
1558 */
1559
1560static inline struct aggregate_struct *
1561aggregate(struct task_group *tg, struct sched_domain *sd)
1562{
1563 return &tg->cfs_rq[sd->first_cpu]->aggregate;
1564}
1565
1566typedef void (*aggregate_func)(struct task_group *, struct sched_domain *);
1567
1568/*
1569 * Iterate the full tree, calling @down when first entering a node and @up when
1570 * leaving it for the final time.
1571 */
1572static
1573void aggregate_walk_tree(aggregate_func down, aggregate_func up,
1574 struct sched_domain *sd)
1575{
1576 struct task_group *parent, *child;
1577
1578 rcu_read_lock();
1579 parent = &root_task_group;
1580down:
1581 (*down)(parent, sd);
1582 list_for_each_entry_rcu(child, &parent->children, siblings) {
1583 parent = child;
1584 goto down;
1585
1586up:
1587 continue;
1588 }
1589 (*up)(parent, sd);
1590
1591 child = parent;
1592 parent = parent->parent;
1593 if (parent)
1594 goto up;
1595 rcu_read_unlock();
1596}
1597
1598/*
1599 * Calculate the aggregate runqueue weight.
1600 */
1601static
1602void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd)
1603{
1604 unsigned long rq_weight = 0;
1605 unsigned long task_weight = 0;
1606 int i;
1607
1608 for_each_cpu_mask(i, sd->span) {
1609 rq_weight += tg->cfs_rq[i]->load.weight;
1610 task_weight += tg->cfs_rq[i]->task_weight;
1611 }
1612
1613 aggregate(tg, sd)->rq_weight = rq_weight;
1614 aggregate(tg, sd)->task_weight = task_weight;
1615}
1616
1617/*
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001618 * Compute the weight of this group on the given cpus.
1619 */
1620static
1621void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd)
1622{
1623 unsigned long shares = 0;
1624 int i;
1625
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001626 for_each_cpu_mask(i, sd->span)
1627 shares += tg->cfs_rq[i]->shares;
1628
Peter Zijlstra3f5087a2008-04-25 00:25:08 +02001629 if ((!shares && aggregate(tg, sd)->rq_weight) || shares > tg->shares)
1630 shares = tg->shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001631
1632 aggregate(tg, sd)->shares = shares;
1633}
1634
1635/*
1636 * Compute the load fraction assigned to this group, relies on the aggregate
1637 * weight and this group's parent's load, i.e. top-down.
1638 */
1639static
1640void aggregate_group_load(struct task_group *tg, struct sched_domain *sd)
1641{
1642 unsigned long load;
1643
1644 if (!tg->parent) {
1645 int i;
1646
1647 load = 0;
1648 for_each_cpu_mask(i, sd->span)
1649 load += cpu_rq(i)->load.weight;
1650
1651 } else {
1652 load = aggregate(tg->parent, sd)->load;
1653
1654 /*
1655 * shares is our weight in the parent's rq so
1656 * shares/parent->rq_weight gives our fraction of the load
1657 */
1658 load *= aggregate(tg, sd)->shares;
1659 load /= aggregate(tg->parent, sd)->rq_weight + 1;
1660 }
1661
1662 aggregate(tg, sd)->load = load;
1663}
1664
1665static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1666
1667/*
1668 * Calculate and set the cpu's group shares.
1669 */
1670static void
1671__update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,
1672 int tcpu)
1673{
1674 int boost = 0;
1675 unsigned long shares;
1676 unsigned long rq_weight;
1677
1678 if (!tg->se[tcpu])
1679 return;
1680
1681 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1682
1683 /*
1684 * If there are currently no tasks on the cpu pretend there is one of
1685 * average load so that when a new task gets to run here it will not
1686 * get delayed by group starvation.
1687 */
1688 if (!rq_weight) {
1689 boost = 1;
1690 rq_weight = NICE_0_LOAD;
1691 }
1692
1693 /*
1694 * \Sum shares * rq_weight
1695 * shares = -----------------------
1696 * \Sum rq_weight
1697 *
1698 */
1699 shares = aggregate(tg, sd)->shares * rq_weight;
1700 shares /= aggregate(tg, sd)->rq_weight + 1;
1701
1702 /*
1703 * record the actual number of shares, not the boosted amount.
1704 */
1705 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1706
1707 if (shares < MIN_SHARES)
1708 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08001709 else if (shares > MAX_SHARES)
1710 shares = MAX_SHARES;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001711
1712 __set_se_shares(tg->se[tcpu], shares);
1713}
1714
1715/*
1716 * Re-adjust the weights on the cpu the task came from and on the cpu the
1717 * task went to.
1718 */
1719static void
1720__move_group_shares(struct task_group *tg, struct sched_domain *sd,
1721 int scpu, int dcpu)
1722{
1723 unsigned long shares;
1724
1725 shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1726
1727 __update_group_shares_cpu(tg, sd, scpu);
1728 __update_group_shares_cpu(tg, sd, dcpu);
1729
1730 /*
1731 * ensure we never loose shares due to rounding errors in the
1732 * above redistribution.
1733 */
1734 shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1735 if (shares)
1736 tg->cfs_rq[dcpu]->shares += shares;
1737}
1738
1739/*
1740 * Because changing a group's shares changes the weight of the super-group
1741 * we need to walk up the tree and change all shares until we hit the root.
1742 */
1743static void
1744move_group_shares(struct task_group *tg, struct sched_domain *sd,
1745 int scpu, int dcpu)
1746{
1747 while (tg) {
1748 __move_group_shares(tg, sd, scpu, dcpu);
1749 tg = tg->parent;
1750 }
1751}
1752
1753static
1754void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd)
1755{
1756 unsigned long shares = aggregate(tg, sd)->shares;
1757 int i;
1758
1759 for_each_cpu_mask(i, sd->span) {
1760 struct rq *rq = cpu_rq(i);
1761 unsigned long flags;
1762
1763 spin_lock_irqsave(&rq->lock, flags);
1764 __update_group_shares_cpu(tg, sd, i);
1765 spin_unlock_irqrestore(&rq->lock, flags);
1766 }
1767
1768 aggregate_group_shares(tg, sd);
1769
1770 /*
1771 * ensure we never loose shares due to rounding errors in the
1772 * above redistribution.
1773 */
1774 shares -= aggregate(tg, sd)->shares;
1775 if (shares) {
1776 tg->cfs_rq[sd->first_cpu]->shares += shares;
1777 aggregate(tg, sd)->shares += shares;
1778 }
1779}
1780
1781/*
1782 * Calculate the accumulative weight and recursive load of each task group
1783 * while walking down the tree.
1784 */
1785static
1786void aggregate_get_down(struct task_group *tg, struct sched_domain *sd)
1787{
1788 aggregate_group_weight(tg, sd);
1789 aggregate_group_shares(tg, sd);
1790 aggregate_group_load(tg, sd);
1791}
1792
1793/*
1794 * Rebalance the cpu shares while walking back up the tree.
1795 */
1796static
1797void aggregate_get_up(struct task_group *tg, struct sched_domain *sd)
1798{
1799 aggregate_group_set_shares(tg, sd);
1800}
1801
1802static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1803
1804static void __init init_aggregate(void)
1805{
1806 int i;
1807
1808 for_each_possible_cpu(i)
1809 spin_lock_init(&per_cpu(aggregate_lock, i));
1810}
1811
1812static int get_aggregate(struct sched_domain *sd)
1813{
1814 if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu)))
1815 return 0;
1816
1817 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd);
1818 return 1;
1819}
1820
1821static void put_aggregate(struct sched_domain *sd)
1822{
1823 spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu));
1824}
1825
1826static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1827{
1828 cfs_rq->shares = shares;
1829}
1830
1831#else
1832
1833static inline void init_aggregate(void)
1834{
1835}
1836
1837static inline int get_aggregate(struct sched_domain *sd)
1838{
1839 return 0;
1840}
1841
1842static inline void put_aggregate(struct sched_domain *sd)
1843{
1844}
1845#endif
1846
1847#else /* CONFIG_SMP */
1848
1849#ifdef CONFIG_FAIR_GROUP_SCHED
1850static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1851{
1852}
1853#endif
1854
Gregory Haskinse7693a32008-01-25 21:08:09 +01001855#endif /* CONFIG_SMP */
1856
Ingo Molnardd41f592007-07-09 18:51:59 +02001857#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001858#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001859#include "sched_fair.c"
1860#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001861#ifdef CONFIG_SCHED_DEBUG
1862# include "sched_debug.c"
1863#endif
1864
1865#define sched_class_highest (&rt_sched_class)
1866
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001867static void inc_nr_running(struct rq *rq)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001868{
1869 rq->nr_running++;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001870}
1871
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001872static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001873{
1874 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001875}
1876
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001877static void set_load_weight(struct task_struct *p)
1878{
1879 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001880 p->se.load.weight = prio_to_weight[0] * 2;
1881 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1882 return;
1883 }
1884
1885 /*
1886 * SCHED_IDLE tasks get minimal weight:
1887 */
1888 if (p->policy == SCHED_IDLE) {
1889 p->se.load.weight = WEIGHT_IDLEPRIO;
1890 p->se.load.inv_weight = WMULT_IDLEPRIO;
1891 return;
1892 }
1893
1894 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1895 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001896}
1897
Ingo Molnar8159f872007-08-09 11:16:49 +02001898static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001899{
1900 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001901 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001902 p->se.on_rq = 1;
1903}
1904
Ingo Molnar69be72c2007-08-09 11:16:49 +02001905static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001906{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001907 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001908 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001909}
1910
1911/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001912 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001913 */
Ingo Molnar14531182007-07-09 18:51:59 +02001914static inline int __normal_prio(struct task_struct *p)
1915{
Ingo Molnardd41f592007-07-09 18:51:59 +02001916 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001917}
1918
1919/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001920 * Calculate the expected normal priority: i.e. priority
1921 * without taking RT-inheritance into account. Might be
1922 * boosted by interactivity modifiers. Changes upon fork,
1923 * setprio syscalls, and whenever the interactivity
1924 * estimator recalculates.
1925 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001926static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001927{
1928 int prio;
1929
Ingo Molnare05606d2007-07-09 18:51:59 +02001930 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001931 prio = MAX_RT_PRIO-1 - p->rt_priority;
1932 else
1933 prio = __normal_prio(p);
1934 return prio;
1935}
1936
1937/*
1938 * Calculate the current priority, i.e. the priority
1939 * taken into account by the scheduler. This value might
1940 * be boosted by RT tasks, or might be boosted by
1941 * interactivity modifiers. Will be RT if the task got
1942 * RT-boosted. If not then it returns p->normal_prio.
1943 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001944static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001945{
1946 p->normal_prio = normal_prio(p);
1947 /*
1948 * If we are RT tasks or we were boosted to RT priority,
1949 * keep the priority unchanged. Otherwise, update priority
1950 * to the normal priority:
1951 */
1952 if (!rt_prio(p->prio))
1953 return p->normal_prio;
1954 return p->prio;
1955}
1956
1957/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001958 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001960static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001962 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001963 rq->nr_uninterruptible--;
1964
Ingo Molnar8159f872007-08-09 11:16:49 +02001965 enqueue_task(rq, p, wakeup);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001966 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
1969/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 * deactivate_task - remove a task from the runqueue.
1971 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001972static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001974 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001975 rq->nr_uninterruptible++;
1976
Ingo Molnar69be72c2007-08-09 11:16:49 +02001977 dequeue_task(rq, p, sleep);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001978 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981/**
1982 * task_curr - is this task currently executing on a CPU?
1983 * @p: the task in question.
1984 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001985inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 return cpu_curr(task_cpu(p)) == p;
1988}
1989
Peter Williams2dd73a42006-06-27 02:54:34 -07001990/* Used instead of source_load when we know the type == 0 */
1991unsigned long weighted_cpuload(const int cpu)
1992{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001993 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001994}
1995
1996static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1997{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001998 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001999#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01002000 /*
2001 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2002 * successfuly executed on another CPU. We must ensure that updates of
2003 * per-task data have been completed by this moment.
2004 */
2005 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02002006 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02002007#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07002008}
2009
Steven Rostedtcb469842008-01-25 21:08:22 +01002010static inline void check_class_changed(struct rq *rq, struct task_struct *p,
2011 const struct sched_class *prev_class,
2012 int oldprio, int running)
2013{
2014 if (prev_class != p->sched_class) {
2015 if (prev_class->switched_from)
2016 prev_class->switched_from(rq, p, running);
2017 p->sched_class->switched_to(rq, p, running);
2018 } else
2019 p->sched_class->prio_changed(rq, p, oldprio, running);
2020}
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02002023
Ingo Molnarcc367732007-10-15 17:00:18 +02002024/*
2025 * Is this task likely cache-hot:
2026 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002027static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002028task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2029{
2030 s64 delta;
2031
Ingo Molnarf540a602008-03-15 17:10:34 +01002032 /*
2033 * Buddy candidates are cache hot:
2034 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01002035 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01002036 return 1;
2037
Ingo Molnarcc367732007-10-15 17:00:18 +02002038 if (p->sched_class != &fair_sched_class)
2039 return 0;
2040
Ingo Molnar6bc16652007-10-15 17:00:18 +02002041 if (sysctl_sched_migration_cost == -1)
2042 return 1;
2043 if (sysctl_sched_migration_cost == 0)
2044 return 0;
2045
Ingo Molnarcc367732007-10-15 17:00:18 +02002046 delta = now - p->se.exec_start;
2047
2048 return delta < (s64)sysctl_sched_migration_cost;
2049}
2050
2051
Ingo Molnardd41f592007-07-09 18:51:59 +02002052void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002053{
Ingo Molnardd41f592007-07-09 18:51:59 +02002054 int old_cpu = task_cpu(p);
2055 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002056 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
2057 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02002058 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002059
2060 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002061
2062#ifdef CONFIG_SCHEDSTATS
2063 if (p->se.wait_start)
2064 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002065 if (p->se.sleep_start)
2066 p->se.sleep_start -= clock_offset;
2067 if (p->se.block_start)
2068 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02002069 if (old_cpu != new_cpu) {
2070 schedstat_inc(p, se.nr_migrations);
2071 if (task_hot(p, old_rq->clock, NULL))
2072 schedstat_inc(p, se.nr_forced2_migrations);
2073 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002074#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002075 p->se.vruntime -= old_cfsrq->min_vruntime -
2076 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02002077
2078 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002079}
2080
Ingo Molnar70b97a72006-07-03 00:25:42 -07002081struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Ingo Molnar36c8b582006-07-03 00:25:41 -07002084 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 int dest_cpu;
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002088};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090/*
2091 * The task's runqueue lock must be held.
2092 * Returns true if you have to wait for migration thread.
2093 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002094static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002095migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002097 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 /*
2100 * If the task is not on a runqueue (and not running), then
2101 * it is sufficient to simply update the task's cpu field.
2102 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002103 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 set_task_cpu(p, dest_cpu);
2105 return 0;
2106 }
2107
2108 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 req->task = p;
2110 req->dest_cpu = dest_cpu;
2111 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return 1;
2114}
2115
2116/*
2117 * wait_task_inactive - wait for a thread to unschedule.
2118 *
2119 * The caller must ensure that the task *will* unschedule sometime soon,
2120 * else this function might spin for a *long* time. This function can't
2121 * be called with interrupts off, or it may introduce deadlock with
2122 * smp_call_function() if an IPI is sent by the same process we are
2123 * waiting to become inactive.
2124 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002125void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002128 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002129 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Andi Kleen3a5c3592007-10-15 17:00:14 +02002131 for (;;) {
2132 /*
2133 * We do the initial early heuristics without holding
2134 * any task-queue locks at all. We'll only try to get
2135 * the runqueue lock when things look like they will
2136 * work out!
2137 */
2138 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002139
Andi Kleen3a5c3592007-10-15 17:00:14 +02002140 /*
2141 * If the task is actively running on another CPU
2142 * still, just relax and busy-wait without holding
2143 * any locks.
2144 *
2145 * NOTE! Since we don't hold any locks, it's not
2146 * even sure that "rq" stays as the right runqueue!
2147 * But we don't care, since "task_running()" will
2148 * return false if the runqueue has changed and p
2149 * is actually now running somewhere else!
2150 */
2151 while (task_running(rq, p))
2152 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002153
Andi Kleen3a5c3592007-10-15 17:00:14 +02002154 /*
2155 * Ok, time to look more closely! We need the rq
2156 * lock now, to be *sure*. If we're wrong, we'll
2157 * just go back and repeat.
2158 */
2159 rq = task_rq_lock(p, &flags);
2160 running = task_running(rq, p);
2161 on_rq = p->se.on_rq;
2162 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002163
Andi Kleen3a5c3592007-10-15 17:00:14 +02002164 /*
2165 * Was it really running after all now that we
2166 * checked with the proper locks actually held?
2167 *
2168 * Oops. Go back and try again..
2169 */
2170 if (unlikely(running)) {
2171 cpu_relax();
2172 continue;
2173 }
2174
2175 /*
2176 * It's not enough that it's not actively running,
2177 * it must be off the runqueue _entirely_, and not
2178 * preempted!
2179 *
2180 * So if it wa still runnable (but just not actively
2181 * running right now), it's preempted, and we should
2182 * yield - it could be a while.
2183 */
2184 if (unlikely(on_rq)) {
2185 schedule_timeout_uninterruptible(1);
2186 continue;
2187 }
2188
2189 /*
2190 * Ahh, all good. It wasn't running, and it wasn't
2191 * runnable, which means that it will never become
2192 * running in the future either. We're all done!
2193 */
2194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
2198/***
2199 * kick_process - kick a running thread to enter/exit the kernel
2200 * @p: the to-be-kicked thread
2201 *
2202 * Cause a process which is running on another CPU to enter
2203 * kernel-mode, without any delay. (to get signals handled.)
2204 *
2205 * NOTE: this function doesnt have to take the runqueue lock,
2206 * because all it wants to ensure is that the remote task enters
2207 * the kernel. If the IPI races and the task has been migrated
2208 * to another CPU then no harm is done and the purpose has been
2209 * achieved as well.
2210 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002211void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
2213 int cpu;
2214
2215 preempt_disable();
2216 cpu = task_cpu(p);
2217 if ((cpu != smp_processor_id()) && task_curr(p))
2218 smp_send_reschedule(cpu);
2219 preempt_enable();
2220}
2221
2222/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002223 * Return a low guess at the load of a migration-source cpu weighted
2224 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 *
2226 * We want to under-estimate the load of migration sources, to
2227 * balance conservatively.
2228 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002229static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002230{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002231 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002232 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002233
Peter Williams2dd73a42006-06-27 02:54:34 -07002234 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002235 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002236
Ingo Molnardd41f592007-07-09 18:51:59 +02002237 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
2240/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002241 * Return a high guess at the load of a migration-target cpu weighted
2242 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002244static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002245{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002246 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002247 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002248
Peter Williams2dd73a42006-06-27 02:54:34 -07002249 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002250 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002251
Ingo Molnardd41f592007-07-09 18:51:59 +02002252 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002253}
2254
2255/*
2256 * Return the average load per task on the cpu's run queue
2257 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002258static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07002259{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002260 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002261 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07002262 unsigned long n = rq->nr_running;
2263
Ingo Molnardd41f592007-07-09 18:51:59 +02002264 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
Nick Piggin147cbb42005-06-25 14:57:19 -07002267/*
2268 * find_idlest_group finds and returns the least busy CPU group within the
2269 * domain.
2270 */
2271static struct sched_group *
2272find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2273{
2274 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2275 unsigned long min_load = ULONG_MAX, this_load = 0;
2276 int load_idx = sd->forkexec_idx;
2277 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2278
2279 do {
2280 unsigned long load, avg_load;
2281 int local_group;
2282 int i;
2283
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002284 /* Skip over this group if it has no CPUs allowed */
2285 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002286 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002287
Nick Piggin147cbb42005-06-25 14:57:19 -07002288 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002289
2290 /* Tally up the load of all CPUs in the group */
2291 avg_load = 0;
2292
2293 for_each_cpu_mask(i, group->cpumask) {
2294 /* Bias balancing toward cpus of our domain */
2295 if (local_group)
2296 load = source_load(i, load_idx);
2297 else
2298 load = target_load(i, load_idx);
2299
2300 avg_load += load;
2301 }
2302
2303 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002304 avg_load = sg_div_cpu_power(group,
2305 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002306
2307 if (local_group) {
2308 this_load = avg_load;
2309 this = group;
2310 } else if (avg_load < min_load) {
2311 min_load = avg_load;
2312 idlest = group;
2313 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002314 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002315
2316 if (!idlest || 100*this_load < imbalance*min_load)
2317 return NULL;
2318 return idlest;
2319}
2320
2321/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002322 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002323 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002324static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002325find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2326 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002327{
2328 unsigned long load, min_load = ULONG_MAX;
2329 int idlest = -1;
2330 int i;
2331
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002332 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002333 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002334
Mike Travis7c16ec52008-04-04 18:11:11 -07002335 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002336 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002337
2338 if (load < min_load || (load == min_load && i == this_cpu)) {
2339 min_load = load;
2340 idlest = i;
2341 }
2342 }
2343
2344 return idlest;
2345}
2346
Nick Piggin476d1392005-06-25 14:57:29 -07002347/*
2348 * sched_balance_self: balance the current task (running on cpu) in domains
2349 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2350 * SD_BALANCE_EXEC.
2351 *
2352 * Balance, ie. select the least loaded group.
2353 *
2354 * Returns the target CPU number, or the same CPU if no balancing is needed.
2355 *
2356 * preempt must be disabled.
2357 */
2358static int sched_balance_self(int cpu, int flag)
2359{
2360 struct task_struct *t = current;
2361 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002362
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002363 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002364 /*
2365 * If power savings logic is enabled for a domain, stop there.
2366 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002367 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2368 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002369 if (tmp->flags & flag)
2370 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002371 }
Nick Piggin476d1392005-06-25 14:57:29 -07002372
2373 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002374 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002375 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002376 int new_cpu, weight;
2377
2378 if (!(sd->flags & flag)) {
2379 sd = sd->child;
2380 continue;
2381 }
Nick Piggin476d1392005-06-25 14:57:29 -07002382
2383 span = sd->span;
2384 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002385 if (!group) {
2386 sd = sd->child;
2387 continue;
2388 }
Nick Piggin476d1392005-06-25 14:57:29 -07002389
Mike Travis7c16ec52008-04-04 18:11:11 -07002390 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002391 if (new_cpu == -1 || new_cpu == cpu) {
2392 /* Now try balancing at a lower domain level of cpu */
2393 sd = sd->child;
2394 continue;
2395 }
Nick Piggin476d1392005-06-25 14:57:29 -07002396
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002397 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002398 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002399 sd = NULL;
2400 weight = cpus_weight(span);
2401 for_each_domain(cpu, tmp) {
2402 if (weight <= cpus_weight(tmp->span))
2403 break;
2404 if (tmp->flags & flag)
2405 sd = tmp;
2406 }
2407 /* while loop will break here if sd == NULL */
2408 }
2409
2410 return cpu;
2411}
2412
2413#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415/***
2416 * try_to_wake_up - wake up a thread
2417 * @p: the to-be-woken-up thread
2418 * @state: the mask of task states that can be woken
2419 * @sync: do a synchronous wakeup?
2420 *
2421 * Put it on the run-queue if it's not already there. The "current"
2422 * thread is always on the run-queue (except when the actual
2423 * re-schedule is in progress), and as such you're allowed to do
2424 * the simpler "current->state = TASK_RUNNING" to mark yourself
2425 * runnable without the overhead of this.
2426 *
2427 * returns failure only if the task is already active.
2428 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002429static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Ingo Molnarcc367732007-10-15 17:00:18 +02002431 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 unsigned long flags;
2433 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002434 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Ingo Molnarb85d0662008-03-16 20:03:22 +01002436 if (!sched_feat(SYNC_WAKEUPS))
2437 sync = 0;
2438
Linus Torvalds04e2f172008-02-23 18:05:03 -08002439 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 rq = task_rq_lock(p, &flags);
2441 old_state = p->state;
2442 if (!(old_state & state))
2443 goto out;
2444
Ingo Molnardd41f592007-07-09 18:51:59 +02002445 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 goto out_running;
2447
2448 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002449 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 this_cpu = smp_processor_id();
2451
2452#ifdef CONFIG_SMP
2453 if (unlikely(task_running(rq, p)))
2454 goto out_activate;
2455
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002456 cpu = p->sched_class->select_task_rq(p, sync);
2457 if (cpu != orig_cpu) {
2458 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 task_rq_unlock(rq, &flags);
2460 /* might preempt at this point */
2461 rq = task_rq_lock(p, &flags);
2462 old_state = p->state;
2463 if (!(old_state & state))
2464 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002465 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 goto out_running;
2467
2468 this_cpu = smp_processor_id();
2469 cpu = task_cpu(p);
2470 }
2471
Gregory Haskinse7693a32008-01-25 21:08:09 +01002472#ifdef CONFIG_SCHEDSTATS
2473 schedstat_inc(rq, ttwu_count);
2474 if (cpu == this_cpu)
2475 schedstat_inc(rq, ttwu_local);
2476 else {
2477 struct sched_domain *sd;
2478 for_each_domain(this_cpu, sd) {
2479 if (cpu_isset(cpu, sd->span)) {
2480 schedstat_inc(sd, ttwu_wake_remote);
2481 break;
2482 }
2483 }
2484 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002485#endif
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487out_activate:
2488#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002489 schedstat_inc(p, se.nr_wakeups);
2490 if (sync)
2491 schedstat_inc(p, se.nr_wakeups_sync);
2492 if (orig_cpu != cpu)
2493 schedstat_inc(p, se.nr_wakeups_migrate);
2494 if (cpu == this_cpu)
2495 schedstat_inc(p, se.nr_wakeups_local);
2496 else
2497 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002498 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002499 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 success = 1;
2501
2502out_running:
Peter Zijlstra5429db22008-05-12 21:20:53 +02002503 ftrace_wake_up_task(rq, p, rq->curr);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002504 check_preempt_curr(rq, p);
2505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002507#ifdef CONFIG_SMP
2508 if (p->sched_class->task_wake_up)
2509 p->sched_class->task_wake_up(rq, p);
2510#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511out:
2512 task_rq_unlock(rq, &flags);
2513
2514 return success;
2515}
2516
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002517int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002519 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521EXPORT_SYMBOL(wake_up_process);
2522
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002523int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
2525 return try_to_wake_up(p, state, 0);
2526}
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528/*
2529 * Perform scheduler related setup for a newly forked process p.
2530 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002531 *
2532 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002534static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Ingo Molnardd41f592007-07-09 18:51:59 +02002536 p->se.exec_start = 0;
2537 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002538 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002539 p->se.last_wakeup = 0;
2540 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002541
2542#ifdef CONFIG_SCHEDSTATS
2543 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002544 p->se.sum_sleep_runtime = 0;
2545 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002546 p->se.block_start = 0;
2547 p->se.sleep_max = 0;
2548 p->se.block_max = 0;
2549 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002550 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002551 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002552#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002553
Peter Zijlstrafa717062008-01-25 21:08:27 +01002554 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002555 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002556 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002557
Avi Kivitye107be32007-07-26 13:40:43 +02002558#ifdef CONFIG_PREEMPT_NOTIFIERS
2559 INIT_HLIST_HEAD(&p->preempt_notifiers);
2560#endif
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 /*
2563 * We mark the process as running here, but have not actually
2564 * inserted it onto the runqueue yet. This guarantees that
2565 * nobody will actually run it, and a signal or other external
2566 * event cannot wake it up and insert it on the runqueue either.
2567 */
2568 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002569}
2570
2571/*
2572 * fork()/clone()-time setup:
2573 */
2574void sched_fork(struct task_struct *p, int clone_flags)
2575{
2576 int cpu = get_cpu();
2577
2578 __sched_fork(p);
2579
2580#ifdef CONFIG_SMP
2581 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2582#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002583 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002584
2585 /*
2586 * Make sure we do not leak PI boosting priority to the child:
2587 */
2588 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002589 if (!rt_prio(p->prio))
2590 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002591
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002592#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002593 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002594 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002596#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002597 p->oncpu = 0;
2598#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002600 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002601 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002603 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
2606/*
2607 * wake_up_new_task - wake up a newly created task for the first time.
2608 *
2609 * This function will do some initial scheduler statistics housekeeping
2610 * that must be done for every newly created context, then puts the task
2611 * on the runqueue and wakes it.
2612 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002613void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
2615 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002616 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002620 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 p->prio = effective_prio(p);
2623
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002624 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002625 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002628 * Let the scheduling class do new task startup
2629 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002631 p->sched_class->task_new(rq, p);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002632 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
Ingo Molnar4e655512008-05-12 21:20:52 +02002634 ftrace_wake_up_task(rq, p, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02002635 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002636#ifdef CONFIG_SMP
2637 if (p->sched_class->task_wake_up)
2638 p->sched_class->task_wake_up(rq, p);
2639#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002640 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
Avi Kivitye107be32007-07-26 13:40:43 +02002643#ifdef CONFIG_PREEMPT_NOTIFIERS
2644
2645/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002646 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2647 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002648 */
2649void preempt_notifier_register(struct preempt_notifier *notifier)
2650{
2651 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2652}
2653EXPORT_SYMBOL_GPL(preempt_notifier_register);
2654
2655/**
2656 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002657 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002658 *
2659 * This is safe to call from within a preemption notifier.
2660 */
2661void preempt_notifier_unregister(struct preempt_notifier *notifier)
2662{
2663 hlist_del(&notifier->link);
2664}
2665EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2666
2667static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2668{
2669 struct preempt_notifier *notifier;
2670 struct hlist_node *node;
2671
2672 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2673 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2674}
2675
2676static void
2677fire_sched_out_preempt_notifiers(struct task_struct *curr,
2678 struct task_struct *next)
2679{
2680 struct preempt_notifier *notifier;
2681 struct hlist_node *node;
2682
2683 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2684 notifier->ops->sched_out(notifier, next);
2685}
2686
2687#else
2688
2689static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2690{
2691}
2692
2693static void
2694fire_sched_out_preempt_notifiers(struct task_struct *curr,
2695 struct task_struct *next)
2696{
2697}
2698
2699#endif
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002702 * prepare_task_switch - prepare to switch tasks
2703 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002704 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002705 * @next: the task we are going to switch to.
2706 *
2707 * This is called with the rq lock held and interrupts off. It must
2708 * be paired with a subsequent finish_task_switch after the context
2709 * switch.
2710 *
2711 * prepare_task_switch sets up locking and calls architecture specific
2712 * hooks.
2713 */
Avi Kivitye107be32007-07-26 13:40:43 +02002714static inline void
2715prepare_task_switch(struct rq *rq, struct task_struct *prev,
2716 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002717{
Avi Kivitye107be32007-07-26 13:40:43 +02002718 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002719 prepare_lock_switch(rq, next);
2720 prepare_arch_switch(next);
2721}
2722
2723/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002725 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 * @prev: the thread we just switched away from.
2727 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002728 * finish_task_switch must be called after the context switch, paired
2729 * with a prepare_task_switch call before the context switch.
2730 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2731 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 *
2733 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002734 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 * with the lock held can cause deadlocks; see schedule() for
2736 * details.)
2737 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002738static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 __releases(rq->lock)
2740{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002742 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 rq->prev_mm = NULL;
2745
2746 /*
2747 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002748 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002749 * schedule one last time. The schedule call will never return, and
2750 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002751 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 * still held, otherwise prev could be scheduled on another cpu, die
2753 * there before we look at prev->state, and then the reference would
2754 * be dropped twice.
2755 * Manfred Spraul <manfred@colorfullife.com>
2756 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002757 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002758 finish_arch_switch(prev);
2759 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002760#ifdef CONFIG_SMP
2761 if (current->sched_class->post_schedule)
2762 current->sched_class->post_schedule(rq);
2763#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002764
Avi Kivitye107be32007-07-26 13:40:43 +02002765 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (mm)
2767 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002768 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002769 /*
2770 * Remove function-return probe instances associated with this
2771 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002772 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002773 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
2778/**
2779 * schedule_tail - first thing a freshly forked thread must call.
2780 * @prev: the thread we just switched away from.
2781 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002782asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 __releases(rq->lock)
2784{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002785 struct rq *rq = this_rq();
2786
Nick Piggin4866cde2005-06-25 14:57:23 -07002787 finish_task_switch(rq, prev);
2788#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2789 /* In this case, finish_task_switch does not reenable preemption */
2790 preempt_enable();
2791#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002793 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
2796/*
2797 * context_switch - switch to the new MM and the new
2798 * thread's register state.
2799 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002800static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002801context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002802 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
Ingo Molnardd41f592007-07-09 18:51:59 +02002804 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Avi Kivitye107be32007-07-26 13:40:43 +02002806 prepare_task_switch(rq, prev, next);
Ingo Molnar4e655512008-05-12 21:20:52 +02002807 ftrace_ctx_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002808 mm = next->mm;
2809 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002810 /*
2811 * For paravirt, this is coupled with an exit in switch_to to
2812 * combine the page table reload and the switch backend into
2813 * one hypercall.
2814 */
2815 arch_enter_lazy_cpu_mode();
2816
Ingo Molnardd41f592007-07-09 18:51:59 +02002817 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 next->active_mm = oldmm;
2819 atomic_inc(&oldmm->mm_count);
2820 enter_lazy_tlb(oldmm, next);
2821 } else
2822 switch_mm(oldmm, mm, next);
2823
Ingo Molnardd41f592007-07-09 18:51:59 +02002824 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 rq->prev_mm = oldmm;
2827 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002828 /*
2829 * Since the runqueue lock will be released by the next
2830 * task (which is an invalid locking op but in the case
2831 * of the scheduler it's an obvious special-case), so we
2832 * do an early lockdep release here:
2833 */
2834#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002835 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002836#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 /* Here we just switch the register state and the stack. */
2839 switch_to(prev, next, prev);
2840
Ingo Molnardd41f592007-07-09 18:51:59 +02002841 barrier();
2842 /*
2843 * this_rq must be evaluated again because prev may have moved
2844 * CPUs since it called schedule(), thus the 'rq' on its stack
2845 * frame will be invalid.
2846 */
2847 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848}
2849
2850/*
2851 * nr_running, nr_uninterruptible and nr_context_switches:
2852 *
2853 * externally visible scheduler statistics: current number of runnable
2854 * threads, current number of uninterruptible-sleeping threads, total
2855 * number of context switches performed since bootup.
2856 */
2857unsigned long nr_running(void)
2858{
2859 unsigned long i, sum = 0;
2860
2861 for_each_online_cpu(i)
2862 sum += cpu_rq(i)->nr_running;
2863
2864 return sum;
2865}
2866
2867unsigned long nr_uninterruptible(void)
2868{
2869 unsigned long i, sum = 0;
2870
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002871 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 sum += cpu_rq(i)->nr_uninterruptible;
2873
2874 /*
2875 * Since we read the counters lockless, it might be slightly
2876 * inaccurate. Do not allow it to go below zero though:
2877 */
2878 if (unlikely((long)sum < 0))
2879 sum = 0;
2880
2881 return sum;
2882}
2883
2884unsigned long long nr_context_switches(void)
2885{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002886 int i;
2887 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002889 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 sum += cpu_rq(i)->nr_switches;
2891
2892 return sum;
2893}
2894
2895unsigned long nr_iowait(void)
2896{
2897 unsigned long i, sum = 0;
2898
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002899 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2901
2902 return sum;
2903}
2904
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002905unsigned long nr_active(void)
2906{
2907 unsigned long i, running = 0, uninterruptible = 0;
2908
2909 for_each_online_cpu(i) {
2910 running += cpu_rq(i)->nr_running;
2911 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2912 }
2913
2914 if (unlikely((long)uninterruptible < 0))
2915 uninterruptible = 0;
2916
2917 return running + uninterruptible;
2918}
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002921 * Update rq->cpu_load[] statistics. This function is usually called every
2922 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002923 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002924static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002925{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002926 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002927 int i, scale;
2928
2929 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002930
2931 /* Update our load: */
2932 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2933 unsigned long old_load, new_load;
2934
2935 /* scale is effectively 1 << i now, and >> i divides by scale */
2936
2937 old_load = this_rq->cpu_load[i];
2938 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002939 /*
2940 * Round up the averaging division if load is increasing. This
2941 * prevents us from getting stuck on 9 if the load is 10, for
2942 * example.
2943 */
2944 if (new_load > old_load)
2945 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002946 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2947 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002948}
2949
Ingo Molnardd41f592007-07-09 18:51:59 +02002950#ifdef CONFIG_SMP
2951
Ingo Molnar48f24c42006-07-03 00:25:40 -07002952/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 * double_rq_lock - safely lock two runqueues
2954 *
2955 * Note this does not disable interrupts like task_rq_lock,
2956 * you need to do so manually before calling.
2957 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002958static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 __acquires(rq1->lock)
2960 __acquires(rq2->lock)
2961{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002962 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (rq1 == rq2) {
2964 spin_lock(&rq1->lock);
2965 __acquire(rq2->lock); /* Fake it out ;) */
2966 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002967 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 spin_lock(&rq1->lock);
2969 spin_lock(&rq2->lock);
2970 } else {
2971 spin_lock(&rq2->lock);
2972 spin_lock(&rq1->lock);
2973 }
2974 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002975 update_rq_clock(rq1);
2976 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977}
2978
2979/*
2980 * double_rq_unlock - safely unlock two runqueues
2981 *
2982 * Note this does not restore interrupts like task_rq_unlock,
2983 * you need to do so manually after calling.
2984 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002985static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 __releases(rq1->lock)
2987 __releases(rq2->lock)
2988{
2989 spin_unlock(&rq1->lock);
2990 if (rq1 != rq2)
2991 spin_unlock(&rq2->lock);
2992 else
2993 __release(rq2->lock);
2994}
2995
2996/*
2997 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2998 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002999static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 __releases(this_rq->lock)
3001 __acquires(busiest->lock)
3002 __acquires(this_rq->lock)
3003{
Steven Rostedte8fa1362008-01-25 21:08:05 +01003004 int ret = 0;
3005
Kirill Korotaev054b9102006-12-10 02:20:11 -08003006 if (unlikely(!irqs_disabled())) {
3007 /* printk() doesn't work good under rq->lock */
3008 spin_unlock(&this_rq->lock);
3009 BUG_ON(1);
3010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003012 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 spin_unlock(&this_rq->lock);
3014 spin_lock(&busiest->lock);
3015 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01003016 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 } else
3018 spin_lock(&busiest->lock);
3019 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01003020 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021}
3022
3023/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 * If dest_cpu is allowed for this process, migrate the task to it.
3025 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003026 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 * the cpu_allowed mask is restored.
3028 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003029static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003031 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003033 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 rq = task_rq_lock(p, &flags);
3036 if (!cpu_isset(dest_cpu, p->cpus_allowed)
3037 || unlikely(cpu_is_offline(dest_cpu)))
3038 goto out;
3039
3040 /* force the process onto the specified CPU */
3041 if (migrate_task(p, dest_cpu, &req)) {
3042 /* Need to wait for migration thread (might exit: take ref). */
3043 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 get_task_struct(mt);
3046 task_rq_unlock(rq, &flags);
3047 wake_up_process(mt);
3048 put_task_struct(mt);
3049 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return;
3052 }
3053out:
3054 task_rq_unlock(rq, &flags);
3055}
3056
3057/*
Nick Piggin476d1392005-06-25 14:57:29 -07003058 * sched_exec - execve() is a valuable balancing opportunity, because at
3059 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 */
3061void sched_exec(void)
3062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003064 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003066 if (new_cpu != this_cpu)
3067 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
3070/*
3071 * pull_task - move a task from a remote runqueue to the local runqueue.
3072 * Both runqueues must be locked.
3073 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003074static void pull_task(struct rq *src_rq, struct task_struct *p,
3075 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003077 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003079 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 /*
3081 * Note that idle threads have a prio of MAX_PRIO, for this test
3082 * to be always true for them.
3083 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003084 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
3087/*
3088 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3089 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003090static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003091int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003092 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003093 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 /*
3096 * We do not migrate tasks that are:
3097 * 1) running (obviously), or
3098 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3099 * 3) are cache-hot on their current CPU.
3100 */
Ingo Molnarcc367732007-10-15 17:00:18 +02003101 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3102 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003104 }
Nick Piggin81026792005-06-25 14:57:07 -07003105 *all_pinned = 0;
3106
Ingo Molnarcc367732007-10-15 17:00:18 +02003107 if (task_running(rq, p)) {
3108 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003109 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Ingo Molnarda84d962007-10-15 17:00:18 +02003112 /*
3113 * Aggressive migration if:
3114 * 1) task is cache cold, or
3115 * 2) too many balance attempts have failed.
3116 */
3117
Ingo Molnar6bc16652007-10-15 17:00:18 +02003118 if (!task_hot(p, rq->clock, sd) ||
3119 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003120#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02003121 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003122 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003123 schedstat_inc(p, se.nr_forced_migrations);
3124 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003125#endif
3126 return 1;
3127 }
3128
Ingo Molnarcc367732007-10-15 17:00:18 +02003129 if (task_hot(p, rq->clock, sd)) {
3130 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003131 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 return 1;
3134}
3135
Peter Williamse1d14842007-10-24 18:23:51 +02003136static unsigned long
3137balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3138 unsigned long max_load_move, struct sched_domain *sd,
3139 enum cpu_idle_type idle, int *all_pinned,
3140 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003141{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003142 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02003143 struct task_struct *p;
3144 long rem_load_move = max_load_move;
3145
Peter Williamse1d14842007-10-24 18:23:51 +02003146 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003147 goto out;
3148
3149 pinned = 1;
3150
3151 /*
3152 * Start the load-balancing iterator:
3153 */
3154 p = iterator->start(iterator->arg);
3155next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003156 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003157 goto out;
3158 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003159 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02003160 * skip a task if it will be the highest priority task (i.e. smallest
3161 * prio value) on its new queue regardless of its load weight
3162 */
3163 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
3164 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003165 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003166 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003167 p = iterator->next(iterator->arg);
3168 goto next;
3169 }
3170
3171 pull_task(busiest, p, this_rq, this_cpu);
3172 pulled++;
3173 rem_load_move -= p->se.load.weight;
3174
3175 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003176 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003177 */
Peter Williamse1d14842007-10-24 18:23:51 +02003178 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003179 if (p->prio < *this_best_prio)
3180 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003181 p = iterator->next(iterator->arg);
3182 goto next;
3183 }
3184out:
3185 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003186 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003187 * so we can safely collect pull_task() stats here rather than
3188 * inside pull_task().
3189 */
3190 schedstat_add(sd, lb_gained[idle], pulled);
3191
3192 if (all_pinned)
3193 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003194
3195 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003196}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198/*
Peter Williams43010652007-08-09 11:16:46 +02003199 * move_tasks tries to move up to max_load_move weighted load from busiest to
3200 * this_rq, as part of a balancing operation within domain "sd".
3201 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 *
3203 * Called with both runqueues locked.
3204 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003205static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003206 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003207 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003208 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003210 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003211 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003212 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Ingo Molnardd41f592007-07-09 18:51:59 +02003214 do {
Peter Williams43010652007-08-09 11:16:46 +02003215 total_load_moved +=
3216 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003217 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003218 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003219 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003220 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Peter Williams43010652007-08-09 11:16:46 +02003222 return total_load_moved > 0;
3223}
3224
Peter Williamse1d14842007-10-24 18:23:51 +02003225static int
3226iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3227 struct sched_domain *sd, enum cpu_idle_type idle,
3228 struct rq_iterator *iterator)
3229{
3230 struct task_struct *p = iterator->start(iterator->arg);
3231 int pinned = 0;
3232
3233 while (p) {
3234 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3235 pull_task(busiest, p, this_rq, this_cpu);
3236 /*
3237 * Right now, this is only the second place pull_task()
3238 * is called, so we can safely collect pull_task()
3239 * stats here rather than inside pull_task().
3240 */
3241 schedstat_inc(sd, lb_gained[idle]);
3242
3243 return 1;
3244 }
3245 p = iterator->next(iterator->arg);
3246 }
3247
3248 return 0;
3249}
3250
Peter Williams43010652007-08-09 11:16:46 +02003251/*
3252 * move_one_task tries to move exactly one task from busiest to this_rq, as
3253 * part of active balancing operations within "domain".
3254 * Returns 1 if successful and 0 otherwise.
3255 *
3256 * Called with both runqueues locked.
3257 */
3258static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3259 struct sched_domain *sd, enum cpu_idle_type idle)
3260{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003261 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003262
3263 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003264 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003265 return 1;
3266
3267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268}
3269
3270/*
3271 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003272 * domain. It calculates and returns the amount of weighted load which
3273 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 */
3275static struct sched_group *
3276find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003277 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003278 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279{
3280 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3281 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003282 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003283 unsigned long busiest_load_per_task, busiest_nr_running;
3284 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003285 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003286#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3287 int power_savings_balance = 1;
3288 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3289 unsigned long min_nr_running = ULONG_MAX;
3290 struct sched_group *group_min = NULL, *group_leader = NULL;
3291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
3293 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003294 busiest_load_per_task = busiest_nr_running = 0;
3295 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003296 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003297 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003298 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003299 load_idx = sd->newidle_idx;
3300 else
3301 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
3303 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003304 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 int local_group;
3306 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003307 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003308 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003309 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
3311 local_group = cpu_isset(this_cpu, group->cpumask);
3312
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003313 if (local_group)
3314 balance_cpu = first_cpu(group->cpumask);
3315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003317 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02003318 max_cpu_load = 0;
3319 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
3321 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003322 struct rq *rq;
3323
3324 if (!cpu_isset(i, *cpus))
3325 continue;
3326
3327 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003328
Suresh Siddha9439aab2007-07-19 21:28:35 +02003329 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003330 *sd_idle = 0;
3331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003333 if (local_group) {
3334 if (idle_cpu(i) && !first_idle_cpu) {
3335 first_idle_cpu = 1;
3336 balance_cpu = i;
3337 }
3338
Nick Piggina2000572006-02-10 01:51:02 -08003339 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003340 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003341 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003342 if (load > max_cpu_load)
3343 max_cpu_load = load;
3344 if (min_cpu_load > load)
3345 min_cpu_load = load;
3346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
3348 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003349 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003350 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 }
3352
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003353 /*
3354 * First idle cpu or the first cpu(busiest) in this sched group
3355 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003356 * domains. In the newly idle case, we will allow all the cpu's
3357 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003358 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003359 if (idle != CPU_NEWLY_IDLE && local_group &&
3360 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003361 *balance = 0;
3362 goto ret;
3363 }
3364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003366 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
3368 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003369 avg_load = sg_div_cpu_power(group,
3370 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Ken Chen908a7c12007-10-17 16:55:11 +02003372 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3373 __group_imb = 1;
3374
Eric Dumazet5517d862007-05-08 00:32:57 -07003375 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 if (local_group) {
3378 this_load = avg_load;
3379 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003380 this_nr_running = sum_nr_running;
3381 this_load_per_task = sum_weighted_load;
3382 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003383 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 max_load = avg_load;
3385 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003386 busiest_nr_running = sum_nr_running;
3387 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003388 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003390
3391#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3392 /*
3393 * Busy processors will not participate in power savings
3394 * balance.
3395 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003396 if (idle == CPU_NOT_IDLE ||
3397 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3398 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003399
3400 /*
3401 * If the local group is idle or completely loaded
3402 * no need to do power savings balance at this domain
3403 */
3404 if (local_group && (this_nr_running >= group_capacity ||
3405 !this_nr_running))
3406 power_savings_balance = 0;
3407
Ingo Molnardd41f592007-07-09 18:51:59 +02003408 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003409 * If a group is already running at full capacity or idle,
3410 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003411 */
3412 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003413 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003414 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003415
Ingo Molnardd41f592007-07-09 18:51:59 +02003416 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003417 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003418 * This is the group from where we need to pick up the load
3419 * for saving power
3420 */
3421 if ((sum_nr_running < min_nr_running) ||
3422 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003423 first_cpu(group->cpumask) <
3424 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003425 group_min = group;
3426 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003427 min_load_per_task = sum_weighted_load /
3428 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003429 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003430
Ingo Molnardd41f592007-07-09 18:51:59 +02003431 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003432 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003433 * capacity but still has some space to pick up some load
3434 * from other group and save more power
3435 */
3436 if (sum_nr_running <= group_capacity - 1) {
3437 if (sum_nr_running > leader_nr_running ||
3438 (sum_nr_running == leader_nr_running &&
3439 first_cpu(group->cpumask) >
3440 first_cpu(group_leader->cpumask))) {
3441 group_leader = group;
3442 leader_nr_running = sum_nr_running;
3443 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003444 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003445group_next:
3446#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 group = group->next;
3448 } while (group != sd->groups);
3449
Peter Williams2dd73a42006-06-27 02:54:34 -07003450 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 goto out_balanced;
3452
3453 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3454
3455 if (this_load >= avg_load ||
3456 100*max_load <= sd->imbalance_pct*this_load)
3457 goto out_balanced;
3458
Peter Williams2dd73a42006-06-27 02:54:34 -07003459 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003460 if (group_imb)
3461 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 /*
3464 * We're trying to get all the cpus to the average_load, so we don't
3465 * want to push ourselves above the average load, nor do we wish to
3466 * reduce the max loaded cpu below the average load, as either of these
3467 * actions would just result in more rebalancing later, and ping-pong
3468 * tasks around. Thus we look for the minimum possible imbalance.
3469 * Negative imbalances (*we* are more loaded than anyone else) will
3470 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003471 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 * appear as very large values with unsigned longs.
3473 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003474 if (max_load <= busiest_load_per_task)
3475 goto out_balanced;
3476
3477 /*
3478 * In the presence of smp nice balancing, certain scenarios can have
3479 * max load less than avg load(as we skip the groups at or below
3480 * its cpu_power, while calculating max_load..)
3481 */
3482 if (max_load < avg_load) {
3483 *imbalance = 0;
3484 goto small_imbalance;
3485 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003486
3487 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003488 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003491 *imbalance = min(max_pull * busiest->__cpu_power,
3492 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 / SCHED_LOAD_SCALE;
3494
Peter Williams2dd73a42006-06-27 02:54:34 -07003495 /*
3496 * if *imbalance is less than the average load per runnable task
3497 * there is no gaurantee that any tasks will be moved so we'll have
3498 * a think about bumping its value to force at least one task to be
3499 * moved
3500 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003501 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003502 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003503 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Peter Williams2dd73a42006-06-27 02:54:34 -07003505small_imbalance:
3506 pwr_move = pwr_now = 0;
3507 imbn = 2;
3508 if (this_nr_running) {
3509 this_load_per_task /= this_nr_running;
3510 if (busiest_load_per_task > this_load_per_task)
3511 imbn = 1;
3512 } else
3513 this_load_per_task = SCHED_LOAD_SCALE;
3514
Ingo Molnardd41f592007-07-09 18:51:59 +02003515 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3516 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003517 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 return busiest;
3519 }
3520
3521 /*
3522 * OK, we don't have enough imbalance to justify moving tasks,
3523 * however we may be able to increase total CPU power used by
3524 * moving them.
3525 */
3526
Eric Dumazet5517d862007-05-08 00:32:57 -07003527 pwr_now += busiest->__cpu_power *
3528 min(busiest_load_per_task, max_load);
3529 pwr_now += this->__cpu_power *
3530 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 pwr_now /= SCHED_LOAD_SCALE;
3532
3533 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003534 tmp = sg_div_cpu_power(busiest,
3535 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003537 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003538 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
3540 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003541 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003542 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003543 tmp = sg_div_cpu_power(this,
3544 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003546 tmp = sg_div_cpu_power(this,
3547 busiest_load_per_task * SCHED_LOAD_SCALE);
3548 pwr_move += this->__cpu_power *
3549 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 pwr_move /= SCHED_LOAD_SCALE;
3551
3552 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003553 if (pwr_move > pwr_now)
3554 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 }
3556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 return busiest;
3558
3559out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003560#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003561 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003562 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003564 if (this == group_leader && group_leader != group_min) {
3565 *imbalance = min_load_per_task;
3566 return group_min;
3567 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003568#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003569ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 *imbalance = 0;
3571 return NULL;
3572}
3573
3574/*
3575 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3576 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003577static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003578find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003579 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003581 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003582 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 int i;
3584
3585 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003586 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003587
3588 if (!cpu_isset(i, *cpus))
3589 continue;
3590
Ingo Molnar48f24c42006-07-03 00:25:40 -07003591 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003592 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Ingo Molnardd41f592007-07-09 18:51:59 +02003594 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003595 continue;
3596
Ingo Molnardd41f592007-07-09 18:51:59 +02003597 if (wl > max_load) {
3598 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003599 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 }
3601 }
3602
3603 return busiest;
3604}
3605
3606/*
Nick Piggin77391d72005-06-25 14:57:30 -07003607 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3608 * so long as it is large enough.
3609 */
3610#define MAX_PINNED_INTERVAL 512
3611
3612/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3614 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003616static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003617 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003618 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
Peter Williams43010652007-08-09 11:16:46 +02003620 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003623 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003624 unsigned long flags;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003625 int unlock_aggregate;
Nick Piggin5969fe02005-09-10 00:26:19 -07003626
Mike Travis7c16ec52008-04-04 18:11:11 -07003627 cpus_setall(*cpus);
3628
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003629 unlock_aggregate = get_aggregate(sd);
3630
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003631 /*
3632 * When power savings policy is enabled for the parent domain, idle
3633 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003634 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003635 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003636 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003637 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003638 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003639 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Ingo Molnar2d723762007-10-15 17:00:12 +02003641 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003643redo:
3644 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003645 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003646
Chen, Kenneth W06066712006-12-10 02:20:35 -08003647 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003648 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 if (!group) {
3651 schedstat_inc(sd, lb_nobusyg[idle]);
3652 goto out_balanced;
3653 }
3654
Mike Travis7c16ec52008-04-04 18:11:11 -07003655 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 if (!busiest) {
3657 schedstat_inc(sd, lb_nobusyq[idle]);
3658 goto out_balanced;
3659 }
3660
Nick Piggindb935db2005-06-25 14:57:11 -07003661 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
3663 schedstat_add(sd, lb_imbalance[idle], imbalance);
3664
Peter Williams43010652007-08-09 11:16:46 +02003665 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 if (busiest->nr_running > 1) {
3667 /*
3668 * Attempt to move tasks. If find_busiest_group has found
3669 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003670 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 * correctly treated as an imbalance.
3672 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003673 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003674 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003675 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003676 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003677 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003678 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003679
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003680 /*
3681 * some other cpu did the load balance for us.
3682 */
Peter Williams43010652007-08-09 11:16:46 +02003683 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003684 resched_cpu(this_cpu);
3685
Nick Piggin81026792005-06-25 14:57:07 -07003686 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003687 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003688 cpu_clear(cpu_of(busiest), *cpus);
3689 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003690 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003691 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 }
Nick Piggin81026792005-06-25 14:57:07 -07003694
Peter Williams43010652007-08-09 11:16:46 +02003695 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 schedstat_inc(sd, lb_failed[idle]);
3697 sd->nr_balance_failed++;
3698
3699 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003701 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003702
3703 /* don't kick the migration_thread, if the curr
3704 * task on busiest cpu can't be moved to this_cpu
3705 */
3706 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003707 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003708 all_pinned = 1;
3709 goto out_one_pinned;
3710 }
3711
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 if (!busiest->active_balance) {
3713 busiest->active_balance = 1;
3714 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003715 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003717 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003718 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 wake_up_process(busiest->migration_thread);
3720
3721 /*
3722 * We've kicked active balancing, reset the failure
3723 * counter.
3724 */
Nick Piggin39507452005-06-25 14:57:09 -07003725 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 }
Nick Piggin81026792005-06-25 14:57:07 -07003727 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 sd->nr_balance_failed = 0;
3729
Nick Piggin81026792005-06-25 14:57:07 -07003730 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 /* We were unbalanced, so reset the balancing interval */
3732 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003733 } else {
3734 /*
3735 * If we've begun active balancing, start to back off. This
3736 * case may not be covered by the all_pinned logic if there
3737 * is only 1 task on the busy runqueue (because we don't call
3738 * move_tasks).
3739 */
3740 if (sd->balance_interval < sd->max_interval)
3741 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 }
3743
Peter Williams43010652007-08-09 11:16:46 +02003744 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003745 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003746 ld_moved = -1;
3747
3748 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
3750out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 schedstat_inc(sd, lb_balanced[idle]);
3752
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003753 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003754
3755out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003757 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3758 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 sd->balance_interval *= 2;
3760
Ingo Molnar48f24c42006-07-03 00:25:40 -07003761 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003762 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003763 ld_moved = -1;
3764 else
3765 ld_moved = 0;
3766out:
3767 if (unlock_aggregate)
3768 put_aggregate(sd);
3769 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770}
3771
3772/*
3773 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3774 * tasks if there is an imbalance.
3775 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003776 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 * this_rq is locked.
3778 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003779static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003780load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3781 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
3783 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003784 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003786 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003787 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003788 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003789
3790 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003791
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003792 /*
3793 * When power savings policy is enabled for the parent domain, idle
3794 * sibling can pick up load irrespective of busy siblings. In this case,
3795 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003796 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003797 */
3798 if (sd->flags & SD_SHARE_CPUPOWER &&
3799 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003800 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Ingo Molnar2d723762007-10-15 17:00:12 +02003802 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003803redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003804 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003805 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003807 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003808 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 }
3810
Mike Travis7c16ec52008-04-04 18:11:11 -07003811 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003812 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003813 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003814 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 }
3816
Nick Piggindb935db2005-06-25 14:57:11 -07003817 BUG_ON(busiest == this_rq);
3818
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003819 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003820
Peter Williams43010652007-08-09 11:16:46 +02003821 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003822 if (busiest->nr_running > 1) {
3823 /* Attempt to move tasks */
3824 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003825 /* this_rq->clock is already updated */
3826 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003827 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003828 imbalance, sd, CPU_NEWLY_IDLE,
3829 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003830 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003831
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003832 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003833 cpu_clear(cpu_of(busiest), *cpus);
3834 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003835 goto redo;
3836 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003837 }
3838
Peter Williams43010652007-08-09 11:16:46 +02003839 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003840 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003841 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3842 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003843 return -1;
3844 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003845 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Peter Williams43010652007-08-09 11:16:46 +02003847 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003848
3849out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003850 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003851 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003852 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003853 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003854 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003855
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857}
3858
3859/*
3860 * idle_balance is called by schedule() if this_cpu is about to become
3861 * idle. Attempts to pull tasks from other CPUs.
3862 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003863static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864{
3865 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003866 int pulled_task = -1;
3867 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003868 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
3870 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003871 unsigned long interval;
3872
3873 if (!(sd->flags & SD_LOAD_BALANCE))
3874 continue;
3875
3876 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003877 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003878 pulled_task = load_balance_newidle(this_cpu, this_rq,
3879 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003880
3881 interval = msecs_to_jiffies(sd->balance_interval);
3882 if (time_after(next_balance, sd->last_balance + interval))
3883 next_balance = sd->last_balance + interval;
3884 if (pulled_task)
3885 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003887 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003888 /*
3889 * We are going idle. next_balance may be set based on
3890 * a busy processor. So reset next_balance.
3891 */
3892 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894}
3895
3896/*
3897 * active_load_balance is run by migration threads. It pushes running tasks
3898 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3899 * running on each physical CPU where possible, and avoids physical /
3900 * logical imbalances.
3901 *
3902 * Called with busiest_rq locked.
3903 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003904static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905{
Nick Piggin39507452005-06-25 14:57:09 -07003906 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003907 struct sched_domain *sd;
3908 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003909
Ingo Molnar48f24c42006-07-03 00:25:40 -07003910 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003911 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003912 return;
3913
3914 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
3916 /*
Nick Piggin39507452005-06-25 14:57:09 -07003917 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003918 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003919 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 */
Nick Piggin39507452005-06-25 14:57:09 -07003921 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Nick Piggin39507452005-06-25 14:57:09 -07003923 /* move a task from busiest_rq to target_rq */
3924 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003925 update_rq_clock(busiest_rq);
3926 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
Nick Piggin39507452005-06-25 14:57:09 -07003928 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003929 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003930 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003931 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003932 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
Ingo Molnar48f24c42006-07-03 00:25:40 -07003935 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003936 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Peter Williams43010652007-08-09 11:16:46 +02003938 if (move_one_task(target_rq, target_cpu, busiest_rq,
3939 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003940 schedstat_inc(sd, alb_pushed);
3941 else
3942 schedstat_inc(sd, alb_failed);
3943 }
Nick Piggin39507452005-06-25 14:57:09 -07003944 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945}
3946
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003947#ifdef CONFIG_NO_HZ
3948static struct {
3949 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003950 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003951} nohz ____cacheline_aligned = {
3952 .load_balancer = ATOMIC_INIT(-1),
3953 .cpu_mask = CPU_MASK_NONE,
3954};
3955
Christoph Lameter7835b982006-12-10 02:20:22 -08003956/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003957 * This routine will try to nominate the ilb (idle load balancing)
3958 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3959 * load balancing on behalf of all those cpus. If all the cpus in the system
3960 * go into this tickless mode, then there will be no ilb owner (as there is
3961 * no need for one) and all the cpus will sleep till the next wakeup event
3962 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003963 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003964 * For the ilb owner, tick is not stopped. And this tick will be used
3965 * for idle load balancing. ilb owner will still be part of
3966 * nohz.cpu_mask..
3967 *
3968 * While stopping the tick, this cpu will become the ilb owner if there
3969 * is no other owner. And will be the owner till that cpu becomes busy
3970 * or if all cpus in the system stop their ticks at which point
3971 * there is no need for ilb owner.
3972 *
3973 * When the ilb owner becomes busy, it nominates another owner, during the
3974 * next busy scheduler_tick()
3975 */
3976int select_nohz_load_balancer(int stop_tick)
3977{
3978 int cpu = smp_processor_id();
3979
3980 if (stop_tick) {
3981 cpu_set(cpu, nohz.cpu_mask);
3982 cpu_rq(cpu)->in_nohz_recently = 1;
3983
3984 /*
3985 * If we are going offline and still the leader, give up!
3986 */
3987 if (cpu_is_offline(cpu) &&
3988 atomic_read(&nohz.load_balancer) == cpu) {
3989 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3990 BUG();
3991 return 0;
3992 }
3993
3994 /* time for ilb owner also to sleep */
3995 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3996 if (atomic_read(&nohz.load_balancer) == cpu)
3997 atomic_set(&nohz.load_balancer, -1);
3998 return 0;
3999 }
4000
4001 if (atomic_read(&nohz.load_balancer) == -1) {
4002 /* make me the ilb owner */
4003 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4004 return 1;
4005 } else if (atomic_read(&nohz.load_balancer) == cpu)
4006 return 1;
4007 } else {
4008 if (!cpu_isset(cpu, nohz.cpu_mask))
4009 return 0;
4010
4011 cpu_clear(cpu, nohz.cpu_mask);
4012
4013 if (atomic_read(&nohz.load_balancer) == cpu)
4014 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4015 BUG();
4016 }
4017 return 0;
4018}
4019#endif
4020
4021static DEFINE_SPINLOCK(balancing);
4022
4023/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004024 * It checks each scheduling domain to see if it is due to be balanced,
4025 * and initiates a balancing operation if so.
4026 *
4027 * Balancing parameters are set up in arch_init_sched_domains.
4028 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004029static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08004030{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004031 int balance = 1;
4032 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004033 unsigned long interval;
4034 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004035 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004036 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004037 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07004038 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004040 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 if (!(sd->flags & SD_LOAD_BALANCE))
4042 continue;
4043
4044 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004045 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 interval *= sd->busy_factor;
4047
4048 /* scale ms to jiffies */
4049 interval = msecs_to_jiffies(interval);
4050 if (unlikely(!interval))
4051 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004052 if (interval > HZ*NR_CPUS/10)
4053 interval = HZ*NR_CPUS/10;
4054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
Christoph Lameter08c183f2006-12-10 02:20:29 -08004056 if (sd->flags & SD_SERIALIZE) {
4057 if (!spin_trylock(&balancing))
4058 goto out;
4059 }
4060
Christoph Lameterc9819f42006-12-10 02:20:25 -08004061 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07004062 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004063 /*
4064 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004065 * longer idle, or one of our SMT siblings is
4066 * not idle.
4067 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004068 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004070 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08004072 if (sd->flags & SD_SERIALIZE)
4073 spin_unlock(&balancing);
4074out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004075 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004076 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004077 update_next_balance = 1;
4078 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004079
4080 /*
4081 * Stop the load balance at this level. There is another
4082 * CPU in our sched group which is doing load balancing more
4083 * actively.
4084 */
4085 if (!balance)
4086 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004088
4089 /*
4090 * next_balance will be updated only when there is a need.
4091 * When the cpu is attached to null domain for ex, it will not be
4092 * updated.
4093 */
4094 if (likely(update_next_balance))
4095 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004096}
4097
4098/*
4099 * run_rebalance_domains is triggered when needed from the scheduler tick.
4100 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4101 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4102 */
4103static void run_rebalance_domains(struct softirq_action *h)
4104{
Ingo Molnardd41f592007-07-09 18:51:59 +02004105 int this_cpu = smp_processor_id();
4106 struct rq *this_rq = cpu_rq(this_cpu);
4107 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4108 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004109
Ingo Molnardd41f592007-07-09 18:51:59 +02004110 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004111
4112#ifdef CONFIG_NO_HZ
4113 /*
4114 * If this cpu is the owner for idle load balancing, then do the
4115 * balancing on behalf of the other idle cpus whose ticks are
4116 * stopped.
4117 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004118 if (this_rq->idle_at_tick &&
4119 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004120 cpumask_t cpus = nohz.cpu_mask;
4121 struct rq *rq;
4122 int balance_cpu;
4123
Ingo Molnardd41f592007-07-09 18:51:59 +02004124 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004125 for_each_cpu_mask(balance_cpu, cpus) {
4126 /*
4127 * If this cpu gets work to do, stop the load balancing
4128 * work being done for other cpus. Next load
4129 * balancing owner will pick it up.
4130 */
4131 if (need_resched())
4132 break;
4133
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004134 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004135
4136 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004137 if (time_after(this_rq->next_balance, rq->next_balance))
4138 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004139 }
4140 }
4141#endif
4142}
4143
4144/*
4145 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4146 *
4147 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4148 * idle load balancing owner or decide to stop the periodic load balancing,
4149 * if the whole system is idle.
4150 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004151static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004152{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004153#ifdef CONFIG_NO_HZ
4154 /*
4155 * If we were in the nohz mode recently and busy at the current
4156 * scheduler tick, then check if we need to nominate new idle
4157 * load balancer.
4158 */
4159 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4160 rq->in_nohz_recently = 0;
4161
4162 if (atomic_read(&nohz.load_balancer) == cpu) {
4163 cpu_clear(cpu, nohz.cpu_mask);
4164 atomic_set(&nohz.load_balancer, -1);
4165 }
4166
4167 if (atomic_read(&nohz.load_balancer) == -1) {
4168 /*
4169 * simple selection for now: Nominate the
4170 * first cpu in the nohz list to be the next
4171 * ilb owner.
4172 *
4173 * TBD: Traverse the sched domains and nominate
4174 * the nearest cpu in the nohz.cpu_mask.
4175 */
4176 int ilb = first_cpu(nohz.cpu_mask);
4177
Mike Travis434d53b2008-04-04 18:11:04 -07004178 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004179 resched_cpu(ilb);
4180 }
4181 }
4182
4183 /*
4184 * If this cpu is idle and doing idle load balancing for all the
4185 * cpus with ticks stopped, is it time for that to stop?
4186 */
4187 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4188 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4189 resched_cpu(cpu);
4190 return;
4191 }
4192
4193 /*
4194 * If this cpu is idle and the idle load balancing is done by
4195 * someone else, then no need raise the SCHED_SOFTIRQ
4196 */
4197 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4198 cpu_isset(cpu, nohz.cpu_mask))
4199 return;
4200#endif
4201 if (time_after_eq(jiffies, rq->next_balance))
4202 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203}
Ingo Molnardd41f592007-07-09 18:51:59 +02004204
4205#else /* CONFIG_SMP */
4206
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207/*
4208 * on UP we do not need to balance between CPUs:
4209 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004210static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211{
4212}
Ingo Molnardd41f592007-07-09 18:51:59 +02004213
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214#endif
4215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216DEFINE_PER_CPU(struct kernel_stat, kstat);
4217
4218EXPORT_PER_CPU_SYMBOL(kstat);
4219
4220/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004221 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4222 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004224unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004227 u64 ns, delta_exec;
4228 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004229
Ingo Molnar41b86e92007-07-09 18:51:58 +02004230 rq = task_rq_lock(p, &flags);
4231 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004232 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004233 update_rq_clock(rq);
4234 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004235 if ((s64)delta_exec > 0)
4236 ns += delta_exec;
4237 }
4238 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 return ns;
4241}
4242
4243/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 * Account user cpu time to a process.
4245 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 * @cputime: the cpu time spent in user space since the last update
4247 */
4248void account_user_time(struct task_struct *p, cputime_t cputime)
4249{
4250 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4251 cputime64_t tmp;
4252
4253 p->utime = cputime_add(p->utime, cputime);
4254
4255 /* Add user time to cpustat. */
4256 tmp = cputime_to_cputime64(cputime);
4257 if (TASK_NICE(p) > 0)
4258 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4259 else
4260 cpustat->user = cputime64_add(cpustat->user, tmp);
4261}
4262
4263/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004264 * Account guest cpu time to a process.
4265 * @p: the process that the cpu time gets accounted to
4266 * @cputime: the cpu time spent in virtual machine since the last update
4267 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004268static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004269{
4270 cputime64_t tmp;
4271 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4272
4273 tmp = cputime_to_cputime64(cputime);
4274
4275 p->utime = cputime_add(p->utime, cputime);
4276 p->gtime = cputime_add(p->gtime, cputime);
4277
4278 cpustat->user = cputime64_add(cpustat->user, tmp);
4279 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4280}
4281
4282/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004283 * Account scaled user cpu time to a process.
4284 * @p: the process that the cpu time gets accounted to
4285 * @cputime: the cpu time spent in user space since the last update
4286 */
4287void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4288{
4289 p->utimescaled = cputime_add(p->utimescaled, cputime);
4290}
4291
4292/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 * Account system cpu time to a process.
4294 * @p: the process that the cpu time gets accounted to
4295 * @hardirq_offset: the offset to subtract from hardirq_count()
4296 * @cputime: the cpu time spent in kernel space since the last update
4297 */
4298void account_system_time(struct task_struct *p, int hardirq_offset,
4299 cputime_t cputime)
4300{
4301 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004302 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 cputime64_t tmp;
4304
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004305 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4306 account_guest_time(p, cputime);
4307 return;
4308 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 p->stime = cputime_add(p->stime, cputime);
4311
4312 /* Add system time to cpustat. */
4313 tmp = cputime_to_cputime64(cputime);
4314 if (hardirq_count() - hardirq_offset)
4315 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4316 else if (softirq_count())
4317 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004318 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004320 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4322 else
4323 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4324 /* Account for system time used */
4325 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326}
4327
4328/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004329 * Account scaled system cpu time to a process.
4330 * @p: the process that the cpu time gets accounted to
4331 * @hardirq_offset: the offset to subtract from hardirq_count()
4332 * @cputime: the cpu time spent in kernel space since the last update
4333 */
4334void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4335{
4336 p->stimescaled = cputime_add(p->stimescaled, cputime);
4337}
4338
4339/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 * Account for involuntary wait time.
4341 * @p: the process from which the cpu time has been stolen
4342 * @steal: the cpu time spent in involuntary wait
4343 */
4344void account_steal_time(struct task_struct *p, cputime_t steal)
4345{
4346 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4347 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004348 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
4350 if (p == rq->idle) {
4351 p->stime = cputime_add(p->stime, steal);
4352 if (atomic_read(&rq->nr_iowait) > 0)
4353 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4354 else
4355 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004356 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4358}
4359
Christoph Lameter7835b982006-12-10 02:20:22 -08004360/*
4361 * This function gets called by the timer code, with HZ frequency.
4362 * We call it with interrupts disabled.
4363 *
4364 * It also gets called by the fork code, when changing the parent's
4365 * timeslices.
4366 */
4367void scheduler_tick(void)
4368{
Christoph Lameter7835b982006-12-10 02:20:22 -08004369 int cpu = smp_processor_id();
4370 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004371 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004372
4373 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004374
Ingo Molnardd41f592007-07-09 18:51:59 +02004375 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004376 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004377 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004378 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004379 spin_unlock(&rq->lock);
4380
Christoph Lametere418e1c2006-12-10 02:20:23 -08004381#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004382 rq->idle_at_tick = idle_cpu(cpu);
4383 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004384#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385}
4386
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004387#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
4388 defined(CONFIG_PREEMPT_TRACER))
4389
4390static inline unsigned long get_parent_ip(unsigned long addr)
4391{
4392 if (in_lock_functions(addr)) {
4393 addr = CALLER_ADDR2;
4394 if (in_lock_functions(addr))
4395 addr = CALLER_ADDR3;
4396 }
4397 return addr;
4398}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Srinivasa Ds43627582008-02-23 15:24:04 -08004400void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004402#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 /*
4404 * Underflow?
4405 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004406 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4407 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004408#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004410#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 /*
4412 * Spinlock count overflowing soon?
4413 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004414 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4415 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004416#endif
4417 if (preempt_count() == val)
4418 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419}
4420EXPORT_SYMBOL(add_preempt_count);
4421
Srinivasa Ds43627582008-02-23 15:24:04 -08004422void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004424#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 /*
4426 * Underflow?
4427 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004428 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4429 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 /*
4431 * Is the spinlock portion underflowing?
4432 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004433 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4434 !(preempt_count() & PREEMPT_MASK)))
4435 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004436#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004437
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004438 if (preempt_count() == val)
4439 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 preempt_count() -= val;
4441}
4442EXPORT_SYMBOL(sub_preempt_count);
4443
4444#endif
4445
4446/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004447 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004449static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450{
Satyam Sharma838225b2007-10-24 18:23:50 +02004451 struct pt_regs *regs = get_irq_regs();
4452
4453 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4454 prev->comm, prev->pid, preempt_count());
4455
Ingo Molnardd41f592007-07-09 18:51:59 +02004456 debug_show_held_locks(prev);
4457 if (irqs_disabled())
4458 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004459
4460 if (regs)
4461 show_regs(regs);
4462 else
4463 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004464}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
Ingo Molnardd41f592007-07-09 18:51:59 +02004466/*
4467 * Various schedule()-time debugging checks and statistics:
4468 */
4469static inline void schedule_debug(struct task_struct *prev)
4470{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004472 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 * schedule() atomically, we ignore that path for now.
4474 * Otherwise, whine if we are scheduling when we should not be.
4475 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004476 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4477 __schedule_bug(prev);
4478
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4480
Ingo Molnar2d723762007-10-15 17:00:12 +02004481 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004482#ifdef CONFIG_SCHEDSTATS
4483 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004484 schedstat_inc(this_rq(), bkl_count);
4485 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004486 }
4487#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004488}
4489
4490/*
4491 * Pick up the highest-prio task:
4492 */
4493static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004494pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004495{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004496 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004497 struct task_struct *p;
4498
4499 /*
4500 * Optimization: we know that if all tasks are in
4501 * the fair class we can call that function directly:
4502 */
4503 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004504 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004505 if (likely(p))
4506 return p;
4507 }
4508
4509 class = sched_class_highest;
4510 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004511 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004512 if (p)
4513 return p;
4514 /*
4515 * Will never be NULL as the idle class always
4516 * returns a non-NULL p:
4517 */
4518 class = class->next;
4519 }
4520}
4521
4522/*
4523 * schedule() is the main scheduler function.
4524 */
4525asmlinkage void __sched schedule(void)
4526{
4527 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004528 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004529 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004530 int cpu;
4531
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532need_resched:
4533 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004534 cpu = smp_processor_id();
4535 rq = cpu_rq(cpu);
4536 rcu_qsctr_inc(cpu);
4537 prev = rq->curr;
4538 switch_count = &prev->nivcsw;
4539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 release_kernel_lock(prev);
4541need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Ingo Molnardd41f592007-07-09 18:51:59 +02004543 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004545 hrtick_clear(rq);
4546
Ingo Molnar1e819952007-10-15 17:00:13 +02004547 /*
4548 * Do the rq-clock update outside the rq lock:
4549 */
4550 local_irq_disable();
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004551 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004552 spin_lock(&rq->lock);
4553 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Ingo Molnardd41f592007-07-09 18:51:59 +02004555 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4556 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004557 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004558 prev->state = TASK_RUNNING;
4559 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004560 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004561 }
4562 switch_count = &prev->nvcsw;
4563 }
4564
Steven Rostedt9a897c52008-01-25 21:08:22 +01004565#ifdef CONFIG_SMP
4566 if (prev->sched_class->pre_schedule)
4567 prev->sched_class->pre_schedule(rq, prev);
4568#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004569
Ingo Molnardd41f592007-07-09 18:51:59 +02004570 if (unlikely(!rq->nr_running))
4571 idle_balance(cpu, rq);
4572
Ingo Molnar31ee5292007-08-09 11:16:49 +02004573 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004574 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01004577 sched_info_switch(prev, next);
4578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 rq->nr_switches++;
4580 rq->curr = next;
4581 ++*switch_count;
4582
Ingo Molnardd41f592007-07-09 18:51:59 +02004583 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004584 /*
4585 * the context switch might have flipped the stack from under
4586 * us, hence refresh the local variables.
4587 */
4588 cpu = smp_processor_id();
4589 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 } else
4591 spin_unlock_irq(&rq->lock);
4592
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004593 hrtick_set(rq);
4594
4595 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 preempt_enable_no_resched();
4599 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4600 goto need_resched;
4601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602EXPORT_SYMBOL(schedule);
4603
4604#ifdef CONFIG_PREEMPT
4605/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004606 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004607 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 * occur there and call schedule directly.
4609 */
4610asmlinkage void __sched preempt_schedule(void)
4611{
4612 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 /*
4615 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004616 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004618 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 return;
4620
Andi Kleen3a5c3592007-10-15 17:00:14 +02004621 do {
4622 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004623 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004624 sub_preempt_count(PREEMPT_ACTIVE);
4625
4626 /*
4627 * Check again in case we missed a preemption opportunity
4628 * between schedule and now.
4629 */
4630 barrier();
4631 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633EXPORT_SYMBOL(preempt_schedule);
4634
4635/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004636 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 * off of irq context.
4638 * Note, that this is called and return with irqs disabled. This will
4639 * protect us against recursive calling from irq.
4640 */
4641asmlinkage void __sched preempt_schedule_irq(void)
4642{
4643 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004644
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004645 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 BUG_ON(ti->preempt_count || !irqs_disabled());
4647
Andi Kleen3a5c3592007-10-15 17:00:14 +02004648 do {
4649 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004650 local_irq_enable();
4651 schedule();
4652 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004653 sub_preempt_count(PREEMPT_ACTIVE);
4654
4655 /*
4656 * Check again in case we missed a preemption opportunity
4657 * between schedule and now.
4658 */
4659 barrier();
4660 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661}
4662
4663#endif /* CONFIG_PREEMPT */
4664
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004665int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4666 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004668 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670EXPORT_SYMBOL(default_wake_function);
4671
4672/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004673 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4674 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 * number) then we wake all the non-exclusive tasks and one exclusive task.
4676 *
4677 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004678 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4680 */
4681static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4682 int nr_exclusive, int sync, void *key)
4683{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004684 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004686 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004687 unsigned flags = curr->flags;
4688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004690 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 break;
4692 }
4693}
4694
4695/**
4696 * __wake_up - wake up threads blocked on a waitqueue.
4697 * @q: the waitqueue
4698 * @mode: which threads
4699 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004700 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004702void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004703 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704{
4705 unsigned long flags;
4706
4707 spin_lock_irqsave(&q->lock, flags);
4708 __wake_up_common(q, mode, nr_exclusive, 0, key);
4709 spin_unlock_irqrestore(&q->lock, flags);
4710}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711EXPORT_SYMBOL(__wake_up);
4712
4713/*
4714 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4715 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004716void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717{
4718 __wake_up_common(q, mode, 1, 0, NULL);
4719}
4720
4721/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004722 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 * @q: the waitqueue
4724 * @mode: which threads
4725 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4726 *
4727 * The sync wakeup differs that the waker knows that it will schedule
4728 * away soon, so while the target thread will be woken up, it will not
4729 * be migrated to another CPU - ie. the two threads are 'synchronized'
4730 * with each other. This can prevent needless bouncing between CPUs.
4731 *
4732 * On UP it can prevent extra preemption.
4733 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004734void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004735__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736{
4737 unsigned long flags;
4738 int sync = 1;
4739
4740 if (unlikely(!q))
4741 return;
4742
4743 if (unlikely(!nr_exclusive))
4744 sync = 0;
4745
4746 spin_lock_irqsave(&q->lock, flags);
4747 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4748 spin_unlock_irqrestore(&q->lock, flags);
4749}
4750EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4751
Ingo Molnarb15136e2007-10-24 18:23:48 +02004752void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753{
4754 unsigned long flags;
4755
4756 spin_lock_irqsave(&x->wait.lock, flags);
4757 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004758 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 spin_unlock_irqrestore(&x->wait.lock, flags);
4760}
4761EXPORT_SYMBOL(complete);
4762
Ingo Molnarb15136e2007-10-24 18:23:48 +02004763void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764{
4765 unsigned long flags;
4766
4767 spin_lock_irqsave(&x->wait.lock, flags);
4768 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004769 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 spin_unlock_irqrestore(&x->wait.lock, flags);
4771}
4772EXPORT_SYMBOL(complete_all);
4773
Andi Kleen8cbbe862007-10-15 17:00:14 +02004774static inline long __sched
4775do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 if (!x->done) {
4778 DECLARE_WAITQUEUE(wait, current);
4779
4780 wait.flags |= WQ_FLAG_EXCLUSIVE;
4781 __add_wait_queue_tail(&x->wait, &wait);
4782 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004783 if ((state == TASK_INTERRUPTIBLE &&
4784 signal_pending(current)) ||
4785 (state == TASK_KILLABLE &&
4786 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004787 __remove_wait_queue(&x->wait, &wait);
4788 return -ERESTARTSYS;
4789 }
4790 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004792 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004794 if (!timeout) {
4795 __remove_wait_queue(&x->wait, &wait);
4796 return timeout;
4797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 } while (!x->done);
4799 __remove_wait_queue(&x->wait, &wait);
4800 }
4801 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004802 return timeout;
4803}
4804
4805static long __sched
4806wait_for_common(struct completion *x, long timeout, int state)
4807{
4808 might_sleep();
4809
4810 spin_lock_irq(&x->wait.lock);
4811 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004813 return timeout;
4814}
4815
Ingo Molnarb15136e2007-10-24 18:23:48 +02004816void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004817{
4818 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819}
4820EXPORT_SYMBOL(wait_for_completion);
4821
Ingo Molnarb15136e2007-10-24 18:23:48 +02004822unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4824{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004825 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826}
4827EXPORT_SYMBOL(wait_for_completion_timeout);
4828
Andi Kleen8cbbe862007-10-15 17:00:14 +02004829int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830{
Andi Kleen51e97992007-10-18 21:32:55 +02004831 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4832 if (t == -ERESTARTSYS)
4833 return t;
4834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835}
4836EXPORT_SYMBOL(wait_for_completion_interruptible);
4837
Ingo Molnarb15136e2007-10-24 18:23:48 +02004838unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839wait_for_completion_interruptible_timeout(struct completion *x,
4840 unsigned long timeout)
4841{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004842 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843}
4844EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4845
Matthew Wilcox009e5772007-12-06 12:29:54 -05004846int __sched wait_for_completion_killable(struct completion *x)
4847{
4848 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4849 if (t == -ERESTARTSYS)
4850 return t;
4851 return 0;
4852}
4853EXPORT_SYMBOL(wait_for_completion_killable);
4854
Andi Kleen8cbbe862007-10-15 17:00:14 +02004855static long __sched
4856sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004857{
4858 unsigned long flags;
4859 wait_queue_t wait;
4860
4861 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
Andi Kleen8cbbe862007-10-15 17:00:14 +02004863 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
Andi Kleen8cbbe862007-10-15 17:00:14 +02004865 spin_lock_irqsave(&q->lock, flags);
4866 __add_wait_queue(q, &wait);
4867 spin_unlock(&q->lock);
4868 timeout = schedule_timeout(timeout);
4869 spin_lock_irq(&q->lock);
4870 __remove_wait_queue(q, &wait);
4871 spin_unlock_irqrestore(&q->lock, flags);
4872
4873 return timeout;
4874}
4875
4876void __sched interruptible_sleep_on(wait_queue_head_t *q)
4877{
4878 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880EXPORT_SYMBOL(interruptible_sleep_on);
4881
Ingo Molnar0fec1712007-07-09 18:52:01 +02004882long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004883interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004885 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4888
Ingo Molnar0fec1712007-07-09 18:52:01 +02004889void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004891 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893EXPORT_SYMBOL(sleep_on);
4894
Ingo Molnar0fec1712007-07-09 18:52:01 +02004895long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004897 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899EXPORT_SYMBOL(sleep_on_timeout);
4900
Ingo Molnarb29739f2006-06-27 02:54:51 -07004901#ifdef CONFIG_RT_MUTEXES
4902
4903/*
4904 * rt_mutex_setprio - set the current priority of a task
4905 * @p: task
4906 * @prio: prio value (kernel-internal form)
4907 *
4908 * This function changes the 'effective' priority of a task. It does
4909 * not touch ->normal_prio like __setscheduler().
4910 *
4911 * Used by the rt_mutex code to implement priority inheritance logic.
4912 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004913void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004914{
4915 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004916 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004917 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004918 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004919
4920 BUG_ON(prio < 0 || prio > MAX_PRIO);
4921
4922 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004923 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004924
Andrew Mortond5f9f942007-05-08 20:27:06 -07004925 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004926 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004927 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004928 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004929 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004930 if (running)
4931 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004932
4933 if (rt_prio(prio))
4934 p->sched_class = &rt_sched_class;
4935 else
4936 p->sched_class = &fair_sched_class;
4937
Ingo Molnarb29739f2006-06-27 02:54:51 -07004938 p->prio = prio;
4939
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004940 if (running)
4941 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004942 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004943 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004944
4945 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004946 }
4947 task_rq_unlock(rq, &flags);
4948}
4949
4950#endif
4951
Ingo Molnar36c8b582006-07-03 00:25:41 -07004952void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953{
Ingo Molnardd41f592007-07-09 18:51:59 +02004954 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004956 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
4958 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4959 return;
4960 /*
4961 * We have to be careful, if called from sys_setpriority(),
4962 * the task might be in the middle of scheduling on another CPU.
4963 */
4964 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004965 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 /*
4967 * The RT priorities are set via sched_setscheduler(), but we still
4968 * allow the 'normal' nice value to be set - but as expected
4969 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004970 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004972 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 p->static_prio = NICE_TO_PRIO(nice);
4974 goto out_unlock;
4975 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004976 on_rq = p->se.on_rq;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02004977 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004978 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004981 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004982 old_prio = p->prio;
4983 p->prio = effective_prio(p);
4984 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Ingo Molnardd41f592007-07-09 18:51:59 +02004986 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004987 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004989 * If the task increased its priority or is running and
4990 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004992 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 resched_task(rq->curr);
4994 }
4995out_unlock:
4996 task_rq_unlock(rq, &flags);
4997}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998EXPORT_SYMBOL(set_user_nice);
4999
Matt Mackalle43379f2005-05-01 08:59:00 -07005000/*
5001 * can_nice - check if a task can reduce its nice value
5002 * @p: task
5003 * @nice: nice value
5004 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005005int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005006{
Matt Mackall024f4742005-08-18 11:24:19 -07005007 /* convert nice value [19,-20] to rlimit style value [1,40] */
5008 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005009
Matt Mackalle43379f2005-05-01 08:59:00 -07005010 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5011 capable(CAP_SYS_NICE));
5012}
5013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014#ifdef __ARCH_WANT_SYS_NICE
5015
5016/*
5017 * sys_nice - change the priority of the current process.
5018 * @increment: priority increment
5019 *
5020 * sys_setpriority is a more generic, but much slower function that
5021 * does similar things.
5022 */
5023asmlinkage long sys_nice(int increment)
5024{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005025 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
5027 /*
5028 * Setpriority might change our priority at the same moment.
5029 * We don't have to worry. Conceptually one call occurs first
5030 * and we have a single winner.
5031 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005032 if (increment < -40)
5033 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 if (increment > 40)
5035 increment = 40;
5036
5037 nice = PRIO_TO_NICE(current->static_prio) + increment;
5038 if (nice < -20)
5039 nice = -20;
5040 if (nice > 19)
5041 nice = 19;
5042
Matt Mackalle43379f2005-05-01 08:59:00 -07005043 if (increment < 0 && !can_nice(current, nice))
5044 return -EPERM;
5045
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 retval = security_task_setnice(current, nice);
5047 if (retval)
5048 return retval;
5049
5050 set_user_nice(current, nice);
5051 return 0;
5052}
5053
5054#endif
5055
5056/**
5057 * task_prio - return the priority value of a given task.
5058 * @p: the task in question.
5059 *
5060 * This is the priority value as seen by users in /proc.
5061 * RT tasks are offset by -200. Normal tasks are centered
5062 * around 0, value goes from -16 to +15.
5063 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005064int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065{
5066 return p->prio - MAX_RT_PRIO;
5067}
5068
5069/**
5070 * task_nice - return the nice value of a given task.
5071 * @p: the task in question.
5072 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005073int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074{
5075 return TASK_NICE(p);
5076}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005077EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
5079/**
5080 * idle_cpu - is a given cpu idle currently?
5081 * @cpu: the processor in question.
5082 */
5083int idle_cpu(int cpu)
5084{
5085 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5086}
5087
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088/**
5089 * idle_task - return the idle task for a given cpu.
5090 * @cpu: the processor in question.
5091 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005092struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093{
5094 return cpu_rq(cpu)->idle;
5095}
5096
5097/**
5098 * find_process_by_pid - find a process with a matching PID value.
5099 * @pid: the pid in question.
5100 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005101static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005103 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104}
5105
5106/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005107static void
5108__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109{
Ingo Molnardd41f592007-07-09 18:51:59 +02005110 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005111
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005113 switch (p->policy) {
5114 case SCHED_NORMAL:
5115 case SCHED_BATCH:
5116 case SCHED_IDLE:
5117 p->sched_class = &fair_sched_class;
5118 break;
5119 case SCHED_FIFO:
5120 case SCHED_RR:
5121 p->sched_class = &rt_sched_class;
5122 break;
5123 }
5124
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005126 p->normal_prio = normal_prio(p);
5127 /* we are holding p->pi_lock already */
5128 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005129 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130}
5131
5132/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005133 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 * @p: the task in question.
5135 * @policy: new policy.
5136 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005137 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005138 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005140int sched_setscheduler(struct task_struct *p, int policy,
5141 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005143 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005145 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005146 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Steven Rostedt66e53932006-06-27 02:54:44 -07005148 /* may grab non-irq protected spin_locks */
5149 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150recheck:
5151 /* double check policy once rq lock held */
5152 if (policy < 0)
5153 policy = oldpolicy = p->policy;
5154 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005155 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5156 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005157 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 /*
5159 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005160 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5161 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 */
5163 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005164 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005165 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005167 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 return -EINVAL;
5169
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005170 /*
5171 * Allow unprivileged RT tasks to decrease priority:
5172 */
5173 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005174 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005175 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005176
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005177 if (!lock_task_sighand(p, &flags))
5178 return -ESRCH;
5179 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5180 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005181
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005182 /* can't set/change the rt policy */
5183 if (policy != p->policy && !rlim_rtprio)
5184 return -EPERM;
5185
5186 /* can't increase priority */
5187 if (param->sched_priority > p->rt_priority &&
5188 param->sched_priority > rlim_rtprio)
5189 return -EPERM;
5190 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005191 /*
5192 * Like positive nice levels, dont allow tasks to
5193 * move out of SCHED_IDLE either:
5194 */
5195 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5196 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005197
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005198 /* can't change other user's priorities */
5199 if ((current->euid != p->euid) &&
5200 (current->euid != p->uid))
5201 return -EPERM;
5202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005204#ifdef CONFIG_RT_GROUP_SCHED
5205 /*
5206 * Do not allow realtime tasks into groups that have no runtime
5207 * assigned.
5208 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02005209 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005210 return -EPERM;
5211#endif
5212
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 retval = security_task_setscheduler(p, policy, param);
5214 if (retval)
5215 return retval;
5216 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005217 * make sure no PI-waiters arrive (or leave) while we are
5218 * changing the priority of the task:
5219 */
5220 spin_lock_irqsave(&p->pi_lock, flags);
5221 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 * To be able to change p->policy safely, the apropriate
5223 * runqueue lock must be held.
5224 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005225 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 /* recheck policy now with rq lock held */
5227 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5228 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005229 __task_rq_unlock(rq);
5230 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 goto recheck;
5232 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005233 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005234 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005235 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005236 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005237 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005238 if (running)
5239 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005240
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005242 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005243
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005244 if (running)
5245 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005246 if (on_rq) {
5247 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005248
5249 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005251 __task_rq_unlock(rq);
5252 spin_unlock_irqrestore(&p->pi_lock, flags);
5253
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005254 rt_mutex_adjust_pi(p);
5255
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 return 0;
5257}
5258EXPORT_SYMBOL_GPL(sched_setscheduler);
5259
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005260static int
5261do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 struct sched_param lparam;
5264 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005265 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
5267 if (!param || pid < 0)
5268 return -EINVAL;
5269 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5270 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005271
5272 rcu_read_lock();
5273 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005275 if (p != NULL)
5276 retval = sched_setscheduler(p, policy, &lparam);
5277 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005278
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 return retval;
5280}
5281
5282/**
5283 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5284 * @pid: the pid in question.
5285 * @policy: new policy.
5286 * @param: structure containing the new RT priority.
5287 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005288asmlinkage long
5289sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290{
Jason Baronc21761f2006-01-18 17:43:03 -08005291 /* negative values for policy are not valid */
5292 if (policy < 0)
5293 return -EINVAL;
5294
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 return do_sched_setscheduler(pid, policy, param);
5296}
5297
5298/**
5299 * sys_sched_setparam - set/change the RT priority of a thread
5300 * @pid: the pid in question.
5301 * @param: structure containing the new RT priority.
5302 */
5303asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5304{
5305 return do_sched_setscheduler(pid, -1, param);
5306}
5307
5308/**
5309 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5310 * @pid: the pid in question.
5311 */
5312asmlinkage long sys_sched_getscheduler(pid_t pid)
5313{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005314 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005315 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
5317 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005318 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
5320 retval = -ESRCH;
5321 read_lock(&tasklist_lock);
5322 p = find_process_by_pid(pid);
5323 if (p) {
5324 retval = security_task_getscheduler(p);
5325 if (!retval)
5326 retval = p->policy;
5327 }
5328 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 return retval;
5330}
5331
5332/**
5333 * sys_sched_getscheduler - get the RT priority of a thread
5334 * @pid: the pid in question.
5335 * @param: structure containing the RT priority.
5336 */
5337asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5338{
5339 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005340 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005341 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342
5343 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
5346 read_lock(&tasklist_lock);
5347 p = find_process_by_pid(pid);
5348 retval = -ESRCH;
5349 if (!p)
5350 goto out_unlock;
5351
5352 retval = security_task_getscheduler(p);
5353 if (retval)
5354 goto out_unlock;
5355
5356 lp.sched_priority = p->rt_priority;
5357 read_unlock(&tasklist_lock);
5358
5359 /*
5360 * This one might sleep, we cannot do it with a spinlock held ...
5361 */
5362 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5363
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 return retval;
5365
5366out_unlock:
5367 read_unlock(&tasklist_lock);
5368 return retval;
5369}
5370
Mike Travisb53e9212008-04-04 18:11:08 -07005371long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005374 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005375 struct task_struct *p;
5376 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005378 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 read_lock(&tasklist_lock);
5380
5381 p = find_process_by_pid(pid);
5382 if (!p) {
5383 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005384 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 return -ESRCH;
5386 }
5387
5388 /*
5389 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005390 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 * usage count and then drop tasklist_lock.
5392 */
5393 get_task_struct(p);
5394 read_unlock(&tasklist_lock);
5395
5396 retval = -EPERM;
5397 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5398 !capable(CAP_SYS_NICE))
5399 goto out_unlock;
5400
David Quigleye7834f82006-06-23 02:03:59 -07005401 retval = security_task_setscheduler(p, 0, NULL);
5402 if (retval)
5403 goto out_unlock;
5404
Mike Travisf9a86fc2008-04-04 18:11:07 -07005405 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005407 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005408 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
Paul Menage8707d8b2007-10-18 23:40:22 -07005410 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005411 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005412 if (!cpus_subset(new_mask, cpus_allowed)) {
5413 /*
5414 * We must have raced with a concurrent cpuset
5415 * update. Just reset the cpus_allowed to the
5416 * cpuset's cpus_allowed
5417 */
5418 new_mask = cpus_allowed;
5419 goto again;
5420 }
5421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422out_unlock:
5423 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005424 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 return retval;
5426}
5427
5428static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5429 cpumask_t *new_mask)
5430{
5431 if (len < sizeof(cpumask_t)) {
5432 memset(new_mask, 0, sizeof(cpumask_t));
5433 } else if (len > sizeof(cpumask_t)) {
5434 len = sizeof(cpumask_t);
5435 }
5436 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5437}
5438
5439/**
5440 * sys_sched_setaffinity - set the cpu affinity of a process
5441 * @pid: pid of the process
5442 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5443 * @user_mask_ptr: user-space pointer to the new cpu mask
5444 */
5445asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5446 unsigned long __user *user_mask_ptr)
5447{
5448 cpumask_t new_mask;
5449 int retval;
5450
5451 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5452 if (retval)
5453 return retval;
5454
Mike Travisb53e9212008-04-04 18:11:08 -07005455 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456}
5457
5458/*
5459 * Represents all cpu's present in the system
5460 * In systems capable of hotplug, this map could dynamically grow
5461 * as new cpu's are detected in the system via any platform specific
5462 * method, such as ACPI for e.g.
5463 */
5464
Andi Kleen4cef0c62006-01-11 22:44:57 +01005465cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466EXPORT_SYMBOL(cpu_present_map);
5467
5468#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005469cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005470EXPORT_SYMBOL(cpu_online_map);
5471
Andi Kleen4cef0c62006-01-11 22:44:57 +01005472cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005473EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474#endif
5475
5476long sched_getaffinity(pid_t pid, cpumask_t *mask)
5477{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005478 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005481 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 read_lock(&tasklist_lock);
5483
5484 retval = -ESRCH;
5485 p = find_process_by_pid(pid);
5486 if (!p)
5487 goto out_unlock;
5488
David Quigleye7834f82006-06-23 02:03:59 -07005489 retval = security_task_getscheduler(p);
5490 if (retval)
5491 goto out_unlock;
5492
Jack Steiner2f7016d2006-02-01 03:05:18 -08005493 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
5495out_unlock:
5496 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005497 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498
Ulrich Drepper9531b622007-08-09 11:16:46 +02005499 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500}
5501
5502/**
5503 * sys_sched_getaffinity - get the cpu affinity of a process
5504 * @pid: pid of the process
5505 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5506 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5507 */
5508asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5509 unsigned long __user *user_mask_ptr)
5510{
5511 int ret;
5512 cpumask_t mask;
5513
5514 if (len < sizeof(cpumask_t))
5515 return -EINVAL;
5516
5517 ret = sched_getaffinity(pid, &mask);
5518 if (ret < 0)
5519 return ret;
5520
5521 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5522 return -EFAULT;
5523
5524 return sizeof(cpumask_t);
5525}
5526
5527/**
5528 * sys_sched_yield - yield the current processor to other threads.
5529 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005530 * This function yields the current CPU to other tasks. If there are no
5531 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 */
5533asmlinkage long sys_sched_yield(void)
5534{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005535 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
Ingo Molnar2d723762007-10-15 17:00:12 +02005537 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005538 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
5540 /*
5541 * Since we are going to call schedule() anyway, there's
5542 * no need to preempt or enable interrupts:
5543 */
5544 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005545 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 _raw_spin_unlock(&rq->lock);
5547 preempt_enable_no_resched();
5548
5549 schedule();
5550
5551 return 0;
5552}
5553
Andrew Mortone7b38402006-06-30 01:56:00 -07005554static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005556#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5557 __might_sleep(__FILE__, __LINE__);
5558#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005559 /*
5560 * The BKS might be reacquired before we have dropped
5561 * PREEMPT_ACTIVE, which could trigger a second
5562 * cond_resched() call.
5563 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 do {
5565 add_preempt_count(PREEMPT_ACTIVE);
5566 schedule();
5567 sub_preempt_count(PREEMPT_ACTIVE);
5568 } while (need_resched());
5569}
5570
Herbert Xu02b67cc32008-01-25 21:08:28 +01005571int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572{
Ingo Molnar94142322006-12-29 16:48:13 -08005573 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5574 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 __cond_resched();
5576 return 1;
5577 }
5578 return 0;
5579}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005580EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
5582/*
5583 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5584 * call schedule, and on return reacquire the lock.
5585 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005586 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 * operations here to prevent schedule() from being called twice (once via
5588 * spin_unlock(), once by hand).
5589 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005590int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591{
Nick Piggin95c354f2008-01-30 13:31:20 +01005592 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005593 int ret = 0;
5594
Nick Piggin95c354f2008-01-30 13:31:20 +01005595 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005597 if (resched && need_resched())
5598 __cond_resched();
5599 else
5600 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005601 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005604 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606EXPORT_SYMBOL(cond_resched_lock);
5607
5608int __sched cond_resched_softirq(void)
5609{
5610 BUG_ON(!in_softirq());
5611
Ingo Molnar94142322006-12-29 16:48:13 -08005612 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005613 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 __cond_resched();
5615 local_bh_disable();
5616 return 1;
5617 }
5618 return 0;
5619}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620EXPORT_SYMBOL(cond_resched_softirq);
5621
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622/**
5623 * yield - yield the current processor to other threads.
5624 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005625 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 * thread runnable and calls sys_sched_yield().
5627 */
5628void __sched yield(void)
5629{
5630 set_current_state(TASK_RUNNING);
5631 sys_sched_yield();
5632}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633EXPORT_SYMBOL(yield);
5634
5635/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005636 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 * that process accounting knows that this is a task in IO wait state.
5638 *
5639 * But don't do that if it is a deliberate, throttling IO wait (this task
5640 * has set its backing_dev_info: the queue against which it should throttle)
5641 */
5642void __sched io_schedule(void)
5643{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005644 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005646 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 atomic_inc(&rq->nr_iowait);
5648 schedule();
5649 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005650 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652EXPORT_SYMBOL(io_schedule);
5653
5654long __sched io_schedule_timeout(long timeout)
5655{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005656 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 long ret;
5658
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005659 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 atomic_inc(&rq->nr_iowait);
5661 ret = schedule_timeout(timeout);
5662 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005663 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 return ret;
5665}
5666
5667/**
5668 * sys_sched_get_priority_max - return maximum RT priority.
5669 * @policy: scheduling class.
5670 *
5671 * this syscall returns the maximum rt_priority that can be used
5672 * by a given scheduling class.
5673 */
5674asmlinkage long sys_sched_get_priority_max(int policy)
5675{
5676 int ret = -EINVAL;
5677
5678 switch (policy) {
5679 case SCHED_FIFO:
5680 case SCHED_RR:
5681 ret = MAX_USER_RT_PRIO-1;
5682 break;
5683 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005684 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005685 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 ret = 0;
5687 break;
5688 }
5689 return ret;
5690}
5691
5692/**
5693 * sys_sched_get_priority_min - return minimum RT priority.
5694 * @policy: scheduling class.
5695 *
5696 * this syscall returns the minimum rt_priority that can be used
5697 * by a given scheduling class.
5698 */
5699asmlinkage long sys_sched_get_priority_min(int policy)
5700{
5701 int ret = -EINVAL;
5702
5703 switch (policy) {
5704 case SCHED_FIFO:
5705 case SCHED_RR:
5706 ret = 1;
5707 break;
5708 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005709 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005710 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 ret = 0;
5712 }
5713 return ret;
5714}
5715
5716/**
5717 * sys_sched_rr_get_interval - return the default timeslice of a process.
5718 * @pid: pid of the process.
5719 * @interval: userspace pointer to the timeslice value.
5720 *
5721 * this syscall writes the default timeslice value of a given process
5722 * into the user-space timespec buffer. A value of '0' means infinity.
5723 */
5724asmlinkage
5725long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5726{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005727 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005728 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005729 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
5732 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005733 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
5735 retval = -ESRCH;
5736 read_lock(&tasklist_lock);
5737 p = find_process_by_pid(pid);
5738 if (!p)
5739 goto out_unlock;
5740
5741 retval = security_task_getscheduler(p);
5742 if (retval)
5743 goto out_unlock;
5744
Ingo Molnar77034932007-12-04 17:04:39 +01005745 /*
5746 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5747 * tasks that are on an otherwise idle runqueue:
5748 */
5749 time_slice = 0;
5750 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005751 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005752 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005753 struct sched_entity *se = &p->se;
5754 unsigned long flags;
5755 struct rq *rq;
5756
5757 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005758 if (rq->cfs.load.weight)
5759 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005760 task_rq_unlock(rq, &flags);
5761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005763 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005766
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767out_unlock:
5768 read_unlock(&tasklist_lock);
5769 return retval;
5770}
5771
Steven Rostedt7c731e02008-05-12 21:20:41 +02005772static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005773
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005774void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005777 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005780 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005781 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005782#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005784 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005786 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787#else
5788 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005789 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005791 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792#endif
5793#ifdef CONFIG_DEBUG_STACK_USAGE
5794 {
Al Viro10ebffd2005-11-13 16:06:56 -08005795 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 while (!*n)
5797 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005798 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 }
5800#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005801 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005802 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005804 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805}
5806
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005807void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005809 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
Ingo Molnar4bd77322007-07-11 21:21:47 +02005811#if BITS_PER_LONG == 32
5812 printk(KERN_INFO
5813 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005815 printk(KERN_INFO
5816 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817#endif
5818 read_lock(&tasklist_lock);
5819 do_each_thread(g, p) {
5820 /*
5821 * reset the NMI-timeout, listing all files on a slow
5822 * console might take alot of time:
5823 */
5824 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005825 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005826 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 } while_each_thread(g, p);
5828
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005829 touch_all_softlockup_watchdogs();
5830
Ingo Molnardd41f592007-07-09 18:51:59 +02005831#ifdef CONFIG_SCHED_DEBUG
5832 sysrq_sched_debug_show();
5833#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005835 /*
5836 * Only show locks if all tasks are dumped:
5837 */
5838 if (state_filter == -1)
5839 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840}
5841
Ingo Molnar1df21052007-07-09 18:51:58 +02005842void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5843{
Ingo Molnardd41f592007-07-09 18:51:59 +02005844 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005845}
5846
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005847/**
5848 * init_idle - set up an idle thread for a given CPU
5849 * @idle: task in question
5850 * @cpu: cpu the idle task belongs to
5851 *
5852 * NOTE: this function does not set the idle thread's NEED_RESCHED
5853 * flag, to make booting more robust.
5854 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005855void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005857 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 unsigned long flags;
5859
Ingo Molnardd41f592007-07-09 18:51:59 +02005860 __sched_fork(idle);
5861 idle->se.exec_start = sched_clock();
5862
Ingo Molnarb29739f2006-06-27 02:54:51 -07005863 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005865 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
5867 spin_lock_irqsave(&rq->lock, flags);
5868 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005869#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5870 idle->oncpu = 1;
5871#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 spin_unlock_irqrestore(&rq->lock, flags);
5873
5874 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005875#if defined(CONFIG_PREEMPT)
5876 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
5877#else
Al Viroa1261f542005-11-13 16:06:55 -08005878 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005879#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005880 /*
5881 * The idle tasks have their own, simple scheduling class:
5882 */
5883 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884}
5885
5886/*
5887 * In a system that switches off the HZ timer nohz_cpu_mask
5888 * indicates which cpus entered this state. This is used
5889 * in the rcu update to wait only for active cpus. For system
5890 * which do not switch off the HZ timer nohz_cpu_mask should
5891 * always be CPU_MASK_NONE.
5892 */
5893cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5894
Ingo Molnar19978ca2007-11-09 22:39:38 +01005895/*
5896 * Increase the granularity value when there are more CPUs,
5897 * because with more CPUs the 'effective latency' as visible
5898 * to users decreases. But the relationship is not linear,
5899 * so pick a second-best guess by going with the log2 of the
5900 * number of CPUs.
5901 *
5902 * This idea comes from the SD scheduler of Con Kolivas:
5903 */
5904static inline void sched_init_granularity(void)
5905{
5906 unsigned int factor = 1 + ilog2(num_online_cpus());
5907 const unsigned long limit = 200000000;
5908
5909 sysctl_sched_min_granularity *= factor;
5910 if (sysctl_sched_min_granularity > limit)
5911 sysctl_sched_min_granularity = limit;
5912
5913 sysctl_sched_latency *= factor;
5914 if (sysctl_sched_latency > limit)
5915 sysctl_sched_latency = limit;
5916
5917 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005918}
5919
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920#ifdef CONFIG_SMP
5921/*
5922 * This is how migration works:
5923 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005924 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 * runqueue and wake up that CPU's migration thread.
5926 * 2) we down() the locked semaphore => thread blocks.
5927 * 3) migration thread wakes up (implicitly it forces the migrated
5928 * thread off the CPU)
5929 * 4) it gets the migration request and checks whether the migrated
5930 * task is still in the wrong runqueue.
5931 * 5) if it's in the wrong runqueue then the migration thread removes
5932 * it and puts it into the right queue.
5933 * 6) migration thread up()s the semaphore.
5934 * 7) we wake up and the migration is done.
5935 */
5936
5937/*
5938 * Change a given task's CPU affinity. Migrate the thread to a
5939 * proper CPU and schedule it away if the CPU it's executing on
5940 * is removed from the allowed bitmask.
5941 *
5942 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005943 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 * call is not atomic; no spinlocks may be held.
5945 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005946int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005948 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005950 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005951 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
5953 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005954 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 ret = -EINVAL;
5956 goto out;
5957 }
5958
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005959 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005960 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005961 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005962 p->cpus_allowed = *new_mask;
5963 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005964 }
5965
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005967 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 goto out;
5969
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005970 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 /* Need help from migration thread: drop lock and wait. */
5972 task_rq_unlock(rq, &flags);
5973 wake_up_process(rq->migration_thread);
5974 wait_for_completion(&req.done);
5975 tlb_migrate_finish(p->mm);
5976 return 0;
5977 }
5978out:
5979 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005980
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 return ret;
5982}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005983EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
5985/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005986 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 * this because either it can't run here any more (set_cpus_allowed()
5988 * away from this CPU, or CPU going down), or because we're
5989 * attempting to rebalance this task on exec (sched_exec).
5990 *
5991 * So we race with normal scheduler movements, but that's OK, as long
5992 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005993 *
5994 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005996static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005998 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005999 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000
6001 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006002 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003
6004 rq_src = cpu_rq(src_cpu);
6005 rq_dest = cpu_rq(dest_cpu);
6006
6007 double_rq_lock(rq_src, rq_dest);
6008 /* Already moved. */
6009 if (task_cpu(p) != src_cpu)
6010 goto out;
6011 /* Affinity changed (again). */
6012 if (!cpu_isset(dest_cpu, p->cpus_allowed))
6013 goto out;
6014
Ingo Molnardd41f592007-07-09 18:51:59 +02006015 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006016 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006017 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006018
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006020 if (on_rq) {
6021 activate_task(rq_dest, p, 0);
6022 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07006024 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025out:
6026 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006027 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028}
6029
6030/*
6031 * migration_thread - this is a highprio system thread that performs
6032 * thread migration by bumping thread off CPU then 'pushing' onto
6033 * another runqueue.
6034 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006035static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006038 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
6040 rq = cpu_rq(cpu);
6041 BUG_ON(rq->migration_thread != current);
6042
6043 set_current_state(TASK_INTERRUPTIBLE);
6044 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006045 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 spin_lock_irq(&rq->lock);
6049
6050 if (cpu_is_offline(cpu)) {
6051 spin_unlock_irq(&rq->lock);
6052 goto wait_to_die;
6053 }
6054
6055 if (rq->active_balance) {
6056 active_load_balance(rq, cpu);
6057 rq->active_balance = 0;
6058 }
6059
6060 head = &rq->migration_queue;
6061
6062 if (list_empty(head)) {
6063 spin_unlock_irq(&rq->lock);
6064 schedule();
6065 set_current_state(TASK_INTERRUPTIBLE);
6066 continue;
6067 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006068 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 list_del_init(head->next);
6070
Nick Piggin674311d2005-06-25 14:57:27 -07006071 spin_unlock(&rq->lock);
6072 __migrate_task(req->task, cpu, req->dest_cpu);
6073 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074
6075 complete(&req->done);
6076 }
6077 __set_current_state(TASK_RUNNING);
6078 return 0;
6079
6080wait_to_die:
6081 /* Wait for kthread_stop */
6082 set_current_state(TASK_INTERRUPTIBLE);
6083 while (!kthread_should_stop()) {
6084 schedule();
6085 set_current_state(TASK_INTERRUPTIBLE);
6086 }
6087 __set_current_state(TASK_RUNNING);
6088 return 0;
6089}
6090
6091#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006092
6093static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6094{
6095 int ret;
6096
6097 local_irq_disable();
6098 ret = __migrate_task(p, src_cpu, dest_cpu);
6099 local_irq_enable();
6100 return ret;
6101}
6102
Kirill Korotaev054b9102006-12-10 02:20:11 -08006103/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006104 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006105 * NOTE: interrupts should be disabled by the caller
6106 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006107static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006109 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006111 struct rq *rq;
6112 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113
Andi Kleen3a5c3592007-10-15 17:00:14 +02006114 do {
6115 /* On same node? */
6116 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6117 cpus_and(mask, mask, p->cpus_allowed);
6118 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
Andi Kleen3a5c3592007-10-15 17:00:14 +02006120 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006121 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006122 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123
Andi Kleen3a5c3592007-10-15 17:00:14 +02006124 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006125 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006126 cpumask_t cpus_allowed;
6127
6128 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006129 /*
6130 * Try to stay on the same cpuset, where the
6131 * current cpuset may be a subset of all cpus.
6132 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006133 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006134 * called within calls to cpuset_lock/cpuset_unlock.
6135 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006136 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006137 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006138 dest_cpu = any_online_cpu(p->cpus_allowed);
6139 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
Andi Kleen3a5c3592007-10-15 17:00:14 +02006141 /*
6142 * Don't tell them about moving exiting tasks or
6143 * kernel threads (both mm NULL), since they never
6144 * leave kernel.
6145 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006146 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006147 printk(KERN_INFO "process %d (%s) no "
6148 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006149 task_pid_nr(p), p->comm, dead_cpu);
6150 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006151 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006152 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153}
6154
6155/*
6156 * While a dead CPU has no uninterruptible tasks queued at this point,
6157 * it might still have a nonzero ->nr_uninterruptible counter, because
6158 * for performance reasons the counter is not stricly tracking tasks to
6159 * their home CPUs. So we just add the counter to another CPU's counter,
6160 * to keep the global sum constant after CPU-down:
6161 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006162static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163{
Mike Travis7c16ec52008-04-04 18:11:11 -07006164 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 unsigned long flags;
6166
6167 local_irq_save(flags);
6168 double_rq_lock(rq_src, rq_dest);
6169 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6170 rq_src->nr_uninterruptible = 0;
6171 double_rq_unlock(rq_src, rq_dest);
6172 local_irq_restore(flags);
6173}
6174
6175/* Run through task list and migrate tasks from the dead cpu. */
6176static void migrate_live_tasks(int src_cpu)
6177{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006178 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006180 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
Ingo Molnar48f24c42006-07-03 00:25:40 -07006182 do_each_thread(t, p) {
6183 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 continue;
6185
Ingo Molnar48f24c42006-07-03 00:25:40 -07006186 if (task_cpu(p) == src_cpu)
6187 move_task_off_dead_cpu(src_cpu, p);
6188 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006190 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191}
6192
Ingo Molnardd41f592007-07-09 18:51:59 +02006193/*
6194 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006195 * It does so by boosting its priority to highest possible.
6196 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 */
6198void sched_idle_next(void)
6199{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006200 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006201 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 struct task_struct *p = rq->idle;
6203 unsigned long flags;
6204
6205 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006206 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
Ingo Molnar48f24c42006-07-03 00:25:40 -07006208 /*
6209 * Strictly not necessary since rest of the CPUs are stopped by now
6210 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 */
6212 spin_lock_irqsave(&rq->lock, flags);
6213
Ingo Molnardd41f592007-07-09 18:51:59 +02006214 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006215
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006216 update_rq_clock(rq);
6217 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
6219 spin_unlock_irqrestore(&rq->lock, flags);
6220}
6221
Ingo Molnar48f24c42006-07-03 00:25:40 -07006222/*
6223 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 * offline.
6225 */
6226void idle_task_exit(void)
6227{
6228 struct mm_struct *mm = current->active_mm;
6229
6230 BUG_ON(cpu_online(smp_processor_id()));
6231
6232 if (mm != &init_mm)
6233 switch_mm(mm, &init_mm, current);
6234 mmdrop(mm);
6235}
6236
Kirill Korotaev054b9102006-12-10 02:20:11 -08006237/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006238static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006240 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241
6242 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006243 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
6245 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006246 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247
Ingo Molnar48f24c42006-07-03 00:25:40 -07006248 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249
6250 /*
6251 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006252 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 * fine.
6254 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006255 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006256 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006257 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
Ingo Molnar48f24c42006-07-03 00:25:40 -07006259 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260}
6261
6262/* release_task() removes task from tasklist, so we won't find dead tasks. */
6263static void migrate_dead_tasks(unsigned int dead_cpu)
6264{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006265 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006266 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267
Ingo Molnardd41f592007-07-09 18:51:59 +02006268 for ( ; ; ) {
6269 if (!rq->nr_running)
6270 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006271 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006272 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006273 if (!next)
6274 break;
6275 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006276
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 }
6278}
6279#endif /* CONFIG_HOTPLUG_CPU */
6280
Nick Piggine692ab52007-07-26 13:40:43 +02006281#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6282
6283static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006284 {
6285 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006286 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006287 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006288 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006289};
6290
6291static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006292 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006293 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006294 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006295 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006296 .child = sd_ctl_dir,
6297 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006298 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006299};
6300
6301static struct ctl_table *sd_alloc_ctl_entry(int n)
6302{
6303 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006304 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006305
Nick Piggine692ab52007-07-26 13:40:43 +02006306 return entry;
6307}
6308
Milton Miller6382bc92007-10-15 17:00:19 +02006309static void sd_free_ctl_entry(struct ctl_table **tablep)
6310{
Milton Millercd7900762007-10-17 16:55:11 +02006311 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006312
Milton Millercd7900762007-10-17 16:55:11 +02006313 /*
6314 * In the intermediate directories, both the child directory and
6315 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006316 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006317 * static strings and all have proc handlers.
6318 */
6319 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006320 if (entry->child)
6321 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006322 if (entry->proc_handler == NULL)
6323 kfree(entry->procname);
6324 }
Milton Miller6382bc92007-10-15 17:00:19 +02006325
6326 kfree(*tablep);
6327 *tablep = NULL;
6328}
6329
Nick Piggine692ab52007-07-26 13:40:43 +02006330static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006331set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006332 const char *procname, void *data, int maxlen,
6333 mode_t mode, proc_handler *proc_handler)
6334{
Nick Piggine692ab52007-07-26 13:40:43 +02006335 entry->procname = procname;
6336 entry->data = data;
6337 entry->maxlen = maxlen;
6338 entry->mode = mode;
6339 entry->proc_handler = proc_handler;
6340}
6341
6342static struct ctl_table *
6343sd_alloc_ctl_domain_table(struct sched_domain *sd)
6344{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006345 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006346
Milton Millerad1cdc12007-10-15 17:00:19 +02006347 if (table == NULL)
6348 return NULL;
6349
Alexey Dobriyane0361852007-08-09 11:16:46 +02006350 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006351 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006352 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006353 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006354 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006355 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006356 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006357 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006358 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006359 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006360 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006361 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006362 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006363 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006364 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006365 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006366 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006367 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006368 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006369 &sd->cache_nice_tries,
6370 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006371 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006372 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006373 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006374
6375 return table;
6376}
6377
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006378static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006379{
6380 struct ctl_table *entry, *table;
6381 struct sched_domain *sd;
6382 int domain_num = 0, i;
6383 char buf[32];
6384
6385 for_each_domain(cpu, sd)
6386 domain_num++;
6387 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006388 if (table == NULL)
6389 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006390
6391 i = 0;
6392 for_each_domain(cpu, sd) {
6393 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006394 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006395 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006396 entry->child = sd_alloc_ctl_domain_table(sd);
6397 entry++;
6398 i++;
6399 }
6400 return table;
6401}
6402
6403static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006404static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006405{
6406 int i, cpu_num = num_online_cpus();
6407 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6408 char buf[32];
6409
Milton Miller73785472007-10-24 18:23:48 +02006410 WARN_ON(sd_ctl_dir[0].child);
6411 sd_ctl_dir[0].child = entry;
6412
Milton Millerad1cdc12007-10-15 17:00:19 +02006413 if (entry == NULL)
6414 return;
6415
Milton Miller97b6ea72007-10-15 17:00:19 +02006416 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006417 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006418 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006419 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006420 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006421 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006422 }
Milton Miller73785472007-10-24 18:23:48 +02006423
6424 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006425 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6426}
Milton Miller6382bc92007-10-15 17:00:19 +02006427
Milton Miller73785472007-10-24 18:23:48 +02006428/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006429static void unregister_sched_domain_sysctl(void)
6430{
Milton Miller73785472007-10-24 18:23:48 +02006431 if (sd_sysctl_header)
6432 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006433 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006434 if (sd_ctl_dir[0].child)
6435 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006436}
Nick Piggine692ab52007-07-26 13:40:43 +02006437#else
Milton Miller6382bc92007-10-15 17:00:19 +02006438static void register_sched_domain_sysctl(void)
6439{
6440}
6441static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006442{
6443}
6444#endif
6445
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446/*
6447 * migration_call - callback that gets triggered when a CPU is added.
6448 * Here we can start up the necessary migration thread for the new CPU.
6449 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006450static int __cpuinit
6451migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006454 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006456 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
6458 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006459
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006461 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006462 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 if (IS_ERR(p))
6464 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 kthread_bind(p, cpu);
6466 /* Must be high prio: stop_machine expects to yield to it. */
6467 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006468 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 task_rq_unlock(rq, &flags);
6470 cpu_rq(cpu)->migration_thread = p;
6471 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006472
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006474 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006475 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006477
6478 /* Update our root-domain */
6479 rq = cpu_rq(cpu);
6480 spin_lock_irqsave(&rq->lock, flags);
6481 if (rq->rd) {
6482 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6483 cpu_set(cpu, rq->rd->online);
6484 }
6485 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006487
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488#ifdef CONFIG_HOTPLUG_CPU
6489 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006490 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006491 if (!cpu_rq(cpu)->migration_thread)
6492 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006493 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006494 kthread_bind(cpu_rq(cpu)->migration_thread,
6495 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 kthread_stop(cpu_rq(cpu)->migration_thread);
6497 cpu_rq(cpu)->migration_thread = NULL;
6498 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006499
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006501 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006502 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 migrate_live_tasks(cpu);
6504 rq = cpu_rq(cpu);
6505 kthread_stop(rq->migration_thread);
6506 rq->migration_thread = NULL;
6507 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006508 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006509 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006510 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006512 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6513 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006515 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006516 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 migrate_nr_uninterruptible(rq);
6518 BUG_ON(rq->nr_running != 0);
6519
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006520 /*
6521 * No need to migrate the tasks: it was best-effort if
6522 * they didn't take sched_hotcpu_mutex. Just wake up
6523 * the requestors.
6524 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 spin_lock_irq(&rq->lock);
6526 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006527 struct migration_req *req;
6528
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006530 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 list_del_init(&req->list);
6532 complete(&req->done);
6533 }
6534 spin_unlock_irq(&rq->lock);
6535 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006536
Gregory Haskins08f503b2008-03-10 17:59:11 -04006537 case CPU_DYING:
6538 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006539 /* Update our root-domain */
6540 rq = cpu_rq(cpu);
6541 spin_lock_irqsave(&rq->lock, flags);
6542 if (rq->rd) {
6543 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6544 cpu_clear(cpu, rq->rd->online);
6545 }
6546 spin_unlock_irqrestore(&rq->lock, flags);
6547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548#endif
6549 }
6550 return NOTIFY_OK;
6551}
6552
6553/* Register at highest priority so that task migration (migrate_all_tasks)
6554 * happens before everything else.
6555 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006556static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 .notifier_call = migration_call,
6558 .priority = 10
6559};
6560
Adrian Bunke6fe6642007-11-09 22:39:39 +01006561void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562{
6563 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006564 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006565
6566 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006567 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6568 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6570 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571}
6572#endif
6573
6574#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006575
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006576#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006577
Mike Travis7c16ec52008-04-04 18:11:11 -07006578static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6579 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006580{
6581 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006582 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006583
Mike Travis434d53b2008-04-04 18:11:04 -07006584 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006585 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006586
6587 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6588
6589 if (!(sd->flags & SD_LOAD_BALANCE)) {
6590 printk("does not load-balance\n");
6591 if (sd->parent)
6592 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6593 " has parent");
6594 return -1;
6595 }
6596
6597 printk(KERN_CONT "span %s\n", str);
6598
6599 if (!cpu_isset(cpu, sd->span)) {
6600 printk(KERN_ERR "ERROR: domain->span does not contain "
6601 "CPU%d\n", cpu);
6602 }
6603 if (!cpu_isset(cpu, group->cpumask)) {
6604 printk(KERN_ERR "ERROR: domain->groups does not contain"
6605 " CPU%d\n", cpu);
6606 }
6607
6608 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6609 do {
6610 if (!group) {
6611 printk("\n");
6612 printk(KERN_ERR "ERROR: group is NULL\n");
6613 break;
6614 }
6615
6616 if (!group->__cpu_power) {
6617 printk(KERN_CONT "\n");
6618 printk(KERN_ERR "ERROR: domain->cpu_power not "
6619 "set\n");
6620 break;
6621 }
6622
6623 if (!cpus_weight(group->cpumask)) {
6624 printk(KERN_CONT "\n");
6625 printk(KERN_ERR "ERROR: empty group\n");
6626 break;
6627 }
6628
Mike Travis7c16ec52008-04-04 18:11:11 -07006629 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006630 printk(KERN_CONT "\n");
6631 printk(KERN_ERR "ERROR: repeated CPUs\n");
6632 break;
6633 }
6634
Mike Travis7c16ec52008-04-04 18:11:11 -07006635 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006636
Mike Travis434d53b2008-04-04 18:11:04 -07006637 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006638 printk(KERN_CONT " %s", str);
6639
6640 group = group->next;
6641 } while (group != sd->groups);
6642 printk(KERN_CONT "\n");
6643
Mike Travis7c16ec52008-04-04 18:11:11 -07006644 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006645 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6646
Mike Travis7c16ec52008-04-04 18:11:11 -07006647 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006648 printk(KERN_ERR "ERROR: parent span is not a superset "
6649 "of domain->span\n");
6650 return 0;
6651}
6652
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653static void sched_domain_debug(struct sched_domain *sd, int cpu)
6654{
Mike Travis7c16ec52008-04-04 18:11:11 -07006655 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 int level = 0;
6657
Nick Piggin41c7ce92005-06-25 14:57:24 -07006658 if (!sd) {
6659 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6660 return;
6661 }
6662
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6664
Mike Travis7c16ec52008-04-04 18:11:11 -07006665 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6666 if (!groupmask) {
6667 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6668 return;
6669 }
6670
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006671 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006672 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 level++;
6675 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006676 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006677 break;
6678 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006679 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680}
6681#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006682# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683#endif
6684
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006685static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006686{
6687 if (cpus_weight(sd->span) == 1)
6688 return 1;
6689
6690 /* Following flags need at least 2 groups */
6691 if (sd->flags & (SD_LOAD_BALANCE |
6692 SD_BALANCE_NEWIDLE |
6693 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006694 SD_BALANCE_EXEC |
6695 SD_SHARE_CPUPOWER |
6696 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006697 if (sd->groups != sd->groups->next)
6698 return 0;
6699 }
6700
6701 /* Following flags don't use groups */
6702 if (sd->flags & (SD_WAKE_IDLE |
6703 SD_WAKE_AFFINE |
6704 SD_WAKE_BALANCE))
6705 return 0;
6706
6707 return 1;
6708}
6709
Ingo Molnar48f24c42006-07-03 00:25:40 -07006710static int
6711sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006712{
6713 unsigned long cflags = sd->flags, pflags = parent->flags;
6714
6715 if (sd_degenerate(parent))
6716 return 1;
6717
6718 if (!cpus_equal(sd->span, parent->span))
6719 return 0;
6720
6721 /* Does parent contain flags not in child? */
6722 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6723 if (cflags & SD_WAKE_AFFINE)
6724 pflags &= ~SD_WAKE_BALANCE;
6725 /* Flags needing groups don't count if only 1 group in parent */
6726 if (parent->groups == parent->groups->next) {
6727 pflags &= ~(SD_LOAD_BALANCE |
6728 SD_BALANCE_NEWIDLE |
6729 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006730 SD_BALANCE_EXEC |
6731 SD_SHARE_CPUPOWER |
6732 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006733 }
6734 if (~cflags & pflags)
6735 return 0;
6736
6737 return 1;
6738}
6739
Gregory Haskins57d885f2008-01-25 21:08:18 +01006740static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6741{
6742 unsigned long flags;
6743 const struct sched_class *class;
6744
6745 spin_lock_irqsave(&rq->lock, flags);
6746
6747 if (rq->rd) {
6748 struct root_domain *old_rd = rq->rd;
6749
Ingo Molnar0eab9142008-01-25 21:08:19 +01006750 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006751 if (class->leave_domain)
6752 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006753 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006754
Gregory Haskinsdc938522008-01-25 21:08:26 +01006755 cpu_clear(rq->cpu, old_rd->span);
6756 cpu_clear(rq->cpu, old_rd->online);
6757
Gregory Haskins57d885f2008-01-25 21:08:18 +01006758 if (atomic_dec_and_test(&old_rd->refcount))
6759 kfree(old_rd);
6760 }
6761
6762 atomic_inc(&rd->refcount);
6763 rq->rd = rd;
6764
Gregory Haskinsdc938522008-01-25 21:08:26 +01006765 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006766 if (cpu_isset(rq->cpu, cpu_online_map))
6767 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006768
Ingo Molnar0eab9142008-01-25 21:08:19 +01006769 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006770 if (class->join_domain)
6771 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006772 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006773
6774 spin_unlock_irqrestore(&rq->lock, flags);
6775}
6776
Gregory Haskinsdc938522008-01-25 21:08:26 +01006777static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006778{
6779 memset(rd, 0, sizeof(*rd));
6780
Gregory Haskinsdc938522008-01-25 21:08:26 +01006781 cpus_clear(rd->span);
6782 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006783}
6784
6785static void init_defrootdomain(void)
6786{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006787 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006788 atomic_set(&def_root_domain.refcount, 1);
6789}
6790
Gregory Haskinsdc938522008-01-25 21:08:26 +01006791static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006792{
6793 struct root_domain *rd;
6794
6795 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6796 if (!rd)
6797 return NULL;
6798
Gregory Haskinsdc938522008-01-25 21:08:26 +01006799 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006800
6801 return rd;
6802}
6803
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006805 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 * hold the hotplug lock.
6807 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006808static void
6809cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006811 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006812 struct sched_domain *tmp;
6813
6814 /* Remove the sched domains which do not contribute to scheduling. */
6815 for (tmp = sd; tmp; tmp = tmp->parent) {
6816 struct sched_domain *parent = tmp->parent;
6817 if (!parent)
6818 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006819 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006820 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006821 if (parent->parent)
6822 parent->parent->child = tmp;
6823 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006824 }
6825
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006826 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006827 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006828 if (sd)
6829 sd->child = NULL;
6830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831
6832 sched_domain_debug(sd, cpu);
6833
Gregory Haskins57d885f2008-01-25 21:08:18 +01006834 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006835 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836}
6837
6838/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006839static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840
6841/* Setup the mask of cpus configured for isolated domains */
6842static int __init isolated_cpu_setup(char *str)
6843{
6844 int ints[NR_CPUS], i;
6845
6846 str = get_options(str, ARRAY_SIZE(ints), ints);
6847 cpus_clear(cpu_isolated_map);
6848 for (i = 1; i <= ints[0]; i++)
6849 if (ints[i] < NR_CPUS)
6850 cpu_set(ints[i], cpu_isolated_map);
6851 return 1;
6852}
6853
Ingo Molnar8927f492007-10-15 17:00:13 +02006854__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855
6856/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006857 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6858 * to a function which identifies what group(along with sched group) a CPU
6859 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6860 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 *
6862 * init_sched_build_groups will build a circular linked list of the groups
6863 * covered by the given span, and will set each group's ->cpumask correctly,
6864 * and ->cpu_power to 0.
6865 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006866static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006867init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006868 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006869 struct sched_group **sg,
6870 cpumask_t *tmpmask),
6871 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872{
6873 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 int i;
6875
Mike Travis7c16ec52008-04-04 18:11:11 -07006876 cpus_clear(*covered);
6877
6878 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006879 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006880 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 int j;
6882
Mike Travis7c16ec52008-04-04 18:11:11 -07006883 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 continue;
6885
Mike Travis7c16ec52008-04-04 18:11:11 -07006886 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006887 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
Mike Travis7c16ec52008-04-04 18:11:11 -07006889 for_each_cpu_mask(j, *span) {
6890 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 continue;
6892
Mike Travis7c16ec52008-04-04 18:11:11 -07006893 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 cpu_set(j, sg->cpumask);
6895 }
6896 if (!first)
6897 first = sg;
6898 if (last)
6899 last->next = sg;
6900 last = sg;
6901 }
6902 last->next = first;
6903}
6904
John Hawkes9c1cfda2005-09-06 15:18:14 -07006905#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906
John Hawkes9c1cfda2005-09-06 15:18:14 -07006907#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006908
John Hawkes9c1cfda2005-09-06 15:18:14 -07006909/**
6910 * find_next_best_node - find the next node to include in a sched_domain
6911 * @node: node whose sched_domain we're building
6912 * @used_nodes: nodes already in the sched_domain
6913 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006914 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006915 * finds the closest node not already in the @used_nodes map.
6916 *
6917 * Should use nodemask_t.
6918 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006919static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006920{
6921 int i, n, val, min_val, best_node = 0;
6922
6923 min_val = INT_MAX;
6924
6925 for (i = 0; i < MAX_NUMNODES; i++) {
6926 /* Start at @node */
6927 n = (node + i) % MAX_NUMNODES;
6928
6929 if (!nr_cpus_node(n))
6930 continue;
6931
6932 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006933 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006934 continue;
6935
6936 /* Simple min distance search */
6937 val = node_distance(node, n);
6938
6939 if (val < min_val) {
6940 min_val = val;
6941 best_node = n;
6942 }
6943 }
6944
Mike Travisc5f59f02008-04-04 18:11:10 -07006945 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006946 return best_node;
6947}
6948
6949/**
6950 * sched_domain_node_span - get a cpumask for a node's sched_domain
6951 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07006952 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07006953 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006954 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006955 * should be one that prevents unnecessary balancing, but also spreads tasks
6956 * out optimally.
6957 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07006958static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006959{
Mike Travisc5f59f02008-04-04 18:11:10 -07006960 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07006961 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006962 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006963
Mike Travis4bdbaad32008-04-15 16:35:52 -07006964 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07006965 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006966
Mike Travis4bdbaad32008-04-15 16:35:52 -07006967 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07006968 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006969
6970 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07006971 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006972
Mike Travisc5f59f02008-04-04 18:11:10 -07006973 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07006974 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006975 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006976}
6977#endif
6978
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006979int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006980
John Hawkes9c1cfda2005-09-06 15:18:14 -07006981/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006982 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006983 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984#ifdef CONFIG_SCHED_SMT
6985static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006986static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006987
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006988static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006989cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6990 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006992 if (sg)
6993 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 return cpu;
6995}
6996#endif
6997
Ingo Molnar48f24c42006-07-03 00:25:40 -07006998/*
6999 * multi-core sched-domains:
7000 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007001#ifdef CONFIG_SCHED_MC
7002static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007003static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007004#endif
7005
7006#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007007static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007008cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7009 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007010{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007011 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007012
7013 *mask = per_cpu(cpu_sibling_map, cpu);
7014 cpus_and(*mask, *mask, *cpu_map);
7015 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007016 if (sg)
7017 *sg = &per_cpu(sched_group_core, group);
7018 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007019}
7020#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007021static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007022cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7023 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007024{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007025 if (sg)
7026 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007027 return cpu;
7028}
7029#endif
7030
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007032static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007033
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007034static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007035cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7036 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007038 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007039#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007040 *mask = cpu_coregroup_map(cpu);
7041 cpus_and(*mask, *mask, *cpu_map);
7042 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007043#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007044 *mask = per_cpu(cpu_sibling_map, cpu);
7045 cpus_and(*mask, *mask, *cpu_map);
7046 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007048 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007050 if (sg)
7051 *sg = &per_cpu(sched_group_phys, group);
7052 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053}
7054
7055#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007056/*
7057 * The init_sched_build_groups can't handle what we want to do with node
7058 * groups, so roll our own. Now each node has its own list of groups which
7059 * gets dynamically allocated.
7060 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007062static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007063
7064static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007065static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007066
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007067static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007068 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007070 int group;
7071
Mike Travis7c16ec52008-04-04 18:11:11 -07007072 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7073 cpus_and(*nodemask, *nodemask, *cpu_map);
7074 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007075
7076 if (sg)
7077 *sg = &per_cpu(sched_group_allnodes, group);
7078 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007080
Siddha, Suresh B08069032006-03-27 01:15:23 -08007081static void init_numa_sched_groups_power(struct sched_group *group_head)
7082{
7083 struct sched_group *sg = group_head;
7084 int j;
7085
7086 if (!sg)
7087 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007088 do {
7089 for_each_cpu_mask(j, sg->cpumask) {
7090 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007091
Andi Kleen3a5c3592007-10-15 17:00:14 +02007092 sd = &per_cpu(phys_domains, j);
7093 if (j != first_cpu(sd->groups->cpumask)) {
7094 /*
7095 * Only add "power" once for each
7096 * physical package.
7097 */
7098 continue;
7099 }
7100
7101 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007102 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007103 sg = sg->next;
7104 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007105}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106#endif
7107
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007108#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007109/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007110static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007111{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007112 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007113
7114 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007115 struct sched_group **sched_group_nodes
7116 = sched_group_nodes_bycpu[cpu];
7117
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007118 if (!sched_group_nodes)
7119 continue;
7120
7121 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007122 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7123
Mike Travis7c16ec52008-04-04 18:11:11 -07007124 *nodemask = node_to_cpumask(i);
7125 cpus_and(*nodemask, *nodemask, *cpu_map);
7126 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007127 continue;
7128
7129 if (sg == NULL)
7130 continue;
7131 sg = sg->next;
7132next_sg:
7133 oldsg = sg;
7134 sg = sg->next;
7135 kfree(oldsg);
7136 if (oldsg != sched_group_nodes[i])
7137 goto next_sg;
7138 }
7139 kfree(sched_group_nodes);
7140 sched_group_nodes_bycpu[cpu] = NULL;
7141 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007142}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007143#else
Mike Travis7c16ec52008-04-04 18:11:11 -07007144static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007145{
7146}
7147#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007148
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007150 * Initialize sched groups cpu_power.
7151 *
7152 * cpu_power indicates the capacity of sched group, which is used while
7153 * distributing the load between different sched groups in a sched domain.
7154 * Typically cpu_power for all the groups in a sched domain will be same unless
7155 * there are asymmetries in the topology. If there are asymmetries, group
7156 * having more cpu_power will pickup more load compared to the group having
7157 * less cpu_power.
7158 *
7159 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7160 * the maximum number of tasks a group can handle in the presence of other idle
7161 * or lightly loaded groups in the same sched domain.
7162 */
7163static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7164{
7165 struct sched_domain *child;
7166 struct sched_group *group;
7167
7168 WARN_ON(!sd || !sd->groups);
7169
7170 if (cpu != first_cpu(sd->groups->cpumask))
7171 return;
7172
7173 child = sd->child;
7174
Eric Dumazet5517d862007-05-08 00:32:57 -07007175 sd->groups->__cpu_power = 0;
7176
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007177 /*
7178 * For perf policy, if the groups in child domain share resources
7179 * (for example cores sharing some portions of the cache hierarchy
7180 * or SMT), then set this domain groups cpu_power such that each group
7181 * can handle only one task, when there are other idle groups in the
7182 * same sched domain.
7183 */
7184 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7185 (child->flags &
7186 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007187 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007188 return;
7189 }
7190
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007191 /*
7192 * add cpu_power of each child group to this groups cpu_power
7193 */
7194 group = child->groups;
7195 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007196 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007197 group = group->next;
7198 } while (group != child->groups);
7199}
7200
7201/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007202 * Initializers for schedule domains
7203 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7204 */
7205
7206#define SD_INIT(sd, type) sd_init_##type(sd)
7207#define SD_INIT_FUNC(type) \
7208static noinline void sd_init_##type(struct sched_domain *sd) \
7209{ \
7210 memset(sd, 0, sizeof(*sd)); \
7211 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007212 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007213}
7214
7215SD_INIT_FUNC(CPU)
7216#ifdef CONFIG_NUMA
7217 SD_INIT_FUNC(ALLNODES)
7218 SD_INIT_FUNC(NODE)
7219#endif
7220#ifdef CONFIG_SCHED_SMT
7221 SD_INIT_FUNC(SIBLING)
7222#endif
7223#ifdef CONFIG_SCHED_MC
7224 SD_INIT_FUNC(MC)
7225#endif
7226
7227/*
7228 * To minimize stack usage kmalloc room for cpumasks and share the
7229 * space as the usage in build_sched_domains() dictates. Used only
7230 * if the amount of space is significant.
7231 */
7232struct allmasks {
7233 cpumask_t tmpmask; /* make this one first */
7234 union {
7235 cpumask_t nodemask;
7236 cpumask_t this_sibling_map;
7237 cpumask_t this_core_map;
7238 };
7239 cpumask_t send_covered;
7240
7241#ifdef CONFIG_NUMA
7242 cpumask_t domainspan;
7243 cpumask_t covered;
7244 cpumask_t notcovered;
7245#endif
7246};
7247
7248#if NR_CPUS > 128
7249#define SCHED_CPUMASK_ALLOC 1
7250#define SCHED_CPUMASK_FREE(v) kfree(v)
7251#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7252#else
7253#define SCHED_CPUMASK_ALLOC 0
7254#define SCHED_CPUMASK_FREE(v)
7255#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7256#endif
7257
7258#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7259 ((unsigned long)(a) + offsetof(struct allmasks, v))
7260
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007261static int default_relax_domain_level = -1;
7262
7263static int __init setup_relax_domain_level(char *str)
7264{
7265 default_relax_domain_level = simple_strtoul(str, NULL, 0);
7266 return 1;
7267}
7268__setup("relax_domain_level=", setup_relax_domain_level);
7269
7270static void set_domain_attribute(struct sched_domain *sd,
7271 struct sched_domain_attr *attr)
7272{
7273 int request;
7274
7275 if (!attr || attr->relax_domain_level < 0) {
7276 if (default_relax_domain_level < 0)
7277 return;
7278 else
7279 request = default_relax_domain_level;
7280 } else
7281 request = attr->relax_domain_level;
7282 if (request < sd->level) {
7283 /* turn off idle balance on this domain */
7284 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7285 } else {
7286 /* turn on idle balance on this domain */
7287 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7288 }
7289}
7290
Mike Travis7c16ec52008-04-04 18:11:11 -07007291/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007292 * Build sched domains for a given set of cpus and attach the sched domains
7293 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007295static int __build_sched_domains(const cpumask_t *cpu_map,
7296 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297{
7298 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007299 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007300 SCHED_CPUMASK_DECLARE(allmasks);
7301 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007302#ifdef CONFIG_NUMA
7303 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007304 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007305
7306 /*
7307 * Allocate the per-node list of sched groups
7308 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007309 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007310 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007311 if (!sched_group_nodes) {
7312 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007313 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007314 }
John Hawkesd1b55132005-09-06 15:18:14 -07007315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316
Gregory Haskinsdc938522008-01-25 21:08:26 +01007317 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007318 if (!rd) {
7319 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007320#ifdef CONFIG_NUMA
7321 kfree(sched_group_nodes);
7322#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007323 return -ENOMEM;
7324 }
7325
Mike Travis7c16ec52008-04-04 18:11:11 -07007326#if SCHED_CPUMASK_ALLOC
7327 /* get space for all scratch cpumask variables */
7328 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7329 if (!allmasks) {
7330 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7331 kfree(rd);
7332#ifdef CONFIG_NUMA
7333 kfree(sched_group_nodes);
7334#endif
7335 return -ENOMEM;
7336 }
7337#endif
7338 tmpmask = (cpumask_t *)allmasks;
7339
7340
7341#ifdef CONFIG_NUMA
7342 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7343#endif
7344
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007346 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007348 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007350 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351
Mike Travis7c16ec52008-04-04 18:11:11 -07007352 *nodemask = node_to_cpumask(cpu_to_node(i));
7353 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354
7355#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007356 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007357 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007358 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007359 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007360 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007361 sd->span = *cpu_map;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007362 sd->first_cpu = first_cpu(sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07007363 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007364 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007365 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007366 } else
7367 p = NULL;
7368
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007370 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007371 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007372 sched_domain_node_span(cpu_to_node(i), &sd->span);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007373 sd->first_cpu = first_cpu(sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007374 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007375 if (p)
7376 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007377 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378#endif
7379
7380 p = sd;
7381 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007382 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007383 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007384 sd->span = *nodemask;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007385 sd->first_cpu = first_cpu(sd->span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007387 if (p)
7388 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007389 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007391#ifdef CONFIG_SCHED_MC
7392 p = sd;
7393 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007394 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007395 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007396 sd->span = cpu_coregroup_map(i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007397 sd->first_cpu = first_cpu(sd->span);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007398 cpus_and(sd->span, sd->span, *cpu_map);
7399 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007400 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007401 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007402#endif
7403
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404#ifdef CONFIG_SCHED_SMT
7405 p = sd;
7406 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007407 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007408 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007409 sd->span = per_cpu(cpu_sibling_map, i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007410 sd->first_cpu = first_cpu(sd->span);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007411 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007413 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007414 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415#endif
7416 }
7417
7418#ifdef CONFIG_SCHED_SMT
7419 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007420 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007421 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7422 SCHED_CPUMASK_VAR(send_covered, allmasks);
7423
7424 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7425 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7426 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 continue;
7428
Ingo Molnardd41f592007-07-09 18:51:59 +02007429 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007430 &cpu_to_cpu_group,
7431 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 }
7433#endif
7434
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007435#ifdef CONFIG_SCHED_MC
7436 /* Set up multi-core groups */
7437 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007438 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7439 SCHED_CPUMASK_VAR(send_covered, allmasks);
7440
7441 *this_core_map = cpu_coregroup_map(i);
7442 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7443 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007444 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007445
Ingo Molnardd41f592007-07-09 18:51:59 +02007446 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007447 &cpu_to_core_group,
7448 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007449 }
7450#endif
7451
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 /* Set up physical groups */
7453 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007454 SCHED_CPUMASK_VAR(nodemask, allmasks);
7455 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456
Mike Travis7c16ec52008-04-04 18:11:11 -07007457 *nodemask = node_to_cpumask(i);
7458 cpus_and(*nodemask, *nodemask, *cpu_map);
7459 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 continue;
7461
Mike Travis7c16ec52008-04-04 18:11:11 -07007462 init_sched_build_groups(nodemask, cpu_map,
7463 &cpu_to_phys_group,
7464 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 }
7466
7467#ifdef CONFIG_NUMA
7468 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007469 if (sd_allnodes) {
7470 SCHED_CPUMASK_VAR(send_covered, allmasks);
7471
7472 init_sched_build_groups(cpu_map, cpu_map,
7473 &cpu_to_allnodes_group,
7474 send_covered, tmpmask);
7475 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007476
7477 for (i = 0; i < MAX_NUMNODES; i++) {
7478 /* Set up node groups */
7479 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007480 SCHED_CPUMASK_VAR(nodemask, allmasks);
7481 SCHED_CPUMASK_VAR(domainspan, allmasks);
7482 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007483 int j;
7484
Mike Travis7c16ec52008-04-04 18:11:11 -07007485 *nodemask = node_to_cpumask(i);
7486 cpus_clear(*covered);
7487
7488 cpus_and(*nodemask, *nodemask, *cpu_map);
7489 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007490 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007491 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007492 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007493
Mike Travis4bdbaad32008-04-15 16:35:52 -07007494 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007495 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007496
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007497 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007498 if (!sg) {
7499 printk(KERN_WARNING "Can not alloc domain group for "
7500 "node %d\n", i);
7501 goto error;
7502 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007503 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007504 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007505 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007506
John Hawkes9c1cfda2005-09-06 15:18:14 -07007507 sd = &per_cpu(node_domains, j);
7508 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007509 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007510 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007511 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007512 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007513 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007514 prev = sg;
7515
7516 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007517 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007518 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007519 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007520
Mike Travis7c16ec52008-04-04 18:11:11 -07007521 cpus_complement(*notcovered, *covered);
7522 cpus_and(*tmpmask, *notcovered, *cpu_map);
7523 cpus_and(*tmpmask, *tmpmask, *domainspan);
7524 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007525 break;
7526
Mike Travis7c16ec52008-04-04 18:11:11 -07007527 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7528 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007529 continue;
7530
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007531 sg = kmalloc_node(sizeof(struct sched_group),
7532 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007533 if (!sg) {
7534 printk(KERN_WARNING
7535 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007536 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007537 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007538 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007539 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007540 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007541 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007542 prev->next = sg;
7543 prev = sg;
7544 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546#endif
7547
7548 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007549#ifdef CONFIG_SCHED_SMT
7550 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007551 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7552
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007553 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007554 }
7555#endif
7556#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007557 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007558 struct sched_domain *sd = &per_cpu(core_domains, i);
7559
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007560 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007561 }
7562#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007564 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007565 struct sched_domain *sd = &per_cpu(phys_domains, i);
7566
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007567 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 }
7569
John Hawkes9c1cfda2005-09-06 15:18:14 -07007570#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007571 for (i = 0; i < MAX_NUMNODES; i++)
7572 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007573
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007574 if (sd_allnodes) {
7575 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007576
Mike Travis7c16ec52008-04-04 18:11:11 -07007577 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7578 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007579 init_numa_sched_groups_power(sg);
7580 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007581#endif
7582
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007584 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585 struct sched_domain *sd;
7586#ifdef CONFIG_SCHED_SMT
7587 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007588#elif defined(CONFIG_SCHED_MC)
7589 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590#else
7591 sd = &per_cpu(phys_domains, i);
7592#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007593 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007595
Mike Travis7c16ec52008-04-04 18:11:11 -07007596 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007597 return 0;
7598
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007599#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007600error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007601 free_sched_groups(cpu_map, tmpmask);
7602 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007603 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007604#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605}
Paul Jackson029190c2007-10-18 23:40:20 -07007606
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007607static int build_sched_domains(const cpumask_t *cpu_map)
7608{
7609 return __build_sched_domains(cpu_map, NULL);
7610}
7611
Paul Jackson029190c2007-10-18 23:40:20 -07007612static cpumask_t *doms_cur; /* current sched domains */
7613static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007614static struct sched_domain_attr *dattr_cur; /* attribues of custom domains
7615 in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007616
7617/*
7618 * Special case: If a kmalloc of a doms_cur partition (array of
7619 * cpumask_t) fails, then fallback to a single sched domain,
7620 * as determined by the single cpumask_t fallback_doms.
7621 */
7622static cpumask_t fallback_doms;
7623
Heiko Carstens22e52b02008-03-12 18:31:59 +01007624void __attribute__((weak)) arch_update_cpu_topology(void)
7625{
7626}
7627
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007628/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007629 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007630 * For now this just excludes isolated cpus, but could be used to
7631 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007632 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007633static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007634{
Milton Miller73785472007-10-24 18:23:48 +02007635 int err;
7636
Heiko Carstens22e52b02008-03-12 18:31:59 +01007637 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007638 ndoms_cur = 1;
7639 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7640 if (!doms_cur)
7641 doms_cur = &fallback_doms;
7642 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007643 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007644 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007645 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007646
7647 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007648}
7649
Mike Travis7c16ec52008-04-04 18:11:11 -07007650static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7651 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652{
Mike Travis7c16ec52008-04-04 18:11:11 -07007653 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007654}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007656/*
7657 * Detach sched domains from a group of cpus specified in cpu_map
7658 * These cpus will now be attached to the NULL domain
7659 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007660static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007661{
Mike Travis7c16ec52008-04-04 18:11:11 -07007662 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007663 int i;
7664
Milton Miller6382bc92007-10-15 17:00:19 +02007665 unregister_sched_domain_sysctl();
7666
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007667 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007668 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007669 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007670 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007671}
7672
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007673/* handle null as "default" */
7674static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7675 struct sched_domain_attr *new, int idx_new)
7676{
7677 struct sched_domain_attr tmp;
7678
7679 /* fast path */
7680 if (!new && !cur)
7681 return 1;
7682
7683 tmp = SD_ATTR_INIT;
7684 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7685 new ? (new + idx_new) : &tmp,
7686 sizeof(struct sched_domain_attr));
7687}
7688
Paul Jackson029190c2007-10-18 23:40:20 -07007689/*
7690 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007691 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007692 * doms_new[] to the current sched domain partitioning, doms_cur[].
7693 * It destroys each deleted domain and builds each new domain.
7694 *
7695 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007696 * The masks don't intersect (don't overlap.) We should setup one
7697 * sched domain for each mask. CPUs not in any of the cpumasks will
7698 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007699 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7700 * it as it is.
7701 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007702 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7703 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007704 * failed the kmalloc call, then it can pass in doms_new == NULL,
7705 * and partition_sched_domains() will fallback to the single partition
7706 * 'fallback_doms'.
7707 *
7708 * Call with hotplug lock held
7709 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007710void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7711 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007712{
7713 int i, j;
7714
Heiko Carstens712555e2008-04-28 11:33:07 +02007715 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007716
Milton Miller73785472007-10-24 18:23:48 +02007717 /* always unregister in case we don't destroy any domains */
7718 unregister_sched_domain_sysctl();
7719
Paul Jackson029190c2007-10-18 23:40:20 -07007720 if (doms_new == NULL) {
7721 ndoms_new = 1;
7722 doms_new = &fallback_doms;
7723 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007724 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007725 }
7726
7727 /* Destroy deleted domains */
7728 for (i = 0; i < ndoms_cur; i++) {
7729 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007730 if (cpus_equal(doms_cur[i], doms_new[j])
7731 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007732 goto match1;
7733 }
7734 /* no match - a current sched domain not in new doms_new[] */
7735 detach_destroy_domains(doms_cur + i);
7736match1:
7737 ;
7738 }
7739
7740 /* Build new domains */
7741 for (i = 0; i < ndoms_new; i++) {
7742 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007743 if (cpus_equal(doms_new[i], doms_cur[j])
7744 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007745 goto match2;
7746 }
7747 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007748 __build_sched_domains(doms_new + i,
7749 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007750match2:
7751 ;
7752 }
7753
7754 /* Remember the new sched domains */
7755 if (doms_cur != &fallback_doms)
7756 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007757 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007758 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007759 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007760 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007761
7762 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007763
Heiko Carstens712555e2008-04-28 11:33:07 +02007764 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007765}
7766
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007767#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007768int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007769{
7770 int err;
7771
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007772 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007773 mutex_lock(&sched_domains_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007774 detach_destroy_domains(&cpu_online_map);
7775 err = arch_init_sched_domains(&cpu_online_map);
Heiko Carstens712555e2008-04-28 11:33:07 +02007776 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007777 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007778
7779 return err;
7780}
7781
7782static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7783{
7784 int ret;
7785
7786 if (buf[0] != '0' && buf[0] != '1')
7787 return -EINVAL;
7788
7789 if (smt)
7790 sched_smt_power_savings = (buf[0] == '1');
7791 else
7792 sched_mc_power_savings = (buf[0] == '1');
7793
7794 ret = arch_reinit_sched_domains();
7795
7796 return ret ? ret : count;
7797}
7798
Adrian Bunk6707de002007-08-12 18:08:19 +02007799#ifdef CONFIG_SCHED_MC
7800static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7801{
7802 return sprintf(page, "%u\n", sched_mc_power_savings);
7803}
7804static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7805 const char *buf, size_t count)
7806{
7807 return sched_power_savings_store(buf, count, 0);
7808}
7809static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7810 sched_mc_power_savings_store);
7811#endif
7812
7813#ifdef CONFIG_SCHED_SMT
7814static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7815{
7816 return sprintf(page, "%u\n", sched_smt_power_savings);
7817}
7818static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7819 const char *buf, size_t count)
7820{
7821 return sched_power_savings_store(buf, count, 1);
7822}
7823static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7824 sched_smt_power_savings_store);
7825#endif
7826
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007827int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7828{
7829 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007830
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007831#ifdef CONFIG_SCHED_SMT
7832 if (smt_capable())
7833 err = sysfs_create_file(&cls->kset.kobj,
7834 &attr_sched_smt_power_savings.attr);
7835#endif
7836#ifdef CONFIG_SCHED_MC
7837 if (!err && mc_capable())
7838 err = sysfs_create_file(&cls->kset.kobj,
7839 &attr_sched_mc_power_savings.attr);
7840#endif
7841 return err;
7842}
7843#endif
7844
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007846 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007848 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849 * which will prevent rebalancing while the sched domains are recalculated.
7850 */
7851static int update_sched_domains(struct notifier_block *nfb,
7852 unsigned long action, void *hcpu)
7853{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 switch (action) {
7855 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007856 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007858 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007859 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860 return NOTIFY_OK;
7861
7862 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007863 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007865 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007867 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007869 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 /*
7871 * Fall through and re-initialise the domains.
7872 */
7873 break;
7874 default:
7875 return NOTIFY_DONE;
7876 }
7877
7878 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007879 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007880
7881 return NOTIFY_OK;
7882}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883
7884void __init sched_init_smp(void)
7885{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007886 cpumask_t non_isolated_cpus;
7887
Mike Travis434d53b2008-04-04 18:11:04 -07007888#if defined(CONFIG_NUMA)
7889 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7890 GFP_KERNEL);
7891 BUG_ON(sched_group_nodes_bycpu == NULL);
7892#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007893 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007894 mutex_lock(&sched_domains_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007895 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007896 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007897 if (cpus_empty(non_isolated_cpus))
7898 cpu_set(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007899 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007900 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 /* XXX: Theoretical race here - CPU may be hotplugged now */
7902 hotcpu_notifier(update_sched_domains, 0);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007903 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007904
7905 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07007906 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007907 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007908 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909}
7910#else
7911void __init sched_init_smp(void)
7912{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007913 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914}
7915#endif /* CONFIG_SMP */
7916
7917int in_sched_functions(unsigned long addr)
7918{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 return in_lock_functions(addr) ||
7920 (addr >= (unsigned long)__sched_text_start
7921 && addr < (unsigned long)__sched_text_end);
7922}
7923
Alexey Dobriyana9957442007-10-15 17:00:13 +02007924static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007925{
7926 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007927 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02007928#ifdef CONFIG_FAIR_GROUP_SCHED
7929 cfs_rq->rq = rq;
7930#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007931 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007932}
7933
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007934static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7935{
7936 struct rt_prio_array *array;
7937 int i;
7938
7939 array = &rt_rq->active;
7940 for (i = 0; i < MAX_RT_PRIO; i++) {
7941 INIT_LIST_HEAD(array->queue + i);
7942 __clear_bit(i, array->bitmap);
7943 }
7944 /* delimiter for bitsearch: */
7945 __set_bit(MAX_RT_PRIO, array->bitmap);
7946
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007947#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007948 rt_rq->highest_prio = MAX_RT_PRIO;
7949#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007950#ifdef CONFIG_SMP
7951 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007952 rt_rq->overloaded = 0;
7953#endif
7954
7955 rt_rq->rt_time = 0;
7956 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007957 rt_rq->rt_runtime = 0;
7958 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007959
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007960#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007961 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007962 rt_rq->rq = rq;
7963#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007964}
7965
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007966#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007967static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7968 struct sched_entity *se, int cpu, int add,
7969 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007970{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007971 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007972 tg->cfs_rq[cpu] = cfs_rq;
7973 init_cfs_rq(cfs_rq, rq);
7974 cfs_rq->tg = tg;
7975 if (add)
7976 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7977
7978 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007979 /* se could be NULL for init_task_group */
7980 if (!se)
7981 return;
7982
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007983 if (!parent)
7984 se->cfs_rq = &rq->cfs;
7985 else
7986 se->cfs_rq = parent->my_q;
7987
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007988 se->my_q = cfs_rq;
7989 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02007990 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007991 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007992}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007993#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007994
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007995#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007996static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
7997 struct sched_rt_entity *rt_se, int cpu, int add,
7998 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007999{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008000 struct rq *rq = cpu_rq(cpu);
8001
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008002 tg->rt_rq[cpu] = rt_rq;
8003 init_rt_rq(rt_rq, rq);
8004 rt_rq->tg = tg;
8005 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008006 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008007 if (add)
8008 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8009
8010 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008011 if (!rt_se)
8012 return;
8013
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008014 if (!parent)
8015 rt_se->rt_rq = &rq->rt;
8016 else
8017 rt_se->rt_rq = parent->my_q;
8018
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008019 rt_se->rt_rq = &rq->rt;
8020 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008021 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008022 INIT_LIST_HEAD(&rt_se->run_list);
8023}
8024#endif
8025
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026void __init sched_init(void)
8027{
Ingo Molnardd41f592007-07-09 18:51:59 +02008028 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008029 unsigned long alloc_size = 0, ptr;
8030
8031#ifdef CONFIG_FAIR_GROUP_SCHED
8032 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8033#endif
8034#ifdef CONFIG_RT_GROUP_SCHED
8035 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8036#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008037#ifdef CONFIG_USER_SCHED
8038 alloc_size *= 2;
8039#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008040 /*
8041 * As sched_init() is called before page_alloc is setup,
8042 * we use alloc_bootmem().
8043 */
8044 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07008045 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07008046
8047#ifdef CONFIG_FAIR_GROUP_SCHED
8048 init_task_group.se = (struct sched_entity **)ptr;
8049 ptr += nr_cpu_ids * sizeof(void **);
8050
8051 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8052 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008053
8054#ifdef CONFIG_USER_SCHED
8055 root_task_group.se = (struct sched_entity **)ptr;
8056 ptr += nr_cpu_ids * sizeof(void **);
8057
8058 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8059 ptr += nr_cpu_ids * sizeof(void **);
8060#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008061#endif
8062#ifdef CONFIG_RT_GROUP_SCHED
8063 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8064 ptr += nr_cpu_ids * sizeof(void **);
8065
8066 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008067 ptr += nr_cpu_ids * sizeof(void **);
8068
8069#ifdef CONFIG_USER_SCHED
8070 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8071 ptr += nr_cpu_ids * sizeof(void **);
8072
8073 root_task_group.rt_rq = (struct rt_rq **)ptr;
8074 ptr += nr_cpu_ids * sizeof(void **);
8075#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008076#endif
8077 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008078
Gregory Haskins57d885f2008-01-25 21:08:18 +01008079#ifdef CONFIG_SMP
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008080 init_aggregate();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008081 init_defrootdomain();
8082#endif
8083
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008084 init_rt_bandwidth(&def_rt_bandwidth,
8085 global_rt_period(), global_rt_runtime());
8086
8087#ifdef CONFIG_RT_GROUP_SCHED
8088 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8089 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008090#ifdef CONFIG_USER_SCHED
8091 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8092 global_rt_period(), RUNTIME_INF);
8093#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008094#endif
8095
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008096#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008097 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008098 INIT_LIST_HEAD(&init_task_group.children);
8099
8100#ifdef CONFIG_USER_SCHED
8101 INIT_LIST_HEAD(&root_task_group.children);
8102 init_task_group.parent = &root_task_group;
8103 list_add(&init_task_group.siblings, &root_task_group.children);
8104#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008105#endif
8106
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008107 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008108 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109
8110 rq = cpu_rq(i);
8111 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07008112 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07008113 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008114 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008115 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008116#ifdef CONFIG_FAIR_GROUP_SCHED
8117 init_task_group.shares = init_task_group_load;
8118 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008119#ifdef CONFIG_CGROUP_SCHED
8120 /*
8121 * How much cpu bandwidth does init_task_group get?
8122 *
8123 * In case of task-groups formed thr' the cgroup filesystem, it
8124 * gets 100% of the cpu resources in the system. This overall
8125 * system cpu resource is divided among the tasks of
8126 * init_task_group and its child task-groups in a fair manner,
8127 * based on each entity's (task or task-group's) weight
8128 * (se->load.weight).
8129 *
8130 * In other words, if init_task_group has 10 tasks of weight
8131 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8132 * then A0's share of the cpu resource is:
8133 *
8134 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8135 *
8136 * We achieve this by letting init_task_group's tasks sit
8137 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8138 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008139 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008140#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008141 root_task_group.shares = NICE_0_LOAD;
8142 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008143 /*
8144 * In case of task-groups formed thr' the user id of tasks,
8145 * init_task_group represents tasks belonging to root user.
8146 * Hence it forms a sibling of all subsequent groups formed.
8147 * In this case, init_task_group gets only a fraction of overall
8148 * system cpu resource, based on the weight assigned to root
8149 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8150 * by letting tasks of init_task_group sit in a separate cfs_rq
8151 * (init_cfs_rq) and having one entity represent this group of
8152 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8153 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008154 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008155 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008156 &per_cpu(init_sched_entity, i), i, 1,
8157 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008158
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008159#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008160#endif /* CONFIG_FAIR_GROUP_SCHED */
8161
8162 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008163#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008164 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008165#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008166 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008167#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008168 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008169 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008170 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008171 &per_cpu(init_sched_rt_entity, i), i, 1,
8172 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008173#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175
Ingo Molnardd41f592007-07-09 18:51:59 +02008176 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8177 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008179 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008180 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008182 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008184 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 rq->migration_thread = NULL;
8186 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008187 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008189 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191 }
8192
Peter Williams2dd73a42006-06-27 02:54:34 -07008193 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008194
Avi Kivitye107be32007-07-26 13:40:43 +02008195#ifdef CONFIG_PREEMPT_NOTIFIERS
8196 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8197#endif
8198
Christoph Lameterc9819f42006-12-10 02:20:25 -08008199#ifdef CONFIG_SMP
8200 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8201#endif
8202
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008203#ifdef CONFIG_RT_MUTEXES
8204 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8205#endif
8206
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207 /*
8208 * The boot idle thread does lazy MMU switching as well:
8209 */
8210 atomic_inc(&init_mm.mm_count);
8211 enter_lazy_tlb(&init_mm, current);
8212
8213 /*
8214 * Make us the idle thread. Technically, schedule() should not be
8215 * called from this thread, however somewhere below it might be,
8216 * but because we are the idle thread, we just pick up running again
8217 * when this runqueue becomes "idle".
8218 */
8219 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008220 /*
8221 * During early bootup we pretend to be a normal task:
8222 */
8223 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008224
8225 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008226}
8227
8228#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8229void __might_sleep(char *file, int line)
8230{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008231#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008232 static unsigned long prev_jiffy; /* ratelimiting */
8233
8234 if ((in_atomic() || irqs_disabled()) &&
8235 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8236 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8237 return;
8238 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008239 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240 " context at %s:%d\n", file, line);
8241 printk("in_atomic():%d, irqs_disabled():%d\n",
8242 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008243 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008244 if (irqs_disabled())
8245 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246 dump_stack();
8247 }
8248#endif
8249}
8250EXPORT_SYMBOL(__might_sleep);
8251#endif
8252
8253#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008254static void normalize_task(struct rq *rq, struct task_struct *p)
8255{
8256 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02008257
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008258 update_rq_clock(rq);
8259 on_rq = p->se.on_rq;
8260 if (on_rq)
8261 deactivate_task(rq, p, 0);
8262 __setscheduler(rq, p, SCHED_NORMAL, 0);
8263 if (on_rq) {
8264 activate_task(rq, p, 0);
8265 resched_task(rq->curr);
8266 }
8267}
8268
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269void normalize_rt_tasks(void)
8270{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008271 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008273 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008275 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008276 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008277 /*
8278 * Only normalize user tasks:
8279 */
8280 if (!p->mm)
8281 continue;
8282
Ingo Molnardd41f592007-07-09 18:51:59 +02008283 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008284#ifdef CONFIG_SCHEDSTATS
8285 p->se.wait_start = 0;
8286 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008287 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008288#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008289
8290 if (!rt_task(p)) {
8291 /*
8292 * Renice negative nice level userspace
8293 * tasks back to 0:
8294 */
8295 if (TASK_NICE(p) < 0 && p->mm)
8296 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008300 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008301 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302
Ingo Molnar178be792007-10-15 17:00:18 +02008303 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008304
Ingo Molnarb29739f2006-06-27 02:54:51 -07008305 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008306 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008307 } while_each_thread(g, p);
8308
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008309 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008310}
8311
8312#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008313
8314#ifdef CONFIG_IA64
8315/*
8316 * These functions are only useful for the IA64 MCA handling.
8317 *
8318 * They can only be called when the whole system has been
8319 * stopped - every CPU needs to be quiescent, and no scheduling
8320 * activity can take place. Using them for anything else would
8321 * be a serious bug, and as a result, they aren't even visible
8322 * under any other configuration.
8323 */
8324
8325/**
8326 * curr_task - return the current task for a given cpu.
8327 * @cpu: the processor in question.
8328 *
8329 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8330 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008331struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008332{
8333 return cpu_curr(cpu);
8334}
8335
8336/**
8337 * set_curr_task - set the current task for a given cpu.
8338 * @cpu: the processor in question.
8339 * @p: the task pointer to set.
8340 *
8341 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008342 * are serviced on a separate stack. It allows the architecture to switch the
8343 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008344 * must be called with all CPU's synchronized, and interrupts disabled, the
8345 * and caller must save the original value of the current task (see
8346 * curr_task() above) and restore that value before reenabling interrupts and
8347 * re-starting the system.
8348 *
8349 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8350 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008351void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008352{
8353 cpu_curr(cpu) = p;
8354}
8355
8356#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008357
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008358#ifdef CONFIG_FAIR_GROUP_SCHED
8359static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008360{
8361 int i;
8362
8363 for_each_possible_cpu(i) {
8364 if (tg->cfs_rq)
8365 kfree(tg->cfs_rq[i]);
8366 if (tg->se)
8367 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008368 }
8369
8370 kfree(tg->cfs_rq);
8371 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008372}
8373
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008374static
8375int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008376{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008377 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008378 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008379 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008380 int i;
8381
Mike Travis434d53b2008-04-04 18:11:04 -07008382 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008383 if (!tg->cfs_rq)
8384 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008385 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008386 if (!tg->se)
8387 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008388
8389 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008390
8391 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008392 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008393
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008394 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8395 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008396 if (!cfs_rq)
8397 goto err;
8398
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008399 se = kmalloc_node(sizeof(struct sched_entity),
8400 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008401 if (!se)
8402 goto err;
8403
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008404 parent_se = parent ? parent->se[i] : NULL;
8405 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008406 }
8407
8408 return 1;
8409
8410 err:
8411 return 0;
8412}
8413
8414static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8415{
8416 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8417 &cpu_rq(cpu)->leaf_cfs_rq_list);
8418}
8419
8420static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8421{
8422 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8423}
8424#else
8425static inline void free_fair_sched_group(struct task_group *tg)
8426{
8427}
8428
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008429static inline
8430int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008431{
8432 return 1;
8433}
8434
8435static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8436{
8437}
8438
8439static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8440{
8441}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008442#endif
8443
8444#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008445static void free_rt_sched_group(struct task_group *tg)
8446{
8447 int i;
8448
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008449 destroy_rt_bandwidth(&tg->rt_bandwidth);
8450
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008451 for_each_possible_cpu(i) {
8452 if (tg->rt_rq)
8453 kfree(tg->rt_rq[i]);
8454 if (tg->rt_se)
8455 kfree(tg->rt_se[i]);
8456 }
8457
8458 kfree(tg->rt_rq);
8459 kfree(tg->rt_se);
8460}
8461
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008462static
8463int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008464{
8465 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008466 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008467 struct rq *rq;
8468 int i;
8469
Mike Travis434d53b2008-04-04 18:11:04 -07008470 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008471 if (!tg->rt_rq)
8472 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008473 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008474 if (!tg->rt_se)
8475 goto err;
8476
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008477 init_rt_bandwidth(&tg->rt_bandwidth,
8478 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008479
8480 for_each_possible_cpu(i) {
8481 rq = cpu_rq(i);
8482
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008483 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8484 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8485 if (!rt_rq)
8486 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008487
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008488 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8489 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8490 if (!rt_se)
8491 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008492
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008493 parent_se = parent ? parent->rt_se[i] : NULL;
8494 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008495 }
8496
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008497 return 1;
8498
8499 err:
8500 return 0;
8501}
8502
8503static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8504{
8505 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8506 &cpu_rq(cpu)->leaf_rt_rq_list);
8507}
8508
8509static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8510{
8511 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8512}
8513#else
8514static inline void free_rt_sched_group(struct task_group *tg)
8515{
8516}
8517
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008518static inline
8519int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008520{
8521 return 1;
8522}
8523
8524static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8525{
8526}
8527
8528static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8529{
8530}
8531#endif
8532
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008533#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008534static void free_sched_group(struct task_group *tg)
8535{
8536 free_fair_sched_group(tg);
8537 free_rt_sched_group(tg);
8538 kfree(tg);
8539}
8540
8541/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008542struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008543{
8544 struct task_group *tg;
8545 unsigned long flags;
8546 int i;
8547
8548 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8549 if (!tg)
8550 return ERR_PTR(-ENOMEM);
8551
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008552 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008553 goto err;
8554
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008555 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008556 goto err;
8557
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008558 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008559 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008560 register_fair_sched_group(tg, i);
8561 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008562 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008563 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008564
8565 WARN_ON(!parent); /* root should already exist */
8566
8567 tg->parent = parent;
8568 list_add_rcu(&tg->siblings, &parent->children);
8569 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008570 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008571
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008572 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008573
8574err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008575 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008576 return ERR_PTR(-ENOMEM);
8577}
8578
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008579/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008580static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008581{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008582 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008583 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008584}
8585
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008586/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008587void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008588{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008589 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008590 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008591
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008592 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008593 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008594 unregister_fair_sched_group(tg, i);
8595 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008596 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008597 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008598 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008599 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008600
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008601 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008602 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008603}
8604
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008605/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008606 * The caller of this function should have put the task in its new group
8607 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8608 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008609 */
8610void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008611{
8612 int on_rq, running;
8613 unsigned long flags;
8614 struct rq *rq;
8615
8616 rq = task_rq_lock(tsk, &flags);
8617
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008618 update_rq_clock(rq);
8619
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008620 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008621 on_rq = tsk->se.on_rq;
8622
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008623 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008624 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008625 if (unlikely(running))
8626 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008627
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008628 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008629
Peter Zijlstra810b3812008-02-29 15:21:01 -05008630#ifdef CONFIG_FAIR_GROUP_SCHED
8631 if (tsk->sched_class->moved_group)
8632 tsk->sched_class->moved_group(tsk);
8633#endif
8634
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008635 if (unlikely(running))
8636 tsk->sched_class->set_curr_task(rq);
8637 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008638 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008639
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008640 task_rq_unlock(rq, &flags);
8641}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008642#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008643
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008644#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008645static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008646{
8647 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008648 int on_rq;
8649
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008650 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008651 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008652 dequeue_entity(cfs_rq, se, 0);
8653
8654 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008655 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008656
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008657 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008658 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008659}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008660
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008661static void set_se_shares(struct sched_entity *se, unsigned long shares)
8662{
8663 struct cfs_rq *cfs_rq = se->cfs_rq;
8664 struct rq *rq = cfs_rq->rq;
8665 unsigned long flags;
8666
8667 spin_lock_irqsave(&rq->lock, flags);
8668 __set_se_shares(se, shares);
8669 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008670}
8671
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008672static DEFINE_MUTEX(shares_mutex);
8673
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008674int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008675{
8676 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008677 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008678
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008679 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008680 * We can't change the weight of the root cgroup.
8681 */
8682 if (!tg->se[0])
8683 return -EINVAL;
8684
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008685 if (shares < MIN_SHARES)
8686 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008687 else if (shares > MAX_SHARES)
8688 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008689
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008690 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008691 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008692 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008693
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008694 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008695 for_each_possible_cpu(i)
8696 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008697 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008698 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008699
8700 /* wait for any ongoing reference to this group to finish */
8701 synchronize_sched();
8702
8703 /*
8704 * Now we are free to modify the group's share on each cpu
8705 * w/o tripping rebalance_share or load_balance_fair.
8706 */
8707 tg->shares = shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008708 for_each_possible_cpu(i) {
8709 /*
8710 * force a rebalance
8711 */
8712 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008713 set_se_shares(tg->se[i], shares);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008714 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008715
8716 /*
8717 * Enable load balance activity on this group, by inserting it back on
8718 * each cpu's rq->leaf_cfs_rq_list.
8719 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008720 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008721 for_each_possible_cpu(i)
8722 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008723 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008724 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008725done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008726 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008727 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008728}
8729
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008730unsigned long sched_group_shares(struct task_group *tg)
8731{
8732 return tg->shares;
8733}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008734#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008735
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008736#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008737/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008738 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008739 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008740static DEFINE_MUTEX(rt_constraints_mutex);
8741
8742static unsigned long to_ratio(u64 period, u64 runtime)
8743{
8744 if (runtime == RUNTIME_INF)
8745 return 1ULL << 16;
8746
Roman Zippel6f6d6a12008-05-01 04:34:28 -07008747 return div64_u64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008748}
8749
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008750#ifdef CONFIG_CGROUP_SCHED
8751static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8752{
8753 struct task_group *tgi, *parent = tg->parent;
8754 unsigned long total = 0;
8755
8756 if (!parent) {
8757 if (global_rt_period() < period)
8758 return 0;
8759
8760 return to_ratio(period, runtime) <
8761 to_ratio(global_rt_period(), global_rt_runtime());
8762 }
8763
8764 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8765 return 0;
8766
8767 rcu_read_lock();
8768 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8769 if (tgi == tg)
8770 continue;
8771
8772 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8773 tgi->rt_bandwidth.rt_runtime);
8774 }
8775 rcu_read_unlock();
8776
8777 return total + to_ratio(period, runtime) <
8778 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8779 parent->rt_bandwidth.rt_runtime);
8780}
8781#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008782static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008783{
8784 struct task_group *tgi;
8785 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008786 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008787 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008788
8789 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008790 list_for_each_entry_rcu(tgi, &task_groups, list) {
8791 if (tgi == tg)
8792 continue;
8793
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008794 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8795 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008796 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008797 rcu_read_unlock();
8798
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008799 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008800}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008801#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008802
Dhaval Giani521f1a242008-02-28 15:21:56 +05308803/* Must be called with tasklist_lock held */
8804static inline int tg_has_rt_tasks(struct task_group *tg)
8805{
8806 struct task_struct *g, *p;
8807 do_each_thread(g, p) {
8808 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8809 return 1;
8810 } while_each_thread(g, p);
8811 return 0;
8812}
8813
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008814static int tg_set_bandwidth(struct task_group *tg,
8815 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008816{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008817 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008818
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008819 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308820 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008821 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308822 err = -EBUSY;
8823 goto unlock;
8824 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008825 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8826 err = -EINVAL;
8827 goto unlock;
8828 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008829
8830 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008831 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8832 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008833
8834 for_each_possible_cpu(i) {
8835 struct rt_rq *rt_rq = tg->rt_rq[i];
8836
8837 spin_lock(&rt_rq->rt_runtime_lock);
8838 rt_rq->rt_runtime = rt_runtime;
8839 spin_unlock(&rt_rq->rt_runtime_lock);
8840 }
8841 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008842 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308843 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008844 mutex_unlock(&rt_constraints_mutex);
8845
8846 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008847}
8848
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008849int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8850{
8851 u64 rt_runtime, rt_period;
8852
8853 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8854 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8855 if (rt_runtime_us < 0)
8856 rt_runtime = RUNTIME_INF;
8857
8858 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8859}
8860
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008861long sched_group_rt_runtime(struct task_group *tg)
8862{
8863 u64 rt_runtime_us;
8864
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008865 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008866 return -1;
8867
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008868 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008869 do_div(rt_runtime_us, NSEC_PER_USEC);
8870 return rt_runtime_us;
8871}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008872
8873int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8874{
8875 u64 rt_runtime, rt_period;
8876
8877 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8878 rt_runtime = tg->rt_bandwidth.rt_runtime;
8879
8880 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8881}
8882
8883long sched_group_rt_period(struct task_group *tg)
8884{
8885 u64 rt_period_us;
8886
8887 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8888 do_div(rt_period_us, NSEC_PER_USEC);
8889 return rt_period_us;
8890}
8891
8892static int sched_rt_global_constraints(void)
8893{
8894 int ret = 0;
8895
8896 mutex_lock(&rt_constraints_mutex);
8897 if (!__rt_schedulable(NULL, 1, 0))
8898 ret = -EINVAL;
8899 mutex_unlock(&rt_constraints_mutex);
8900
8901 return ret;
8902}
8903#else
8904static int sched_rt_global_constraints(void)
8905{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008906 unsigned long flags;
8907 int i;
8908
8909 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
8910 for_each_possible_cpu(i) {
8911 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8912
8913 spin_lock(&rt_rq->rt_runtime_lock);
8914 rt_rq->rt_runtime = global_rt_runtime();
8915 spin_unlock(&rt_rq->rt_runtime_lock);
8916 }
8917 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
8918
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008919 return 0;
8920}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008921#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008922
8923int sched_rt_handler(struct ctl_table *table, int write,
8924 struct file *filp, void __user *buffer, size_t *lenp,
8925 loff_t *ppos)
8926{
8927 int ret;
8928 int old_period, old_runtime;
8929 static DEFINE_MUTEX(mutex);
8930
8931 mutex_lock(&mutex);
8932 old_period = sysctl_sched_rt_period;
8933 old_runtime = sysctl_sched_rt_runtime;
8934
8935 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8936
8937 if (!ret && write) {
8938 ret = sched_rt_global_constraints();
8939 if (ret) {
8940 sysctl_sched_rt_period = old_period;
8941 sysctl_sched_rt_runtime = old_runtime;
8942 } else {
8943 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8944 def_rt_bandwidth.rt_period =
8945 ns_to_ktime(global_rt_period());
8946 }
8947 }
8948 mutex_unlock(&mutex);
8949
8950 return ret;
8951}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008952
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008953#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008954
8955/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008956static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008957{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008958 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8959 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008960}
8961
8962static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008963cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008964{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008965 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008966
Paul Menage2b01dfe2007-10-24 18:23:50 +02008967 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008968 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008969 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008970 return &init_task_group.css;
8971 }
8972
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008973 parent = cgroup_tg(cgrp->parent);
8974 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008975 if (IS_ERR(tg))
8976 return ERR_PTR(-ENOMEM);
8977
8978 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008979 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008980
8981 return &tg->css;
8982}
8983
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008984static void
8985cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008986{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008987 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008988
8989 sched_destroy_group(tg);
8990}
8991
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008992static int
8993cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8994 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008995{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008996#ifdef CONFIG_RT_GROUP_SCHED
8997 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008998 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008999 return -EINVAL;
9000#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009001 /* We don't support RT-tasks being in separate groups */
9002 if (tsk->sched_class != &fair_sched_class)
9003 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009004#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009005
9006 return 0;
9007}
9008
9009static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009010cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009011 struct cgroup *old_cont, struct task_struct *tsk)
9012{
9013 sched_move_task(tsk);
9014}
9015
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009016#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009017static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009018 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009019{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009020 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009021}
9022
Paul Menagef4c753b2008-04-29 00:59:56 -07009023static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009024{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009025 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009026
9027 return (u64) tg->shares;
9028}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009029#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009030
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009031#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07009032static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009033 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009034{
Paul Menage06ecb272008-04-29 01:00:06 -07009035 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009036}
9037
Paul Menage06ecb272008-04-29 01:00:06 -07009038static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009039{
Paul Menage06ecb272008-04-29 01:00:06 -07009040 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009041}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009042
9043static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9044 u64 rt_period_us)
9045{
9046 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9047}
9048
9049static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9050{
9051 return sched_group_rt_period(cgroup_tg(cgrp));
9052}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009053#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009054
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009055static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009056#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009057 {
9058 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009059 .read_u64 = cpu_shares_read_u64,
9060 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009061 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009062#endif
9063#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009064 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009065 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009066 .read_s64 = cpu_rt_runtime_read,
9067 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009068 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009069 {
9070 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009071 .read_u64 = cpu_rt_period_read_uint,
9072 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009073 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009074#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009075};
9076
9077static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9078{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009079 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009080}
9081
9082struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009083 .name = "cpu",
9084 .create = cpu_cgroup_create,
9085 .destroy = cpu_cgroup_destroy,
9086 .can_attach = cpu_cgroup_can_attach,
9087 .attach = cpu_cgroup_attach,
9088 .populate = cpu_cgroup_populate,
9089 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009090 .early_init = 1,
9091};
9092
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009093#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009094
9095#ifdef CONFIG_CGROUP_CPUACCT
9096
9097/*
9098 * CPU accounting code for task groups.
9099 *
9100 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9101 * (balbir@in.ibm.com).
9102 */
9103
9104/* track cpu usage of a group of tasks */
9105struct cpuacct {
9106 struct cgroup_subsys_state css;
9107 /* cpuusage holds pointer to a u64-type object on every cpu */
9108 u64 *cpuusage;
9109};
9110
9111struct cgroup_subsys cpuacct_subsys;
9112
9113/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309114static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009115{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309116 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009117 struct cpuacct, css);
9118}
9119
9120/* return cpu accounting group to which this task belongs */
9121static inline struct cpuacct *task_ca(struct task_struct *tsk)
9122{
9123 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9124 struct cpuacct, css);
9125}
9126
9127/* create a new cpu accounting group */
9128static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309129 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009130{
9131 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9132
9133 if (!ca)
9134 return ERR_PTR(-ENOMEM);
9135
9136 ca->cpuusage = alloc_percpu(u64);
9137 if (!ca->cpuusage) {
9138 kfree(ca);
9139 return ERR_PTR(-ENOMEM);
9140 }
9141
9142 return &ca->css;
9143}
9144
9145/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009146static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309147cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009148{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309149 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009150
9151 free_percpu(ca->cpuusage);
9152 kfree(ca);
9153}
9154
9155/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309156static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009157{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309158 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009159 u64 totalcpuusage = 0;
9160 int i;
9161
9162 for_each_possible_cpu(i) {
9163 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9164
9165 /*
9166 * Take rq->lock to make 64-bit addition safe on 32-bit
9167 * platforms.
9168 */
9169 spin_lock_irq(&cpu_rq(i)->lock);
9170 totalcpuusage += *cpuusage;
9171 spin_unlock_irq(&cpu_rq(i)->lock);
9172 }
9173
9174 return totalcpuusage;
9175}
9176
Dhaval Giani0297b802008-02-29 10:02:44 +05309177static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9178 u64 reset)
9179{
9180 struct cpuacct *ca = cgroup_ca(cgrp);
9181 int err = 0;
9182 int i;
9183
9184 if (reset) {
9185 err = -EINVAL;
9186 goto out;
9187 }
9188
9189 for_each_possible_cpu(i) {
9190 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9191
9192 spin_lock_irq(&cpu_rq(i)->lock);
9193 *cpuusage = 0;
9194 spin_unlock_irq(&cpu_rq(i)->lock);
9195 }
9196out:
9197 return err;
9198}
9199
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009200static struct cftype files[] = {
9201 {
9202 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009203 .read_u64 = cpuusage_read,
9204 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009205 },
9206};
9207
Dhaval Giani32cd7562008-02-29 10:02:43 +05309208static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009209{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309210 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009211}
9212
9213/*
9214 * charge this task's execution time to its accounting group.
9215 *
9216 * called with rq->lock held.
9217 */
9218static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9219{
9220 struct cpuacct *ca;
9221
9222 if (!cpuacct_subsys.active)
9223 return;
9224
9225 ca = task_ca(tsk);
9226 if (ca) {
9227 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9228
9229 *cpuusage += cputime;
9230 }
9231}
9232
9233struct cgroup_subsys cpuacct_subsys = {
9234 .name = "cpuacct",
9235 .create = cpuacct_create,
9236 .destroy = cpuacct_destroy,
9237 .populate = cpuacct_populate,
9238 .subsys_id = cpuacct_subsys_id,
9239};
9240#endif /* CONFIG_CGROUP_CPUACCT */