blob: 673b588b713bcf5deefbaf8d5bd526d7416b4a57 [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
645/*
646 * Debugging: various feature bits
647 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200648
649#define SCHED_FEAT(name, enabled) \
650 __SCHED_FEAT_##name ,
651
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200652enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200653#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200654};
655
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200656#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200657
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200658#define SCHED_FEAT(name, enabled) \
659 (1UL << __SCHED_FEAT_##name) * enabled |
660
661const_debug unsigned int sysctl_sched_features =
662#include "sched_features.h"
663 0;
664
665#undef SCHED_FEAT
666
667#ifdef CONFIG_SCHED_DEBUG
668#define SCHED_FEAT(name, enabled) \
669 #name ,
670
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700671static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200672#include "sched_features.h"
673 NULL
674};
675
676#undef SCHED_FEAT
677
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700678static int sched_feat_open(struct inode *inode, struct file *filp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200679{
680 filp->private_data = inode->i_private;
681 return 0;
682}
683
684static ssize_t
685sched_feat_read(struct file *filp, char __user *ubuf,
686 size_t cnt, loff_t *ppos)
687{
688 char *buf;
689 int r = 0;
690 int len = 0;
691 int i;
692
693 for (i = 0; sched_feat_names[i]; i++) {
694 len += strlen(sched_feat_names[i]);
695 len += 4;
696 }
697
698 buf = kmalloc(len + 2, GFP_KERNEL);
699 if (!buf)
700 return -ENOMEM;
701
702 for (i = 0; sched_feat_names[i]; i++) {
703 if (sysctl_sched_features & (1UL << i))
704 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
705 else
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200706 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200707 }
708
709 r += sprintf(buf + r, "\n");
710 WARN_ON(r >= len + 2);
711
712 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
713
714 kfree(buf);
715
716 return r;
717}
718
719static ssize_t
720sched_feat_write(struct file *filp, const char __user *ubuf,
721 size_t cnt, loff_t *ppos)
722{
723 char buf[64];
724 char *cmp = buf;
725 int neg = 0;
726 int i;
727
728 if (cnt > 63)
729 cnt = 63;
730
731 if (copy_from_user(&buf, ubuf, cnt))
732 return -EFAULT;
733
734 buf[cnt] = 0;
735
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200736 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200737 neg = 1;
738 cmp += 3;
739 }
740
741 for (i = 0; sched_feat_names[i]; i++) {
742 int len = strlen(sched_feat_names[i]);
743
744 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
745 if (neg)
746 sysctl_sched_features &= ~(1UL << i);
747 else
748 sysctl_sched_features |= (1UL << i);
749 break;
750 }
751 }
752
753 if (!sched_feat_names[i])
754 return -EINVAL;
755
756 filp->f_pos += cnt;
757
758 return cnt;
759}
760
761static struct file_operations sched_feat_fops = {
762 .open = sched_feat_open,
763 .read = sched_feat_read,
764 .write = sched_feat_write,
765};
766
767static __init int sched_init_debug(void)
768{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200769 debugfs_create_file("sched_features", 0644, NULL, NULL,
770 &sched_feat_fops);
771
772 return 0;
773}
774late_initcall(sched_init_debug);
775
776#endif
777
778#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200779
780/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100781 * Number of tasks to iterate in a single balance run.
782 * Limited because this is done with IRQs disabled.
783 */
784const_debug unsigned int sysctl_sched_nr_migrate = 32;
785
786/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100787 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100788 * default: 1s
789 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100790unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100791
Ingo Molnar6892b752008-02-13 14:02:36 +0100792static __read_mostly int scheduler_running;
793
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100794/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100795 * part of the period that we allow rt tasks to run in us.
796 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100797 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100798int sysctl_sched_rt_runtime = 950000;
799
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200800static inline u64 global_rt_period(void)
801{
802 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
803}
804
805static inline u64 global_rt_runtime(void)
806{
807 if (sysctl_sched_rt_period < 0)
808 return RUNTIME_INF;
809
810 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
811}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100812
Ingo Molnar690229a2008-04-23 09:31:35 +0200813unsigned long long time_sync_thresh = 100000;
Ingo Molnar27ec4402008-02-28 21:00:21 +0100814
815static DEFINE_PER_CPU(unsigned long long, time_offset);
816static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
817
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100818/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100819 * Global lock which we take every now and then to synchronize
820 * the CPUs time. This method is not warp-safe, but it's good
821 * enough to synchronize slowly diverging time sources and thus
822 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200823 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100824static DEFINE_SPINLOCK(time_sync_lock);
825static unsigned long long prev_global_time;
826
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200827static unsigned long long __sync_cpu_clock(unsigned long long time, int cpu)
Ingo Molnar27ec4402008-02-28 21:00:21 +0100828{
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200829 /*
830 * We want this inlined, to not get tracer function calls
831 * in this critical section:
832 */
833 spin_acquire(&time_sync_lock.dep_map, 0, 0, _THIS_IP_);
834 __raw_spin_lock(&time_sync_lock.raw_lock);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100835
836 if (time < prev_global_time) {
837 per_cpu(time_offset, cpu) += prev_global_time - time;
838 time = prev_global_time;
839 } else {
840 prev_global_time = time;
841 }
842
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200843 __raw_spin_unlock(&time_sync_lock.raw_lock);
844 spin_release(&time_sync_lock.dep_map, 1, _THIS_IP_);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100845
846 return time;
847}
848
849static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200850{
Ingo Molnare436d802007-07-19 21:28:35 +0200851 unsigned long long now;
Ingo Molnare436d802007-07-19 21:28:35 +0200852
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100853 /*
854 * Only call sched_clock() if the scheduler has already been
855 * initialized (some code might call cpu_clock() very early):
856 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100857 if (unlikely(!scheduler_running))
858 return 0;
859
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200860 now = sched_clock_cpu(cpu);
Ingo Molnare436d802007-07-19 21:28:35 +0200861
862 return now;
863}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100864
865/*
866 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
867 * clock constructed from sched_clock():
868 */
869unsigned long long cpu_clock(int cpu)
870{
871 unsigned long long prev_cpu_time, time, delta_time;
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200872 unsigned long flags;
Ingo Molnar27ec4402008-02-28 21:00:21 +0100873
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200874 local_irq_save(flags);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100875 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
876 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
877 delta_time = time-prev_cpu_time;
878
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200879 if (unlikely(delta_time > time_sync_thresh)) {
Ingo Molnar27ec4402008-02-28 21:00:21 +0100880 time = __sync_cpu_clock(time, cpu);
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200881 per_cpu(prev_cpu_time, cpu) = time;
882 }
883 local_irq_restore(flags);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100884
885 return time;
886}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200887EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700890# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700892#ifndef finish_arch_switch
893# define finish_arch_switch(prev) do { } while (0)
894#endif
895
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100896static inline int task_current(struct rq *rq, struct task_struct *p)
897{
898 return rq->curr == p;
899}
900
Nick Piggin4866cde2005-06-25 14:57:23 -0700901#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700902static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700903{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100904 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700905}
906
Ingo Molnar70b97a72006-07-03 00:25:42 -0700907static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700908{
909}
910
Ingo Molnar70b97a72006-07-03 00:25:42 -0700911static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700912{
Ingo Molnarda04c032005-09-13 11:17:59 +0200913#ifdef CONFIG_DEBUG_SPINLOCK
914 /* this is a valid case when another task releases the spinlock */
915 rq->lock.owner = current;
916#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700917 /*
918 * If we are tracking spinlock dependencies then we have to
919 * fix up the runqueue lock - which gets 'carried over' from
920 * prev into current:
921 */
922 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
923
Nick Piggin4866cde2005-06-25 14:57:23 -0700924 spin_unlock_irq(&rq->lock);
925}
926
927#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700928static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700929{
930#ifdef CONFIG_SMP
931 return p->oncpu;
932#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100933 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700934#endif
935}
936
Ingo Molnar70b97a72006-07-03 00:25:42 -0700937static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700938{
939#ifdef CONFIG_SMP
940 /*
941 * We can optimise this out completely for !SMP, because the
942 * SMP rebalancing from interrupt is the only thing that cares
943 * here.
944 */
945 next->oncpu = 1;
946#endif
947#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
948 spin_unlock_irq(&rq->lock);
949#else
950 spin_unlock(&rq->lock);
951#endif
952}
953
Ingo Molnar70b97a72006-07-03 00:25:42 -0700954static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700955{
956#ifdef CONFIG_SMP
957 /*
958 * After ->oncpu is cleared, the task can be moved to a different CPU.
959 * We must ensure this doesn't happen until the switch is completely
960 * finished.
961 */
962 smp_wmb();
963 prev->oncpu = 0;
964#endif
965#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
966 local_irq_enable();
967#endif
968}
969#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700972 * __task_rq_lock - lock the runqueue a given task resides on.
973 * Must be called interrupts disabled.
974 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700975static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700976 __acquires(rq->lock)
977{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200978 for (;;) {
979 struct rq *rq = task_rq(p);
980 spin_lock(&rq->lock);
981 if (likely(rq == task_rq(p)))
982 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700983 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700984 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700985}
986
987/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100989 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 * explicitly disabling preemption.
991 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700992static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 __acquires(rq->lock)
994{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700995 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Andi Kleen3a5c3592007-10-15 17:00:14 +0200997 for (;;) {
998 local_irq_save(*flags);
999 rq = task_rq(p);
1000 spin_lock(&rq->lock);
1001 if (likely(rq == task_rq(p)))
1002 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
Alexey Dobriyana9957442007-10-15 17:00:13 +02001007static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001008 __releases(rq->lock)
1009{
1010 spin_unlock(&rq->lock);
1011}
1012
Ingo Molnar70b97a72006-07-03 00:25:42 -07001013static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 __releases(rq->lock)
1015{
1016 spin_unlock_irqrestore(&rq->lock, *flags);
1017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001020 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001022static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 __acquires(rq->lock)
1024{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001025 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 local_irq_disable();
1028 rq = this_rq();
1029 spin_lock(&rq->lock);
1030
1031 return rq;
1032}
1033
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001034static void __resched_task(struct task_struct *p, int tif_bit);
1035
1036static inline void resched_task(struct task_struct *p)
1037{
1038 __resched_task(p, TIF_NEED_RESCHED);
1039}
1040
1041#ifdef CONFIG_SCHED_HRTICK
1042/*
1043 * Use HR-timers to deliver accurate preemption points.
1044 *
1045 * Its all a bit involved since we cannot program an hrt while holding the
1046 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1047 * reschedule event.
1048 *
1049 * When we get rescheduled we reprogram the hrtick_timer outside of the
1050 * rq->lock.
1051 */
1052static inline void resched_hrt(struct task_struct *p)
1053{
1054 __resched_task(p, TIF_HRTICK_RESCHED);
1055}
1056
1057static inline void resched_rq(struct rq *rq)
1058{
1059 unsigned long flags;
1060
1061 spin_lock_irqsave(&rq->lock, flags);
1062 resched_task(rq->curr);
1063 spin_unlock_irqrestore(&rq->lock, flags);
1064}
1065
1066enum {
1067 HRTICK_SET, /* re-programm hrtick_timer */
1068 HRTICK_RESET, /* not a new slice */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001069 HRTICK_BLOCK, /* stop hrtick operations */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001070};
1071
1072/*
1073 * Use hrtick when:
1074 * - enabled by features
1075 * - hrtimer is actually high res
1076 */
1077static inline int hrtick_enabled(struct rq *rq)
1078{
1079 if (!sched_feat(HRTICK))
1080 return 0;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001081 if (unlikely(test_bit(HRTICK_BLOCK, &rq->hrtick_flags)))
1082 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001083 return hrtimer_is_hres_active(&rq->hrtick_timer);
1084}
1085
1086/*
1087 * Called to set the hrtick timer state.
1088 *
1089 * called with rq->lock held and irqs disabled
1090 */
1091static void hrtick_start(struct rq *rq, u64 delay, int reset)
1092{
1093 assert_spin_locked(&rq->lock);
1094
1095 /*
1096 * preempt at: now + delay
1097 */
1098 rq->hrtick_expire =
1099 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1100 /*
1101 * indicate we need to program the timer
1102 */
1103 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1104 if (reset)
1105 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1106
1107 /*
1108 * New slices are called from the schedule path and don't need a
1109 * forced reschedule.
1110 */
1111 if (reset)
1112 resched_hrt(rq->curr);
1113}
1114
1115static void hrtick_clear(struct rq *rq)
1116{
1117 if (hrtimer_active(&rq->hrtick_timer))
1118 hrtimer_cancel(&rq->hrtick_timer);
1119}
1120
1121/*
1122 * Update the timer from the possible pending state.
1123 */
1124static void hrtick_set(struct rq *rq)
1125{
1126 ktime_t time;
1127 int set, reset;
1128 unsigned long flags;
1129
1130 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1131
1132 spin_lock_irqsave(&rq->lock, flags);
1133 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1134 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1135 time = rq->hrtick_expire;
1136 clear_thread_flag(TIF_HRTICK_RESCHED);
1137 spin_unlock_irqrestore(&rq->lock, flags);
1138
1139 if (set) {
1140 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1141 if (reset && !hrtimer_active(&rq->hrtick_timer))
1142 resched_rq(rq);
1143 } else
1144 hrtick_clear(rq);
1145}
1146
1147/*
1148 * High-resolution timer tick.
1149 * Runs from hardirq context with interrupts disabled.
1150 */
1151static enum hrtimer_restart hrtick(struct hrtimer *timer)
1152{
1153 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1154
1155 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1156
1157 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001158 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001159 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1160 spin_unlock(&rq->lock);
1161
1162 return HRTIMER_NORESTART;
1163}
1164
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001165static void hotplug_hrtick_disable(int cpu)
1166{
1167 struct rq *rq = cpu_rq(cpu);
1168 unsigned long flags;
1169
1170 spin_lock_irqsave(&rq->lock, flags);
1171 rq->hrtick_flags = 0;
1172 __set_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1173 spin_unlock_irqrestore(&rq->lock, flags);
1174
1175 hrtick_clear(rq);
1176}
1177
1178static void hotplug_hrtick_enable(int cpu)
1179{
1180 struct rq *rq = cpu_rq(cpu);
1181 unsigned long flags;
1182
1183 spin_lock_irqsave(&rq->lock, flags);
1184 __clear_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1185 spin_unlock_irqrestore(&rq->lock, flags);
1186}
1187
1188static int
1189hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1190{
1191 int cpu = (int)(long)hcpu;
1192
1193 switch (action) {
1194 case CPU_UP_CANCELED:
1195 case CPU_UP_CANCELED_FROZEN:
1196 case CPU_DOWN_PREPARE:
1197 case CPU_DOWN_PREPARE_FROZEN:
1198 case CPU_DEAD:
1199 case CPU_DEAD_FROZEN:
1200 hotplug_hrtick_disable(cpu);
1201 return NOTIFY_OK;
1202
1203 case CPU_UP_PREPARE:
1204 case CPU_UP_PREPARE_FROZEN:
1205 case CPU_DOWN_FAILED:
1206 case CPU_DOWN_FAILED_FROZEN:
1207 case CPU_ONLINE:
1208 case CPU_ONLINE_FROZEN:
1209 hotplug_hrtick_enable(cpu);
1210 return NOTIFY_OK;
1211 }
1212
1213 return NOTIFY_DONE;
1214}
1215
1216static void init_hrtick(void)
1217{
1218 hotcpu_notifier(hotplug_hrtick, 0);
1219}
1220
1221static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001222{
1223 rq->hrtick_flags = 0;
1224 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1225 rq->hrtick_timer.function = hrtick;
1226 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1227}
1228
1229void hrtick_resched(void)
1230{
1231 struct rq *rq;
1232 unsigned long flags;
1233
1234 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1235 return;
1236
1237 local_irq_save(flags);
1238 rq = cpu_rq(smp_processor_id());
1239 hrtick_set(rq);
1240 local_irq_restore(flags);
1241}
1242#else
1243static inline void hrtick_clear(struct rq *rq)
1244{
1245}
1246
1247static inline void hrtick_set(struct rq *rq)
1248{
1249}
1250
1251static inline void init_rq_hrtick(struct rq *rq)
1252{
1253}
1254
1255void hrtick_resched(void)
1256{
1257}
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001258
1259static inline void init_hrtick(void)
1260{
1261}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001262#endif
1263
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001264/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001265 * resched_task - mark a task 'to be rescheduled now'.
1266 *
1267 * On UP this means the setting of the need_resched flag, on SMP it
1268 * might also involve a cross-CPU call to trigger the scheduler on
1269 * the target CPU.
1270 */
1271#ifdef CONFIG_SMP
1272
1273#ifndef tsk_is_polling
1274#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1275#endif
1276
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001277static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001278{
1279 int cpu;
1280
1281 assert_spin_locked(&task_rq(p)->lock);
1282
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001283 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001284 return;
1285
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001286 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001287
1288 cpu = task_cpu(p);
1289 if (cpu == smp_processor_id())
1290 return;
1291
1292 /* NEED_RESCHED must be visible before we test polling */
1293 smp_mb();
1294 if (!tsk_is_polling(p))
1295 smp_send_reschedule(cpu);
1296}
1297
1298static void resched_cpu(int cpu)
1299{
1300 struct rq *rq = cpu_rq(cpu);
1301 unsigned long flags;
1302
1303 if (!spin_trylock_irqsave(&rq->lock, flags))
1304 return;
1305 resched_task(cpu_curr(cpu));
1306 spin_unlock_irqrestore(&rq->lock, flags);
1307}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001308
1309#ifdef CONFIG_NO_HZ
1310/*
1311 * When add_timer_on() enqueues a timer into the timer wheel of an
1312 * idle CPU then this timer might expire before the next timer event
1313 * which is scheduled to wake up that CPU. In case of a completely
1314 * idle system the next event might even be infinite time into the
1315 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1316 * leaves the inner idle loop so the newly added timer is taken into
1317 * account when the CPU goes back to idle and evaluates the timer
1318 * wheel for the next timer event.
1319 */
1320void wake_up_idle_cpu(int cpu)
1321{
1322 struct rq *rq = cpu_rq(cpu);
1323
1324 if (cpu == smp_processor_id())
1325 return;
1326
1327 /*
1328 * This is safe, as this function is called with the timer
1329 * wheel base lock of (cpu) held. When the CPU is on the way
1330 * to idle and has not yet set rq->curr to idle then it will
1331 * be serialized on the timer wheel base lock and take the new
1332 * timer into account automatically.
1333 */
1334 if (rq->curr != rq->idle)
1335 return;
1336
1337 /*
1338 * We can set TIF_RESCHED on the idle task of the other CPU
1339 * lockless. The worst case is that the other CPU runs the
1340 * idle task through an additional NOOP schedule()
1341 */
1342 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1343
1344 /* NEED_RESCHED must be visible before we test polling */
1345 smp_mb();
1346 if (!tsk_is_polling(rq->idle))
1347 smp_send_reschedule(cpu);
1348}
1349#endif
1350
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001351#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001352static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001353{
1354 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001355 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001356}
1357#endif
1358
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001359#if BITS_PER_LONG == 32
1360# define WMULT_CONST (~0UL)
1361#else
1362# define WMULT_CONST (1UL << 32)
1363#endif
1364
1365#define WMULT_SHIFT 32
1366
Ingo Molnar194081e2007-08-09 11:16:51 +02001367/*
1368 * Shift right and round:
1369 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001370#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001371
Peter Zijlstra8f1bc3852008-04-19 19:45:00 +02001372/*
1373 * delta *= weight / lw
1374 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001375static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001376calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1377 struct load_weight *lw)
1378{
1379 u64 tmp;
1380
Peter Zijlstrae05510d2008-05-05 23:56:17 +02001381 if (!lw->inv_weight)
1382 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)/(lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001383
1384 tmp = (u64)delta_exec * weight;
1385 /*
1386 * Check whether we'd overflow the 64-bit multiplication:
1387 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001388 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001389 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001390 WMULT_SHIFT/2);
1391 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001392 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001393
Ingo Molnarecf691d2007-08-02 17:41:40 +02001394 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001395}
1396
Ingo Molnar10919852007-10-15 17:00:04 +02001397static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001398{
1399 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001400 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001401}
1402
Ingo Molnar10919852007-10-15 17:00:04 +02001403static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001404{
1405 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001406 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001407}
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001410 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1411 * of tasks with abnormal "nice" values across CPUs the contribution that
1412 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001413 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001414 * scaled version of the new time slice allocation that they receive on time
1415 * slice expiry etc.
1416 */
1417
Ingo Molnardd41f592007-07-09 18:51:59 +02001418#define WEIGHT_IDLEPRIO 2
1419#define WMULT_IDLEPRIO (1 << 31)
1420
1421/*
1422 * Nice levels are multiplicative, with a gentle 10% change for every
1423 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1424 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1425 * that remained on nice 0.
1426 *
1427 * The "10% effect" is relative and cumulative: from _any_ nice level,
1428 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee62007-07-16 09:46:30 +02001429 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1430 * If a task goes up by ~10% and another task goes down by ~10% then
1431 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001432 */
1433static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001434 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1435 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1436 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1437 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1438 /* 0 */ 1024, 820, 655, 526, 423,
1439 /* 5 */ 335, 272, 215, 172, 137,
1440 /* 10 */ 110, 87, 70, 56, 45,
1441 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001442};
1443
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001444/*
1445 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1446 *
1447 * In cases where the weight does not change often, we can use the
1448 * precalculated inverse to speed up arithmetics by turning divisions
1449 * into multiplications:
1450 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001451static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001452 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1453 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1454 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1455 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1456 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1457 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1458 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1459 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001460};
Peter Williams2dd73a42006-06-27 02:54:34 -07001461
Ingo Molnardd41f592007-07-09 18:51:59 +02001462static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1463
1464/*
1465 * runqueue iterator, to support SMP load-balancing between different
1466 * scheduling classes, without having to expose their internal data
1467 * structures to the load-balancing proper:
1468 */
1469struct rq_iterator {
1470 void *arg;
1471 struct task_struct *(*start)(void *);
1472 struct task_struct *(*next)(void *);
1473};
1474
Peter Williamse1d14842007-10-24 18:23:51 +02001475#ifdef CONFIG_SMP
1476static unsigned long
1477balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1478 unsigned long max_load_move, struct sched_domain *sd,
1479 enum cpu_idle_type idle, int *all_pinned,
1480 int *this_best_prio, struct rq_iterator *iterator);
1481
1482static int
1483iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1484 struct sched_domain *sd, enum cpu_idle_type idle,
1485 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001486#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001487
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001488#ifdef CONFIG_CGROUP_CPUACCT
1489static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1490#else
1491static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1492#endif
1493
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001494static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1495{
1496 update_load_add(&rq->load, load);
1497}
1498
1499static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1500{
1501 update_load_sub(&rq->load, load);
1502}
1503
Gregory Haskinse7693a32008-01-25 21:08:09 +01001504#ifdef CONFIG_SMP
1505static unsigned long source_load(int cpu, int type);
1506static unsigned long target_load(int cpu, int type);
1507static unsigned long cpu_avg_load_per_task(int cpu);
1508static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001509
1510#ifdef CONFIG_FAIR_GROUP_SCHED
1511
1512/*
1513 * Group load balancing.
1514 *
1515 * We calculate a few balance domain wide aggregate numbers; load and weight.
1516 * Given the pictures below, and assuming each item has equal weight:
1517 *
1518 * root 1 - thread
1519 * / | \ A - group
1520 * A 1 B
1521 * /|\ / \
1522 * C 2 D 3 4
1523 * | |
1524 * 5 6
1525 *
1526 * load:
1527 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1528 * which equals 1/9-th of the total load.
1529 *
1530 * shares:
1531 * The weight of this group on the selected cpus.
1532 *
1533 * rq_weight:
1534 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1535 * B would get 2.
1536 *
1537 * task_weight:
1538 * Part of the rq_weight contributed by tasks; all groups except B would
1539 * get 1, B gets 2.
1540 */
1541
1542static inline struct aggregate_struct *
1543aggregate(struct task_group *tg, struct sched_domain *sd)
1544{
1545 return &tg->cfs_rq[sd->first_cpu]->aggregate;
1546}
1547
1548typedef void (*aggregate_func)(struct task_group *, struct sched_domain *);
1549
1550/*
1551 * Iterate the full tree, calling @down when first entering a node and @up when
1552 * leaving it for the final time.
1553 */
1554static
1555void aggregate_walk_tree(aggregate_func down, aggregate_func up,
1556 struct sched_domain *sd)
1557{
1558 struct task_group *parent, *child;
1559
1560 rcu_read_lock();
1561 parent = &root_task_group;
1562down:
1563 (*down)(parent, sd);
1564 list_for_each_entry_rcu(child, &parent->children, siblings) {
1565 parent = child;
1566 goto down;
1567
1568up:
1569 continue;
1570 }
1571 (*up)(parent, sd);
1572
1573 child = parent;
1574 parent = parent->parent;
1575 if (parent)
1576 goto up;
1577 rcu_read_unlock();
1578}
1579
1580/*
1581 * Calculate the aggregate runqueue weight.
1582 */
1583static
1584void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd)
1585{
1586 unsigned long rq_weight = 0;
1587 unsigned long task_weight = 0;
1588 int i;
1589
1590 for_each_cpu_mask(i, sd->span) {
1591 rq_weight += tg->cfs_rq[i]->load.weight;
1592 task_weight += tg->cfs_rq[i]->task_weight;
1593 }
1594
1595 aggregate(tg, sd)->rq_weight = rq_weight;
1596 aggregate(tg, sd)->task_weight = task_weight;
1597}
1598
1599/*
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001600 * Compute the weight of this group on the given cpus.
1601 */
1602static
1603void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd)
1604{
1605 unsigned long shares = 0;
1606 int i;
1607
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001608 for_each_cpu_mask(i, sd->span)
1609 shares += tg->cfs_rq[i]->shares;
1610
Peter Zijlstra3f5087a2008-04-25 00:25:08 +02001611 if ((!shares && aggregate(tg, sd)->rq_weight) || shares > tg->shares)
1612 shares = tg->shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001613
1614 aggregate(tg, sd)->shares = shares;
1615}
1616
1617/*
1618 * Compute the load fraction assigned to this group, relies on the aggregate
1619 * weight and this group's parent's load, i.e. top-down.
1620 */
1621static
1622void aggregate_group_load(struct task_group *tg, struct sched_domain *sd)
1623{
1624 unsigned long load;
1625
1626 if (!tg->parent) {
1627 int i;
1628
1629 load = 0;
1630 for_each_cpu_mask(i, sd->span)
1631 load += cpu_rq(i)->load.weight;
1632
1633 } else {
1634 load = aggregate(tg->parent, sd)->load;
1635
1636 /*
1637 * shares is our weight in the parent's rq so
1638 * shares/parent->rq_weight gives our fraction of the load
1639 */
1640 load *= aggregate(tg, sd)->shares;
1641 load /= aggregate(tg->parent, sd)->rq_weight + 1;
1642 }
1643
1644 aggregate(tg, sd)->load = load;
1645}
1646
1647static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1648
1649/*
1650 * Calculate and set the cpu's group shares.
1651 */
1652static void
1653__update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,
1654 int tcpu)
1655{
1656 int boost = 0;
1657 unsigned long shares;
1658 unsigned long rq_weight;
1659
1660 if (!tg->se[tcpu])
1661 return;
1662
1663 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1664
1665 /*
1666 * If there are currently no tasks on the cpu pretend there is one of
1667 * average load so that when a new task gets to run here it will not
1668 * get delayed by group starvation.
1669 */
1670 if (!rq_weight) {
1671 boost = 1;
1672 rq_weight = NICE_0_LOAD;
1673 }
1674
1675 /*
1676 * \Sum shares * rq_weight
1677 * shares = -----------------------
1678 * \Sum rq_weight
1679 *
1680 */
1681 shares = aggregate(tg, sd)->shares * rq_weight;
1682 shares /= aggregate(tg, sd)->rq_weight + 1;
1683
1684 /*
1685 * record the actual number of shares, not the boosted amount.
1686 */
1687 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1688
1689 if (shares < MIN_SHARES)
1690 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08001691 else if (shares > MAX_SHARES)
1692 shares = MAX_SHARES;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001693
1694 __set_se_shares(tg->se[tcpu], shares);
1695}
1696
1697/*
1698 * Re-adjust the weights on the cpu the task came from and on the cpu the
1699 * task went to.
1700 */
1701static void
1702__move_group_shares(struct task_group *tg, struct sched_domain *sd,
1703 int scpu, int dcpu)
1704{
1705 unsigned long shares;
1706
1707 shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1708
1709 __update_group_shares_cpu(tg, sd, scpu);
1710 __update_group_shares_cpu(tg, sd, dcpu);
1711
1712 /*
1713 * ensure we never loose shares due to rounding errors in the
1714 * above redistribution.
1715 */
1716 shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1717 if (shares)
1718 tg->cfs_rq[dcpu]->shares += shares;
1719}
1720
1721/*
1722 * Because changing a group's shares changes the weight of the super-group
1723 * we need to walk up the tree and change all shares until we hit the root.
1724 */
1725static void
1726move_group_shares(struct task_group *tg, struct sched_domain *sd,
1727 int scpu, int dcpu)
1728{
1729 while (tg) {
1730 __move_group_shares(tg, sd, scpu, dcpu);
1731 tg = tg->parent;
1732 }
1733}
1734
1735static
1736void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd)
1737{
1738 unsigned long shares = aggregate(tg, sd)->shares;
1739 int i;
1740
1741 for_each_cpu_mask(i, sd->span) {
1742 struct rq *rq = cpu_rq(i);
1743 unsigned long flags;
1744
1745 spin_lock_irqsave(&rq->lock, flags);
1746 __update_group_shares_cpu(tg, sd, i);
1747 spin_unlock_irqrestore(&rq->lock, flags);
1748 }
1749
1750 aggregate_group_shares(tg, sd);
1751
1752 /*
1753 * ensure we never loose shares due to rounding errors in the
1754 * above redistribution.
1755 */
1756 shares -= aggregate(tg, sd)->shares;
1757 if (shares) {
1758 tg->cfs_rq[sd->first_cpu]->shares += shares;
1759 aggregate(tg, sd)->shares += shares;
1760 }
1761}
1762
1763/*
1764 * Calculate the accumulative weight and recursive load of each task group
1765 * while walking down the tree.
1766 */
1767static
1768void aggregate_get_down(struct task_group *tg, struct sched_domain *sd)
1769{
1770 aggregate_group_weight(tg, sd);
1771 aggregate_group_shares(tg, sd);
1772 aggregate_group_load(tg, sd);
1773}
1774
1775/*
1776 * Rebalance the cpu shares while walking back up the tree.
1777 */
1778static
1779void aggregate_get_up(struct task_group *tg, struct sched_domain *sd)
1780{
1781 aggregate_group_set_shares(tg, sd);
1782}
1783
1784static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1785
1786static void __init init_aggregate(void)
1787{
1788 int i;
1789
1790 for_each_possible_cpu(i)
1791 spin_lock_init(&per_cpu(aggregate_lock, i));
1792}
1793
1794static int get_aggregate(struct sched_domain *sd)
1795{
1796 if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu)))
1797 return 0;
1798
1799 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd);
1800 return 1;
1801}
1802
1803static void put_aggregate(struct sched_domain *sd)
1804{
1805 spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu));
1806}
1807
1808static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1809{
1810 cfs_rq->shares = shares;
1811}
1812
1813#else
1814
1815static inline void init_aggregate(void)
1816{
1817}
1818
1819static inline int get_aggregate(struct sched_domain *sd)
1820{
1821 return 0;
1822}
1823
1824static inline void put_aggregate(struct sched_domain *sd)
1825{
1826}
1827#endif
1828
1829#else /* CONFIG_SMP */
1830
1831#ifdef CONFIG_FAIR_GROUP_SCHED
1832static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1833{
1834}
1835#endif
1836
Gregory Haskinse7693a32008-01-25 21:08:09 +01001837#endif /* CONFIG_SMP */
1838
Ingo Molnardd41f592007-07-09 18:51:59 +02001839#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001840#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001841#include "sched_fair.c"
1842#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001843#ifdef CONFIG_SCHED_DEBUG
1844# include "sched_debug.c"
1845#endif
1846
1847#define sched_class_highest (&rt_sched_class)
1848
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001849static void inc_nr_running(struct rq *rq)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001850{
1851 rq->nr_running++;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001852}
1853
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001854static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001855{
1856 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001857}
1858
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001859static void set_load_weight(struct task_struct *p)
1860{
1861 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001862 p->se.load.weight = prio_to_weight[0] * 2;
1863 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1864 return;
1865 }
1866
1867 /*
1868 * SCHED_IDLE tasks get minimal weight:
1869 */
1870 if (p->policy == SCHED_IDLE) {
1871 p->se.load.weight = WEIGHT_IDLEPRIO;
1872 p->se.load.inv_weight = WMULT_IDLEPRIO;
1873 return;
1874 }
1875
1876 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1877 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001878}
1879
Ingo Molnar8159f872007-08-09 11:16:49 +02001880static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001881{
1882 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001883 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001884 p->se.on_rq = 1;
1885}
1886
Ingo Molnar69be72c2007-08-09 11:16:49 +02001887static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001888{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001889 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001890 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001891}
1892
1893/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001894 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001895 */
Ingo Molnar14531182007-07-09 18:51:59 +02001896static inline int __normal_prio(struct task_struct *p)
1897{
Ingo Molnardd41f592007-07-09 18:51:59 +02001898 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001899}
1900
1901/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001902 * Calculate the expected normal priority: i.e. priority
1903 * without taking RT-inheritance into account. Might be
1904 * boosted by interactivity modifiers. Changes upon fork,
1905 * setprio syscalls, and whenever the interactivity
1906 * estimator recalculates.
1907 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001908static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001909{
1910 int prio;
1911
Ingo Molnare05606d2007-07-09 18:51:59 +02001912 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001913 prio = MAX_RT_PRIO-1 - p->rt_priority;
1914 else
1915 prio = __normal_prio(p);
1916 return prio;
1917}
1918
1919/*
1920 * Calculate the current priority, i.e. the priority
1921 * taken into account by the scheduler. This value might
1922 * be boosted by RT tasks, or might be boosted by
1923 * interactivity modifiers. Will be RT if the task got
1924 * RT-boosted. If not then it returns p->normal_prio.
1925 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001926static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001927{
1928 p->normal_prio = normal_prio(p);
1929 /*
1930 * If we are RT tasks or we were boosted to RT priority,
1931 * keep the priority unchanged. Otherwise, update priority
1932 * to the normal priority:
1933 */
1934 if (!rt_prio(p->prio))
1935 return p->normal_prio;
1936 return p->prio;
1937}
1938
1939/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001940 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001942static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001944 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001945 rq->nr_uninterruptible--;
1946
Ingo Molnar8159f872007-08-09 11:16:49 +02001947 enqueue_task(rq, p, wakeup);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001948 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
1951/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 * deactivate_task - remove a task from the runqueue.
1953 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001954static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001956 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001957 rq->nr_uninterruptible++;
1958
Ingo Molnar69be72c2007-08-09 11:16:49 +02001959 dequeue_task(rq, p, sleep);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001960 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963/**
1964 * task_curr - is this task currently executing on a CPU?
1965 * @p: the task in question.
1966 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001967inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 return cpu_curr(task_cpu(p)) == p;
1970}
1971
Peter Williams2dd73a42006-06-27 02:54:34 -07001972/* Used instead of source_load when we know the type == 0 */
1973unsigned long weighted_cpuload(const int cpu)
1974{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001975 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001976}
1977
1978static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1979{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001980 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001981#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001982 /*
1983 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1984 * successfuly executed on another CPU. We must ensure that updates of
1985 * per-task data have been completed by this moment.
1986 */
1987 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001988 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001989#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001990}
1991
Steven Rostedtcb469842008-01-25 21:08:22 +01001992static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1993 const struct sched_class *prev_class,
1994 int oldprio, int running)
1995{
1996 if (prev_class != p->sched_class) {
1997 if (prev_class->switched_from)
1998 prev_class->switched_from(rq, p, running);
1999 p->sched_class->switched_to(rq, p, running);
2000 } else
2001 p->sched_class->prio_changed(rq, p, oldprio, running);
2002}
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02002005
Ingo Molnarcc367732007-10-15 17:00:18 +02002006/*
2007 * Is this task likely cache-hot:
2008 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002009static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002010task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2011{
2012 s64 delta;
2013
Ingo Molnarf540a602008-03-15 17:10:34 +01002014 /*
2015 * Buddy candidates are cache hot:
2016 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01002017 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01002018 return 1;
2019
Ingo Molnarcc367732007-10-15 17:00:18 +02002020 if (p->sched_class != &fair_sched_class)
2021 return 0;
2022
Ingo Molnar6bc16652007-10-15 17:00:18 +02002023 if (sysctl_sched_migration_cost == -1)
2024 return 1;
2025 if (sysctl_sched_migration_cost == 0)
2026 return 0;
2027
Ingo Molnarcc367732007-10-15 17:00:18 +02002028 delta = now - p->se.exec_start;
2029
2030 return delta < (s64)sysctl_sched_migration_cost;
2031}
2032
2033
Ingo Molnardd41f592007-07-09 18:51:59 +02002034void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002035{
Ingo Molnardd41f592007-07-09 18:51:59 +02002036 int old_cpu = task_cpu(p);
2037 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002038 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
2039 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02002040 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002041
2042 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002043
2044#ifdef CONFIG_SCHEDSTATS
2045 if (p->se.wait_start)
2046 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002047 if (p->se.sleep_start)
2048 p->se.sleep_start -= clock_offset;
2049 if (p->se.block_start)
2050 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02002051 if (old_cpu != new_cpu) {
2052 schedstat_inc(p, se.nr_migrations);
2053 if (task_hot(p, old_rq->clock, NULL))
2054 schedstat_inc(p, se.nr_forced2_migrations);
2055 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002056#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002057 p->se.vruntime -= old_cfsrq->min_vruntime -
2058 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02002059
2060 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002061}
2062
Ingo Molnar70b97a72006-07-03 00:25:42 -07002063struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Ingo Molnar36c8b582006-07-03 00:25:41 -07002066 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 int dest_cpu;
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002070};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072/*
2073 * The task's runqueue lock must be held.
2074 * Returns true if you have to wait for migration thread.
2075 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002076static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002077migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002079 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 /*
2082 * If the task is not on a runqueue (and not running), then
2083 * it is sufficient to simply update the task's cpu field.
2084 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002085 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 set_task_cpu(p, dest_cpu);
2087 return 0;
2088 }
2089
2090 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 req->task = p;
2092 req->dest_cpu = dest_cpu;
2093 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return 1;
2096}
2097
2098/*
2099 * wait_task_inactive - wait for a thread to unschedule.
2100 *
2101 * The caller must ensure that the task *will* unschedule sometime soon,
2102 * else this function might spin for a *long* time. This function can't
2103 * be called with interrupts off, or it may introduce deadlock with
2104 * smp_call_function() if an IPI is sent by the same process we are
2105 * waiting to become inactive.
2106 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002107void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
2109 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002110 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002111 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Andi Kleen3a5c3592007-10-15 17:00:14 +02002113 for (;;) {
2114 /*
2115 * We do the initial early heuristics without holding
2116 * any task-queue locks at all. We'll only try to get
2117 * the runqueue lock when things look like they will
2118 * work out!
2119 */
2120 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002121
Andi Kleen3a5c3592007-10-15 17:00:14 +02002122 /*
2123 * If the task is actively running on another CPU
2124 * still, just relax and busy-wait without holding
2125 * any locks.
2126 *
2127 * NOTE! Since we don't hold any locks, it's not
2128 * even sure that "rq" stays as the right runqueue!
2129 * But we don't care, since "task_running()" will
2130 * return false if the runqueue has changed and p
2131 * is actually now running somewhere else!
2132 */
2133 while (task_running(rq, p))
2134 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002135
Andi Kleen3a5c3592007-10-15 17:00:14 +02002136 /*
2137 * Ok, time to look more closely! We need the rq
2138 * lock now, to be *sure*. If we're wrong, we'll
2139 * just go back and repeat.
2140 */
2141 rq = task_rq_lock(p, &flags);
2142 running = task_running(rq, p);
2143 on_rq = p->se.on_rq;
2144 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002145
Andi Kleen3a5c3592007-10-15 17:00:14 +02002146 /*
2147 * Was it really running after all now that we
2148 * checked with the proper locks actually held?
2149 *
2150 * Oops. Go back and try again..
2151 */
2152 if (unlikely(running)) {
2153 cpu_relax();
2154 continue;
2155 }
2156
2157 /*
2158 * It's not enough that it's not actively running,
2159 * it must be off the runqueue _entirely_, and not
2160 * preempted!
2161 *
2162 * So if it wa still runnable (but just not actively
2163 * running right now), it's preempted, and we should
2164 * yield - it could be a while.
2165 */
2166 if (unlikely(on_rq)) {
2167 schedule_timeout_uninterruptible(1);
2168 continue;
2169 }
2170
2171 /*
2172 * Ahh, all good. It wasn't running, and it wasn't
2173 * runnable, which means that it will never become
2174 * running in the future either. We're all done!
2175 */
2176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
2180/***
2181 * kick_process - kick a running thread to enter/exit the kernel
2182 * @p: the to-be-kicked thread
2183 *
2184 * Cause a process which is running on another CPU to enter
2185 * kernel-mode, without any delay. (to get signals handled.)
2186 *
2187 * NOTE: this function doesnt have to take the runqueue lock,
2188 * because all it wants to ensure is that the remote task enters
2189 * the kernel. If the IPI races and the task has been migrated
2190 * to another CPU then no harm is done and the purpose has been
2191 * achieved as well.
2192 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002193void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
2195 int cpu;
2196
2197 preempt_disable();
2198 cpu = task_cpu(p);
2199 if ((cpu != smp_processor_id()) && task_curr(p))
2200 smp_send_reschedule(cpu);
2201 preempt_enable();
2202}
2203
2204/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002205 * Return a low guess at the load of a migration-source cpu weighted
2206 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 *
2208 * We want to under-estimate the load of migration sources, to
2209 * balance conservatively.
2210 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002211static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002212{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002213 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002214 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002215
Peter Williams2dd73a42006-06-27 02:54:34 -07002216 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002217 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002218
Ingo Molnardd41f592007-07-09 18:51:59 +02002219 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
2222/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002223 * Return a high guess at the load of a migration-target cpu weighted
2224 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002226static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002227{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002228 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002229 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002230
Peter Williams2dd73a42006-06-27 02:54:34 -07002231 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002232 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002233
Ingo Molnardd41f592007-07-09 18:51:59 +02002234 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002235}
2236
2237/*
2238 * Return the average load per task on the cpu's run queue
2239 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002240static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07002241{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002242 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002243 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07002244 unsigned long n = rq->nr_running;
2245
Ingo Molnardd41f592007-07-09 18:51:59 +02002246 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
Nick Piggin147cbb42005-06-25 14:57:19 -07002249/*
2250 * find_idlest_group finds and returns the least busy CPU group within the
2251 * domain.
2252 */
2253static struct sched_group *
2254find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2255{
2256 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2257 unsigned long min_load = ULONG_MAX, this_load = 0;
2258 int load_idx = sd->forkexec_idx;
2259 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2260
2261 do {
2262 unsigned long load, avg_load;
2263 int local_group;
2264 int i;
2265
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002266 /* Skip over this group if it has no CPUs allowed */
2267 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002268 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002269
Nick Piggin147cbb42005-06-25 14:57:19 -07002270 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002271
2272 /* Tally up the load of all CPUs in the group */
2273 avg_load = 0;
2274
2275 for_each_cpu_mask(i, group->cpumask) {
2276 /* Bias balancing toward cpus of our domain */
2277 if (local_group)
2278 load = source_load(i, load_idx);
2279 else
2280 load = target_load(i, load_idx);
2281
2282 avg_load += load;
2283 }
2284
2285 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002286 avg_load = sg_div_cpu_power(group,
2287 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002288
2289 if (local_group) {
2290 this_load = avg_load;
2291 this = group;
2292 } else if (avg_load < min_load) {
2293 min_load = avg_load;
2294 idlest = group;
2295 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002296 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002297
2298 if (!idlest || 100*this_load < imbalance*min_load)
2299 return NULL;
2300 return idlest;
2301}
2302
2303/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002304 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002305 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002306static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002307find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2308 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002309{
2310 unsigned long load, min_load = ULONG_MAX;
2311 int idlest = -1;
2312 int i;
2313
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002314 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002315 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002316
Mike Travis7c16ec52008-04-04 18:11:11 -07002317 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002318 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002319
2320 if (load < min_load || (load == min_load && i == this_cpu)) {
2321 min_load = load;
2322 idlest = i;
2323 }
2324 }
2325
2326 return idlest;
2327}
2328
Nick Piggin476d1392005-06-25 14:57:29 -07002329/*
2330 * sched_balance_self: balance the current task (running on cpu) in domains
2331 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2332 * SD_BALANCE_EXEC.
2333 *
2334 * Balance, ie. select the least loaded group.
2335 *
2336 * Returns the target CPU number, or the same CPU if no balancing is needed.
2337 *
2338 * preempt must be disabled.
2339 */
2340static int sched_balance_self(int cpu, int flag)
2341{
2342 struct task_struct *t = current;
2343 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002344
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002345 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002346 /*
2347 * If power savings logic is enabled for a domain, stop there.
2348 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002349 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2350 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002351 if (tmp->flags & flag)
2352 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002353 }
Nick Piggin476d1392005-06-25 14:57:29 -07002354
2355 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002356 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002357 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002358 int new_cpu, weight;
2359
2360 if (!(sd->flags & flag)) {
2361 sd = sd->child;
2362 continue;
2363 }
Nick Piggin476d1392005-06-25 14:57:29 -07002364
2365 span = sd->span;
2366 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002367 if (!group) {
2368 sd = sd->child;
2369 continue;
2370 }
Nick Piggin476d1392005-06-25 14:57:29 -07002371
Mike Travis7c16ec52008-04-04 18:11:11 -07002372 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002373 if (new_cpu == -1 || new_cpu == cpu) {
2374 /* Now try balancing at a lower domain level of cpu */
2375 sd = sd->child;
2376 continue;
2377 }
Nick Piggin476d1392005-06-25 14:57:29 -07002378
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002379 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002380 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002381 sd = NULL;
2382 weight = cpus_weight(span);
2383 for_each_domain(cpu, tmp) {
2384 if (weight <= cpus_weight(tmp->span))
2385 break;
2386 if (tmp->flags & flag)
2387 sd = tmp;
2388 }
2389 /* while loop will break here if sd == NULL */
2390 }
2391
2392 return cpu;
2393}
2394
2395#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Ingo Molnar4e655512008-05-12 21:20:52 +02002397#ifdef CONFIG_CONTEXT_SWITCH_TRACER
2398
2399void ftrace_all_fair_tasks(void *__rq, void *__tr, void *__data)
2400{
Ingo Molnar4e655512008-05-12 21:20:52 +02002401 struct task_struct *p;
Ingo Molnar24cd5d12008-05-12 21:20:52 +02002402 struct sched_entity *se;
Ingo Molnar4e655512008-05-12 21:20:52 +02002403 struct rb_node *curr;
2404 struct rq *rq = __rq;
2405
2406 curr = first_fair(&rq->cfs);
2407 if (!curr)
2408 return;
2409
Ingo Molnar24cd5d12008-05-12 21:20:52 +02002410 if (rq->cfs.curr) {
2411 p = task_of(rq->cfs.curr);
2412 __trace_special(__tr, __data,
2413 p->pid, p->se.vruntime, p->se.sum_exec_runtime);
2414 }
2415 if (rq->cfs.next) {
2416 p = task_of(rq->cfs.next);
2417 __trace_special(__tr, __data,
2418 p->pid, p->se.vruntime, p->se.sum_exec_runtime);
2419 }
2420
Ingo Molnar4e655512008-05-12 21:20:52 +02002421 while (curr) {
2422 se = rb_entry(curr, struct sched_entity, run_node);
2423 if (!entity_is_task(se))
2424 continue;
2425
2426 p = task_of(se);
2427
2428 __trace_special(__tr, __data,
2429 p->pid, p->se.vruntime, p->se.sum_exec_runtime);
2430
2431 curr = rb_next(curr);
2432 }
2433}
2434
2435#endif
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437/***
2438 * try_to_wake_up - wake up a thread
2439 * @p: the to-be-woken-up thread
2440 * @state: the mask of task states that can be woken
2441 * @sync: do a synchronous wakeup?
2442 *
2443 * Put it on the run-queue if it's not already there. The "current"
2444 * thread is always on the run-queue (except when the actual
2445 * re-schedule is in progress), and as such you're allowed to do
2446 * the simpler "current->state = TASK_RUNNING" to mark yourself
2447 * runnable without the overhead of this.
2448 *
2449 * returns failure only if the task is already active.
2450 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002451static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
Ingo Molnarcc367732007-10-15 17:00:18 +02002453 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 unsigned long flags;
2455 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002456 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Ingo Molnarb85d0662008-03-16 20:03:22 +01002458 if (!sched_feat(SYNC_WAKEUPS))
2459 sync = 0;
2460
Linus Torvalds04e2f172008-02-23 18:05:03 -08002461 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 rq = task_rq_lock(p, &flags);
2463 old_state = p->state;
2464 if (!(old_state & state))
2465 goto out;
2466
Ingo Molnardd41f592007-07-09 18:51:59 +02002467 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 goto out_running;
2469
2470 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002471 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 this_cpu = smp_processor_id();
2473
2474#ifdef CONFIG_SMP
2475 if (unlikely(task_running(rq, p)))
2476 goto out_activate;
2477
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002478 cpu = p->sched_class->select_task_rq(p, sync);
2479 if (cpu != orig_cpu) {
2480 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 task_rq_unlock(rq, &flags);
2482 /* might preempt at this point */
2483 rq = task_rq_lock(p, &flags);
2484 old_state = p->state;
2485 if (!(old_state & state))
2486 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002487 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 goto out_running;
2489
2490 this_cpu = smp_processor_id();
2491 cpu = task_cpu(p);
2492 }
2493
Gregory Haskinse7693a32008-01-25 21:08:09 +01002494#ifdef CONFIG_SCHEDSTATS
2495 schedstat_inc(rq, ttwu_count);
2496 if (cpu == this_cpu)
2497 schedstat_inc(rq, ttwu_local);
2498 else {
2499 struct sched_domain *sd;
2500 for_each_domain(this_cpu, sd) {
2501 if (cpu_isset(cpu, sd->span)) {
2502 schedstat_inc(sd, ttwu_wake_remote);
2503 break;
2504 }
2505 }
2506 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002507#endif
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509out_activate:
2510#endif /* CONFIG_SMP */
Ingo Molnar4e655512008-05-12 21:20:52 +02002511 ftrace_wake_up_task(rq, p, rq->curr);
Ingo Molnarcc367732007-10-15 17:00:18 +02002512 schedstat_inc(p, se.nr_wakeups);
2513 if (sync)
2514 schedstat_inc(p, se.nr_wakeups_sync);
2515 if (orig_cpu != cpu)
2516 schedstat_inc(p, se.nr_wakeups_migrate);
2517 if (cpu == this_cpu)
2518 schedstat_inc(p, se.nr_wakeups_local);
2519 else
2520 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002521 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002522 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 success = 1;
2524
2525out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002526 check_preempt_curr(rq, p);
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002529#ifdef CONFIG_SMP
2530 if (p->sched_class->task_wake_up)
2531 p->sched_class->task_wake_up(rq, p);
2532#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533out:
2534 task_rq_unlock(rq, &flags);
2535
2536 return success;
2537}
2538
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002539int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002541 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543EXPORT_SYMBOL(wake_up_process);
2544
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002545int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
2547 return try_to_wake_up(p, state, 0);
2548}
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550/*
2551 * Perform scheduler related setup for a newly forked process p.
2552 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002553 *
2554 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002556static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Ingo Molnardd41f592007-07-09 18:51:59 +02002558 p->se.exec_start = 0;
2559 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002560 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002561 p->se.last_wakeup = 0;
2562 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002563
2564#ifdef CONFIG_SCHEDSTATS
2565 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002566 p->se.sum_sleep_runtime = 0;
2567 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002568 p->se.block_start = 0;
2569 p->se.sleep_max = 0;
2570 p->se.block_max = 0;
2571 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002572 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002573 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002574#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002575
Peter Zijlstrafa717062008-01-25 21:08:27 +01002576 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002577 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002578 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002579
Avi Kivitye107be32007-07-26 13:40:43 +02002580#ifdef CONFIG_PREEMPT_NOTIFIERS
2581 INIT_HLIST_HEAD(&p->preempt_notifiers);
2582#endif
2583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 /*
2585 * We mark the process as running here, but have not actually
2586 * inserted it onto the runqueue yet. This guarantees that
2587 * nobody will actually run it, and a signal or other external
2588 * event cannot wake it up and insert it on the runqueue either.
2589 */
2590 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002591}
2592
2593/*
2594 * fork()/clone()-time setup:
2595 */
2596void sched_fork(struct task_struct *p, int clone_flags)
2597{
2598 int cpu = get_cpu();
2599
2600 __sched_fork(p);
2601
2602#ifdef CONFIG_SMP
2603 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2604#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002605 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002606
2607 /*
2608 * Make sure we do not leak PI boosting priority to the child:
2609 */
2610 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002611 if (!rt_prio(p->prio))
2612 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002613
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002614#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002615 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002616 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002618#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002619 p->oncpu = 0;
2620#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002622 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002623 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002625 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
2628/*
2629 * wake_up_new_task - wake up a newly created task for the first time.
2630 *
2631 * This function will do some initial scheduler statistics housekeeping
2632 * that must be done for every newly created context, then puts the task
2633 * on the runqueue and wakes it.
2634 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002635void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002638 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002642 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 p->prio = effective_prio(p);
2645
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002646 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002647 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002650 * Let the scheduling class do new task startup
2651 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002653 p->sched_class->task_new(rq, p);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002654 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
Ingo Molnar4e655512008-05-12 21:20:52 +02002656 ftrace_wake_up_task(rq, p, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02002657 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002658#ifdef CONFIG_SMP
2659 if (p->sched_class->task_wake_up)
2660 p->sched_class->task_wake_up(rq, p);
2661#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002662 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
Avi Kivitye107be32007-07-26 13:40:43 +02002665#ifdef CONFIG_PREEMPT_NOTIFIERS
2666
2667/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002668 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2669 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002670 */
2671void preempt_notifier_register(struct preempt_notifier *notifier)
2672{
2673 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2674}
2675EXPORT_SYMBOL_GPL(preempt_notifier_register);
2676
2677/**
2678 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002679 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002680 *
2681 * This is safe to call from within a preemption notifier.
2682 */
2683void preempt_notifier_unregister(struct preempt_notifier *notifier)
2684{
2685 hlist_del(&notifier->link);
2686}
2687EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2688
2689static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2690{
2691 struct preempt_notifier *notifier;
2692 struct hlist_node *node;
2693
2694 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2695 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2696}
2697
2698static void
2699fire_sched_out_preempt_notifiers(struct task_struct *curr,
2700 struct task_struct *next)
2701{
2702 struct preempt_notifier *notifier;
2703 struct hlist_node *node;
2704
2705 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2706 notifier->ops->sched_out(notifier, next);
2707}
2708
2709#else
2710
2711static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2712{
2713}
2714
2715static void
2716fire_sched_out_preempt_notifiers(struct task_struct *curr,
2717 struct task_struct *next)
2718{
2719}
2720
2721#endif
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002724 * prepare_task_switch - prepare to switch tasks
2725 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002726 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002727 * @next: the task we are going to switch to.
2728 *
2729 * This is called with the rq lock held and interrupts off. It must
2730 * be paired with a subsequent finish_task_switch after the context
2731 * switch.
2732 *
2733 * prepare_task_switch sets up locking and calls architecture specific
2734 * hooks.
2735 */
Avi Kivitye107be32007-07-26 13:40:43 +02002736static inline void
2737prepare_task_switch(struct rq *rq, struct task_struct *prev,
2738 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002739{
Avi Kivitye107be32007-07-26 13:40:43 +02002740 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002741 prepare_lock_switch(rq, next);
2742 prepare_arch_switch(next);
2743}
2744
2745/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002747 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 * @prev: the thread we just switched away from.
2749 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002750 * finish_task_switch must be called after the context switch, paired
2751 * with a prepare_task_switch call before the context switch.
2752 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2753 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 *
2755 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002756 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 * with the lock held can cause deadlocks; see schedule() for
2758 * details.)
2759 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002760static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 __releases(rq->lock)
2762{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002764 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 rq->prev_mm = NULL;
2767
2768 /*
2769 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002770 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002771 * schedule one last time. The schedule call will never return, and
2772 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002773 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 * still held, otherwise prev could be scheduled on another cpu, die
2775 * there before we look at prev->state, and then the reference would
2776 * be dropped twice.
2777 * Manfred Spraul <manfred@colorfullife.com>
2778 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002779 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002780 finish_arch_switch(prev);
2781 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002782#ifdef CONFIG_SMP
2783 if (current->sched_class->post_schedule)
2784 current->sched_class->post_schedule(rq);
2785#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002786
Avi Kivitye107be32007-07-26 13:40:43 +02002787 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (mm)
2789 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002790 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002791 /*
2792 * Remove function-return probe instances associated with this
2793 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002794 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002795 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
2799
2800/**
2801 * schedule_tail - first thing a freshly forked thread must call.
2802 * @prev: the thread we just switched away from.
2803 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002804asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 __releases(rq->lock)
2806{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002807 struct rq *rq = this_rq();
2808
Nick Piggin4866cde2005-06-25 14:57:23 -07002809 finish_task_switch(rq, prev);
2810#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2811 /* In this case, finish_task_switch does not reenable preemption */
2812 preempt_enable();
2813#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002815 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
2818/*
2819 * context_switch - switch to the new MM and the new
2820 * thread's register state.
2821 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002822static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002823context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002824 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Ingo Molnardd41f592007-07-09 18:51:59 +02002826 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Avi Kivitye107be32007-07-26 13:40:43 +02002828 prepare_task_switch(rq, prev, next);
Ingo Molnar4e655512008-05-12 21:20:52 +02002829 ftrace_ctx_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002830 mm = next->mm;
2831 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002832 /*
2833 * For paravirt, this is coupled with an exit in switch_to to
2834 * combine the page table reload and the switch backend into
2835 * one hypercall.
2836 */
2837 arch_enter_lazy_cpu_mode();
2838
Ingo Molnardd41f592007-07-09 18:51:59 +02002839 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 next->active_mm = oldmm;
2841 atomic_inc(&oldmm->mm_count);
2842 enter_lazy_tlb(oldmm, next);
2843 } else
2844 switch_mm(oldmm, mm, next);
2845
Ingo Molnardd41f592007-07-09 18:51:59 +02002846 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 rq->prev_mm = oldmm;
2849 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002850 /*
2851 * Since the runqueue lock will be released by the next
2852 * task (which is an invalid locking op but in the case
2853 * of the scheduler it's an obvious special-case), so we
2854 * do an early lockdep release here:
2855 */
2856#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002857 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002858#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 /* Here we just switch the register state and the stack. */
2861 switch_to(prev, next, prev);
2862
Ingo Molnardd41f592007-07-09 18:51:59 +02002863 barrier();
2864 /*
2865 * this_rq must be evaluated again because prev may have moved
2866 * CPUs since it called schedule(), thus the 'rq' on its stack
2867 * frame will be invalid.
2868 */
2869 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870}
2871
2872/*
2873 * nr_running, nr_uninterruptible and nr_context_switches:
2874 *
2875 * externally visible scheduler statistics: current number of runnable
2876 * threads, current number of uninterruptible-sleeping threads, total
2877 * number of context switches performed since bootup.
2878 */
2879unsigned long nr_running(void)
2880{
2881 unsigned long i, sum = 0;
2882
2883 for_each_online_cpu(i)
2884 sum += cpu_rq(i)->nr_running;
2885
2886 return sum;
2887}
2888
2889unsigned long nr_uninterruptible(void)
2890{
2891 unsigned long i, sum = 0;
2892
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002893 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 sum += cpu_rq(i)->nr_uninterruptible;
2895
2896 /*
2897 * Since we read the counters lockless, it might be slightly
2898 * inaccurate. Do not allow it to go below zero though:
2899 */
2900 if (unlikely((long)sum < 0))
2901 sum = 0;
2902
2903 return sum;
2904}
2905
2906unsigned long long nr_context_switches(void)
2907{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002908 int i;
2909 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002911 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 sum += cpu_rq(i)->nr_switches;
2913
2914 return sum;
2915}
2916
2917unsigned long nr_iowait(void)
2918{
2919 unsigned long i, sum = 0;
2920
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002921 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2923
2924 return sum;
2925}
2926
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002927unsigned long nr_active(void)
2928{
2929 unsigned long i, running = 0, uninterruptible = 0;
2930
2931 for_each_online_cpu(i) {
2932 running += cpu_rq(i)->nr_running;
2933 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2934 }
2935
2936 if (unlikely((long)uninterruptible < 0))
2937 uninterruptible = 0;
2938
2939 return running + uninterruptible;
2940}
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002943 * Update rq->cpu_load[] statistics. This function is usually called every
2944 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002945 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002946static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002947{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002948 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002949 int i, scale;
2950
2951 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002952
2953 /* Update our load: */
2954 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2955 unsigned long old_load, new_load;
2956
2957 /* scale is effectively 1 << i now, and >> i divides by scale */
2958
2959 old_load = this_rq->cpu_load[i];
2960 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002961 /*
2962 * Round up the averaging division if load is increasing. This
2963 * prevents us from getting stuck on 9 if the load is 10, for
2964 * example.
2965 */
2966 if (new_load > old_load)
2967 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002968 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2969 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002970}
2971
Ingo Molnardd41f592007-07-09 18:51:59 +02002972#ifdef CONFIG_SMP
2973
Ingo Molnar48f24c42006-07-03 00:25:40 -07002974/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 * double_rq_lock - safely lock two runqueues
2976 *
2977 * Note this does not disable interrupts like task_rq_lock,
2978 * you need to do so manually before calling.
2979 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002980static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 __acquires(rq1->lock)
2982 __acquires(rq2->lock)
2983{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002984 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (rq1 == rq2) {
2986 spin_lock(&rq1->lock);
2987 __acquire(rq2->lock); /* Fake it out ;) */
2988 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002989 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 spin_lock(&rq1->lock);
2991 spin_lock(&rq2->lock);
2992 } else {
2993 spin_lock(&rq2->lock);
2994 spin_lock(&rq1->lock);
2995 }
2996 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002997 update_rq_clock(rq1);
2998 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
3001/*
3002 * double_rq_unlock - safely unlock two runqueues
3003 *
3004 * Note this does not restore interrupts like task_rq_unlock,
3005 * you need to do so manually after calling.
3006 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003007static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 __releases(rq1->lock)
3009 __releases(rq2->lock)
3010{
3011 spin_unlock(&rq1->lock);
3012 if (rq1 != rq2)
3013 spin_unlock(&rq2->lock);
3014 else
3015 __release(rq2->lock);
3016}
3017
3018/*
3019 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3020 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01003021static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 __releases(this_rq->lock)
3023 __acquires(busiest->lock)
3024 __acquires(this_rq->lock)
3025{
Steven Rostedte8fa1362008-01-25 21:08:05 +01003026 int ret = 0;
3027
Kirill Korotaev054b9102006-12-10 02:20:11 -08003028 if (unlikely(!irqs_disabled())) {
3029 /* printk() doesn't work good under rq->lock */
3030 spin_unlock(&this_rq->lock);
3031 BUG_ON(1);
3032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003034 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 spin_unlock(&this_rq->lock);
3036 spin_lock(&busiest->lock);
3037 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01003038 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 } else
3040 spin_lock(&busiest->lock);
3041 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01003042 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
3045/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 * If dest_cpu is allowed for this process, migrate the task to it.
3047 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003048 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 * the cpu_allowed mask is restored.
3050 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003051static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003053 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003055 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
3057 rq = task_rq_lock(p, &flags);
3058 if (!cpu_isset(dest_cpu, p->cpus_allowed)
3059 || unlikely(cpu_is_offline(dest_cpu)))
3060 goto out;
3061
3062 /* force the process onto the specified CPU */
3063 if (migrate_task(p, dest_cpu, &req)) {
3064 /* Need to wait for migration thread (might exit: take ref). */
3065 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003066
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 get_task_struct(mt);
3068 task_rq_unlock(rq, &flags);
3069 wake_up_process(mt);
3070 put_task_struct(mt);
3071 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return;
3074 }
3075out:
3076 task_rq_unlock(rq, &flags);
3077}
3078
3079/*
Nick Piggin476d1392005-06-25 14:57:29 -07003080 * sched_exec - execve() is a valuable balancing opportunity, because at
3081 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 */
3083void sched_exec(void)
3084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003086 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003088 if (new_cpu != this_cpu)
3089 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090}
3091
3092/*
3093 * pull_task - move a task from a remote runqueue to the local runqueue.
3094 * Both runqueues must be locked.
3095 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003096static void pull_task(struct rq *src_rq, struct task_struct *p,
3097 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003099 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003101 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 /*
3103 * Note that idle threads have a prio of MAX_PRIO, for this test
3104 * to be always true for them.
3105 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003106 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
3109/*
3110 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3111 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003112static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003113int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003114 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003115 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
3117 /*
3118 * We do not migrate tasks that are:
3119 * 1) running (obviously), or
3120 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3121 * 3) are cache-hot on their current CPU.
3122 */
Ingo Molnarcc367732007-10-15 17:00:18 +02003123 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3124 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003126 }
Nick Piggin81026792005-06-25 14:57:07 -07003127 *all_pinned = 0;
3128
Ingo Molnarcc367732007-10-15 17:00:18 +02003129 if (task_running(rq, p)) {
3130 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003131 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Ingo Molnarda84d962007-10-15 17:00:18 +02003134 /*
3135 * Aggressive migration if:
3136 * 1) task is cache cold, or
3137 * 2) too many balance attempts have failed.
3138 */
3139
Ingo Molnar6bc16652007-10-15 17:00:18 +02003140 if (!task_hot(p, rq->clock, sd) ||
3141 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003142#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02003143 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003144 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003145 schedstat_inc(p, se.nr_forced_migrations);
3146 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003147#endif
3148 return 1;
3149 }
3150
Ingo Molnarcc367732007-10-15 17:00:18 +02003151 if (task_hot(p, rq->clock, sd)) {
3152 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003153 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return 1;
3156}
3157
Peter Williamse1d14842007-10-24 18:23:51 +02003158static unsigned long
3159balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3160 unsigned long max_load_move, struct sched_domain *sd,
3161 enum cpu_idle_type idle, int *all_pinned,
3162 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003163{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003164 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02003165 struct task_struct *p;
3166 long rem_load_move = max_load_move;
3167
Peter Williamse1d14842007-10-24 18:23:51 +02003168 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003169 goto out;
3170
3171 pinned = 1;
3172
3173 /*
3174 * Start the load-balancing iterator:
3175 */
3176 p = iterator->start(iterator->arg);
3177next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003178 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003179 goto out;
3180 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003181 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02003182 * skip a task if it will be the highest priority task (i.e. smallest
3183 * prio value) on its new queue regardless of its load weight
3184 */
3185 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
3186 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003187 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003188 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003189 p = iterator->next(iterator->arg);
3190 goto next;
3191 }
3192
3193 pull_task(busiest, p, this_rq, this_cpu);
3194 pulled++;
3195 rem_load_move -= p->se.load.weight;
3196
3197 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003198 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003199 */
Peter Williamse1d14842007-10-24 18:23:51 +02003200 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003201 if (p->prio < *this_best_prio)
3202 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003203 p = iterator->next(iterator->arg);
3204 goto next;
3205 }
3206out:
3207 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003208 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003209 * so we can safely collect pull_task() stats here rather than
3210 * inside pull_task().
3211 */
3212 schedstat_add(sd, lb_gained[idle], pulled);
3213
3214 if (all_pinned)
3215 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003216
3217 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003218}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220/*
Peter Williams43010652007-08-09 11:16:46 +02003221 * move_tasks tries to move up to max_load_move weighted load from busiest to
3222 * this_rq, as part of a balancing operation within domain "sd".
3223 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 *
3225 * Called with both runqueues locked.
3226 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003227static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003228 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003229 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003230 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003232 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003233 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003234 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Ingo Molnardd41f592007-07-09 18:51:59 +02003236 do {
Peter Williams43010652007-08-09 11:16:46 +02003237 total_load_moved +=
3238 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003239 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003240 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003241 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003242 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Peter Williams43010652007-08-09 11:16:46 +02003244 return total_load_moved > 0;
3245}
3246
Peter Williamse1d14842007-10-24 18:23:51 +02003247static int
3248iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3249 struct sched_domain *sd, enum cpu_idle_type idle,
3250 struct rq_iterator *iterator)
3251{
3252 struct task_struct *p = iterator->start(iterator->arg);
3253 int pinned = 0;
3254
3255 while (p) {
3256 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3257 pull_task(busiest, p, this_rq, this_cpu);
3258 /*
3259 * Right now, this is only the second place pull_task()
3260 * is called, so we can safely collect pull_task()
3261 * stats here rather than inside pull_task().
3262 */
3263 schedstat_inc(sd, lb_gained[idle]);
3264
3265 return 1;
3266 }
3267 p = iterator->next(iterator->arg);
3268 }
3269
3270 return 0;
3271}
3272
Peter Williams43010652007-08-09 11:16:46 +02003273/*
3274 * move_one_task tries to move exactly one task from busiest to this_rq, as
3275 * part of active balancing operations within "domain".
3276 * Returns 1 if successful and 0 otherwise.
3277 *
3278 * Called with both runqueues locked.
3279 */
3280static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3281 struct sched_domain *sd, enum cpu_idle_type idle)
3282{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003283 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003284
3285 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003286 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003287 return 1;
3288
3289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290}
3291
3292/*
3293 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003294 * domain. It calculates and returns the amount of weighted load which
3295 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 */
3297static struct sched_group *
3298find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003299 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003300 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301{
3302 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3303 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003304 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003305 unsigned long busiest_load_per_task, busiest_nr_running;
3306 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003307 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003308#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3309 int power_savings_balance = 1;
3310 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3311 unsigned long min_nr_running = ULONG_MAX;
3312 struct sched_group *group_min = NULL, *group_leader = NULL;
3313#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003316 busiest_load_per_task = busiest_nr_running = 0;
3317 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003318 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003319 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003320 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003321 load_idx = sd->newidle_idx;
3322 else
3323 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
3325 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003326 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 int local_group;
3328 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003329 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003330 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003331 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
3333 local_group = cpu_isset(this_cpu, group->cpumask);
3334
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003335 if (local_group)
3336 balance_cpu = first_cpu(group->cpumask);
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003339 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02003340 max_cpu_load = 0;
3341 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
3343 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003344 struct rq *rq;
3345
3346 if (!cpu_isset(i, *cpus))
3347 continue;
3348
3349 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003350
Suresh Siddha9439aab2007-07-19 21:28:35 +02003351 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003352 *sd_idle = 0;
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003355 if (local_group) {
3356 if (idle_cpu(i) && !first_idle_cpu) {
3357 first_idle_cpu = 1;
3358 balance_cpu = i;
3359 }
3360
Nick Piggina2000572006-02-10 01:51:02 -08003361 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003362 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003363 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003364 if (load > max_cpu_load)
3365 max_cpu_load = load;
3366 if (min_cpu_load > load)
3367 min_cpu_load = load;
3368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
3370 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003371 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003372 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 }
3374
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003375 /*
3376 * First idle cpu or the first cpu(busiest) in this sched group
3377 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003378 * domains. In the newly idle case, we will allow all the cpu's
3379 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003380 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003381 if (idle != CPU_NEWLY_IDLE && local_group &&
3382 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003383 *balance = 0;
3384 goto ret;
3385 }
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003388 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003391 avg_load = sg_div_cpu_power(group,
3392 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Ken Chen908a7c12007-10-17 16:55:11 +02003394 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3395 __group_imb = 1;
3396
Eric Dumazet5517d862007-05-08 00:32:57 -07003397 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003398
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 if (local_group) {
3400 this_load = avg_load;
3401 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003402 this_nr_running = sum_nr_running;
3403 this_load_per_task = sum_weighted_load;
3404 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003405 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 max_load = avg_load;
3407 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003408 busiest_nr_running = sum_nr_running;
3409 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003410 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003412
3413#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3414 /*
3415 * Busy processors will not participate in power savings
3416 * balance.
3417 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003418 if (idle == CPU_NOT_IDLE ||
3419 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3420 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003421
3422 /*
3423 * If the local group is idle or completely loaded
3424 * no need to do power savings balance at this domain
3425 */
3426 if (local_group && (this_nr_running >= group_capacity ||
3427 !this_nr_running))
3428 power_savings_balance = 0;
3429
Ingo Molnardd41f592007-07-09 18:51:59 +02003430 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003431 * If a group is already running at full capacity or idle,
3432 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003433 */
3434 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003435 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003436 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003437
Ingo Molnardd41f592007-07-09 18:51:59 +02003438 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003439 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003440 * This is the group from where we need to pick up the load
3441 * for saving power
3442 */
3443 if ((sum_nr_running < min_nr_running) ||
3444 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003445 first_cpu(group->cpumask) <
3446 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003447 group_min = group;
3448 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003449 min_load_per_task = sum_weighted_load /
3450 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003451 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003452
Ingo Molnardd41f592007-07-09 18:51:59 +02003453 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003454 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003455 * capacity but still has some space to pick up some load
3456 * from other group and save more power
3457 */
3458 if (sum_nr_running <= group_capacity - 1) {
3459 if (sum_nr_running > leader_nr_running ||
3460 (sum_nr_running == leader_nr_running &&
3461 first_cpu(group->cpumask) >
3462 first_cpu(group_leader->cpumask))) {
3463 group_leader = group;
3464 leader_nr_running = sum_nr_running;
3465 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003466 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003467group_next:
3468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 group = group->next;
3470 } while (group != sd->groups);
3471
Peter Williams2dd73a42006-06-27 02:54:34 -07003472 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 goto out_balanced;
3474
3475 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3476
3477 if (this_load >= avg_load ||
3478 100*max_load <= sd->imbalance_pct*this_load)
3479 goto out_balanced;
3480
Peter Williams2dd73a42006-06-27 02:54:34 -07003481 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003482 if (group_imb)
3483 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3484
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 /*
3486 * We're trying to get all the cpus to the average_load, so we don't
3487 * want to push ourselves above the average load, nor do we wish to
3488 * reduce the max loaded cpu below the average load, as either of these
3489 * actions would just result in more rebalancing later, and ping-pong
3490 * tasks around. Thus we look for the minimum possible imbalance.
3491 * Negative imbalances (*we* are more loaded than anyone else) will
3492 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003493 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 * appear as very large values with unsigned longs.
3495 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003496 if (max_load <= busiest_load_per_task)
3497 goto out_balanced;
3498
3499 /*
3500 * In the presence of smp nice balancing, certain scenarios can have
3501 * max load less than avg load(as we skip the groups at or below
3502 * its cpu_power, while calculating max_load..)
3503 */
3504 if (max_load < avg_load) {
3505 *imbalance = 0;
3506 goto small_imbalance;
3507 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003508
3509 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003510 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003513 *imbalance = min(max_pull * busiest->__cpu_power,
3514 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 / SCHED_LOAD_SCALE;
3516
Peter Williams2dd73a42006-06-27 02:54:34 -07003517 /*
3518 * if *imbalance is less than the average load per runnable task
3519 * there is no gaurantee that any tasks will be moved so we'll have
3520 * a think about bumping its value to force at least one task to be
3521 * moved
3522 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003523 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003524 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003525 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Peter Williams2dd73a42006-06-27 02:54:34 -07003527small_imbalance:
3528 pwr_move = pwr_now = 0;
3529 imbn = 2;
3530 if (this_nr_running) {
3531 this_load_per_task /= this_nr_running;
3532 if (busiest_load_per_task > this_load_per_task)
3533 imbn = 1;
3534 } else
3535 this_load_per_task = SCHED_LOAD_SCALE;
3536
Ingo Molnardd41f592007-07-09 18:51:59 +02003537 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3538 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003539 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 return busiest;
3541 }
3542
3543 /*
3544 * OK, we don't have enough imbalance to justify moving tasks,
3545 * however we may be able to increase total CPU power used by
3546 * moving them.
3547 */
3548
Eric Dumazet5517d862007-05-08 00:32:57 -07003549 pwr_now += busiest->__cpu_power *
3550 min(busiest_load_per_task, max_load);
3551 pwr_now += this->__cpu_power *
3552 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 pwr_now /= SCHED_LOAD_SCALE;
3554
3555 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003556 tmp = sg_div_cpu_power(busiest,
3557 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003559 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003560 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
3562 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003563 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003564 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003565 tmp = sg_div_cpu_power(this,
3566 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003568 tmp = sg_div_cpu_power(this,
3569 busiest_load_per_task * SCHED_LOAD_SCALE);
3570 pwr_move += this->__cpu_power *
3571 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 pwr_move /= SCHED_LOAD_SCALE;
3573
3574 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003575 if (pwr_move > pwr_now)
3576 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 }
3578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 return busiest;
3580
3581out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003582#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003583 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003584 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003586 if (this == group_leader && group_leader != group_min) {
3587 *imbalance = min_load_per_task;
3588 return group_min;
3589 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003590#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003591ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 *imbalance = 0;
3593 return NULL;
3594}
3595
3596/*
3597 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3598 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003599static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003600find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003601 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003603 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003604 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 int i;
3606
3607 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003608 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003609
3610 if (!cpu_isset(i, *cpus))
3611 continue;
3612
Ingo Molnar48f24c42006-07-03 00:25:40 -07003613 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003614 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Ingo Molnardd41f592007-07-09 18:51:59 +02003616 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003617 continue;
3618
Ingo Molnardd41f592007-07-09 18:51:59 +02003619 if (wl > max_load) {
3620 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003621 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 }
3623 }
3624
3625 return busiest;
3626}
3627
3628/*
Nick Piggin77391d72005-06-25 14:57:30 -07003629 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3630 * so long as it is large enough.
3631 */
3632#define MAX_PINNED_INTERVAL 512
3633
3634/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3636 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003638static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003639 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003640 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641{
Peter Williams43010652007-08-09 11:16:46 +02003642 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003645 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003646 unsigned long flags;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003647 int unlock_aggregate;
Nick Piggin5969fe02005-09-10 00:26:19 -07003648
Mike Travis7c16ec52008-04-04 18:11:11 -07003649 cpus_setall(*cpus);
3650
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003651 unlock_aggregate = get_aggregate(sd);
3652
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003653 /*
3654 * When power savings policy is enabled for the parent domain, idle
3655 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003656 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003657 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003658 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003659 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003660 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003661 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
Ingo Molnar2d723762007-10-15 17:00:12 +02003663 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003665redo:
3666 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003667 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003668
Chen, Kenneth W06066712006-12-10 02:20:35 -08003669 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003670 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 if (!group) {
3673 schedstat_inc(sd, lb_nobusyg[idle]);
3674 goto out_balanced;
3675 }
3676
Mike Travis7c16ec52008-04-04 18:11:11 -07003677 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 if (!busiest) {
3679 schedstat_inc(sd, lb_nobusyq[idle]);
3680 goto out_balanced;
3681 }
3682
Nick Piggindb935db2005-06-25 14:57:11 -07003683 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
3685 schedstat_add(sd, lb_imbalance[idle], imbalance);
3686
Peter Williams43010652007-08-09 11:16:46 +02003687 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 if (busiest->nr_running > 1) {
3689 /*
3690 * Attempt to move tasks. If find_busiest_group has found
3691 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003692 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 * correctly treated as an imbalance.
3694 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003695 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003696 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003697 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003698 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003699 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003700 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003701
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003702 /*
3703 * some other cpu did the load balance for us.
3704 */
Peter Williams43010652007-08-09 11:16:46 +02003705 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003706 resched_cpu(this_cpu);
3707
Nick Piggin81026792005-06-25 14:57:07 -07003708 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003709 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003710 cpu_clear(cpu_of(busiest), *cpus);
3711 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003712 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003713 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 }
Nick Piggin81026792005-06-25 14:57:07 -07003716
Peter Williams43010652007-08-09 11:16:46 +02003717 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 schedstat_inc(sd, lb_failed[idle]);
3719 sd->nr_balance_failed++;
3720
3721 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003723 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003724
3725 /* don't kick the migration_thread, if the curr
3726 * task on busiest cpu can't be moved to this_cpu
3727 */
3728 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003729 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003730 all_pinned = 1;
3731 goto out_one_pinned;
3732 }
3733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 if (!busiest->active_balance) {
3735 busiest->active_balance = 1;
3736 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003737 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003739 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003740 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 wake_up_process(busiest->migration_thread);
3742
3743 /*
3744 * We've kicked active balancing, reset the failure
3745 * counter.
3746 */
Nick Piggin39507452005-06-25 14:57:09 -07003747 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 }
Nick Piggin81026792005-06-25 14:57:07 -07003749 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 sd->nr_balance_failed = 0;
3751
Nick Piggin81026792005-06-25 14:57:07 -07003752 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 /* We were unbalanced, so reset the balancing interval */
3754 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003755 } else {
3756 /*
3757 * If we've begun active balancing, start to back off. This
3758 * case may not be covered by the all_pinned logic if there
3759 * is only 1 task on the busy runqueue (because we don't call
3760 * move_tasks).
3761 */
3762 if (sd->balance_interval < sd->max_interval)
3763 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 }
3765
Peter Williams43010652007-08-09 11:16:46 +02003766 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003767 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003768 ld_moved = -1;
3769
3770 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
3772out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 schedstat_inc(sd, lb_balanced[idle]);
3774
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003775 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003776
3777out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003779 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3780 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 sd->balance_interval *= 2;
3782
Ingo Molnar48f24c42006-07-03 00:25:40 -07003783 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003784 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003785 ld_moved = -1;
3786 else
3787 ld_moved = 0;
3788out:
3789 if (unlock_aggregate)
3790 put_aggregate(sd);
3791 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
3794/*
3795 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3796 * tasks if there is an imbalance.
3797 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003798 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 * this_rq is locked.
3800 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003801static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003802load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3803 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
3805 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003806 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003808 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003809 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003810 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003811
3812 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003813
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003814 /*
3815 * When power savings policy is enabled for the parent domain, idle
3816 * sibling can pick up load irrespective of busy siblings. In this case,
3817 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003818 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003819 */
3820 if (sd->flags & SD_SHARE_CPUPOWER &&
3821 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003822 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
Ingo Molnar2d723762007-10-15 17:00:12 +02003824 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003825redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003826 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003827 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003829 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003830 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 }
3832
Mike Travis7c16ec52008-04-04 18:11:11 -07003833 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003834 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003835 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003836 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 }
3838
Nick Piggindb935db2005-06-25 14:57:11 -07003839 BUG_ON(busiest == this_rq);
3840
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003841 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003842
Peter Williams43010652007-08-09 11:16:46 +02003843 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003844 if (busiest->nr_running > 1) {
3845 /* Attempt to move tasks */
3846 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003847 /* this_rq->clock is already updated */
3848 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003849 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003850 imbalance, sd, CPU_NEWLY_IDLE,
3851 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003852 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003853
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003854 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003855 cpu_clear(cpu_of(busiest), *cpus);
3856 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003857 goto redo;
3858 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003859 }
3860
Peter Williams43010652007-08-09 11:16:46 +02003861 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003862 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003863 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3864 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003865 return -1;
3866 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003867 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
Peter Williams43010652007-08-09 11:16:46 +02003869 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003870
3871out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003872 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003873 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003874 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003875 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003876 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003877
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879}
3880
3881/*
3882 * idle_balance is called by schedule() if this_cpu is about to become
3883 * idle. Attempts to pull tasks from other CPUs.
3884 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003885static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886{
3887 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003888 int pulled_task = -1;
3889 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003890 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
3892 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003893 unsigned long interval;
3894
3895 if (!(sd->flags & SD_LOAD_BALANCE))
3896 continue;
3897
3898 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003899 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003900 pulled_task = load_balance_newidle(this_cpu, this_rq,
3901 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003902
3903 interval = msecs_to_jiffies(sd->balance_interval);
3904 if (time_after(next_balance, sd->last_balance + interval))
3905 next_balance = sd->last_balance + interval;
3906 if (pulled_task)
3907 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003909 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003910 /*
3911 * We are going idle. next_balance may be set based on
3912 * a busy processor. So reset next_balance.
3913 */
3914 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916}
3917
3918/*
3919 * active_load_balance is run by migration threads. It pushes running tasks
3920 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3921 * running on each physical CPU where possible, and avoids physical /
3922 * logical imbalances.
3923 *
3924 * Called with busiest_rq locked.
3925 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003926static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
Nick Piggin39507452005-06-25 14:57:09 -07003928 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003929 struct sched_domain *sd;
3930 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003931
Ingo Molnar48f24c42006-07-03 00:25:40 -07003932 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003933 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003934 return;
3935
3936 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
3938 /*
Nick Piggin39507452005-06-25 14:57:09 -07003939 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003940 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003941 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 */
Nick Piggin39507452005-06-25 14:57:09 -07003943 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
Nick Piggin39507452005-06-25 14:57:09 -07003945 /* move a task from busiest_rq to target_rq */
3946 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003947 update_rq_clock(busiest_rq);
3948 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Nick Piggin39507452005-06-25 14:57:09 -07003950 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003951 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003952 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003953 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003954 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
Ingo Molnar48f24c42006-07-03 00:25:40 -07003957 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003958 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Peter Williams43010652007-08-09 11:16:46 +02003960 if (move_one_task(target_rq, target_cpu, busiest_rq,
3961 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003962 schedstat_inc(sd, alb_pushed);
3963 else
3964 schedstat_inc(sd, alb_failed);
3965 }
Nick Piggin39507452005-06-25 14:57:09 -07003966 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967}
3968
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003969#ifdef CONFIG_NO_HZ
3970static struct {
3971 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003972 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003973} nohz ____cacheline_aligned = {
3974 .load_balancer = ATOMIC_INIT(-1),
3975 .cpu_mask = CPU_MASK_NONE,
3976};
3977
Christoph Lameter7835b982006-12-10 02:20:22 -08003978/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003979 * This routine will try to nominate the ilb (idle load balancing)
3980 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3981 * load balancing on behalf of all those cpus. If all the cpus in the system
3982 * go into this tickless mode, then there will be no ilb owner (as there is
3983 * no need for one) and all the cpus will sleep till the next wakeup event
3984 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003985 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003986 * For the ilb owner, tick is not stopped. And this tick will be used
3987 * for idle load balancing. ilb owner will still be part of
3988 * nohz.cpu_mask..
3989 *
3990 * While stopping the tick, this cpu will become the ilb owner if there
3991 * is no other owner. And will be the owner till that cpu becomes busy
3992 * or if all cpus in the system stop their ticks at which point
3993 * there is no need for ilb owner.
3994 *
3995 * When the ilb owner becomes busy, it nominates another owner, during the
3996 * next busy scheduler_tick()
3997 */
3998int select_nohz_load_balancer(int stop_tick)
3999{
4000 int cpu = smp_processor_id();
4001
4002 if (stop_tick) {
4003 cpu_set(cpu, nohz.cpu_mask);
4004 cpu_rq(cpu)->in_nohz_recently = 1;
4005
4006 /*
4007 * If we are going offline and still the leader, give up!
4008 */
4009 if (cpu_is_offline(cpu) &&
4010 atomic_read(&nohz.load_balancer) == cpu) {
4011 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4012 BUG();
4013 return 0;
4014 }
4015
4016 /* time for ilb owner also to sleep */
4017 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4018 if (atomic_read(&nohz.load_balancer) == cpu)
4019 atomic_set(&nohz.load_balancer, -1);
4020 return 0;
4021 }
4022
4023 if (atomic_read(&nohz.load_balancer) == -1) {
4024 /* make me the ilb owner */
4025 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4026 return 1;
4027 } else if (atomic_read(&nohz.load_balancer) == cpu)
4028 return 1;
4029 } else {
4030 if (!cpu_isset(cpu, nohz.cpu_mask))
4031 return 0;
4032
4033 cpu_clear(cpu, nohz.cpu_mask);
4034
4035 if (atomic_read(&nohz.load_balancer) == cpu)
4036 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4037 BUG();
4038 }
4039 return 0;
4040}
4041#endif
4042
4043static DEFINE_SPINLOCK(balancing);
4044
4045/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004046 * It checks each scheduling domain to see if it is due to be balanced,
4047 * and initiates a balancing operation if so.
4048 *
4049 * Balancing parameters are set up in arch_init_sched_domains.
4050 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004051static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08004052{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004053 int balance = 1;
4054 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004055 unsigned long interval;
4056 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004057 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004058 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004059 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07004060 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004062 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 if (!(sd->flags & SD_LOAD_BALANCE))
4064 continue;
4065
4066 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004067 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 interval *= sd->busy_factor;
4069
4070 /* scale ms to jiffies */
4071 interval = msecs_to_jiffies(interval);
4072 if (unlikely(!interval))
4073 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004074 if (interval > HZ*NR_CPUS/10)
4075 interval = HZ*NR_CPUS/10;
4076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Christoph Lameter08c183f2006-12-10 02:20:29 -08004078 if (sd->flags & SD_SERIALIZE) {
4079 if (!spin_trylock(&balancing))
4080 goto out;
4081 }
4082
Christoph Lameterc9819f42006-12-10 02:20:25 -08004083 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07004084 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004085 /*
4086 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004087 * longer idle, or one of our SMT siblings is
4088 * not idle.
4089 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004090 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004092 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08004094 if (sd->flags & SD_SERIALIZE)
4095 spin_unlock(&balancing);
4096out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004097 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004098 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004099 update_next_balance = 1;
4100 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004101
4102 /*
4103 * Stop the load balance at this level. There is another
4104 * CPU in our sched group which is doing load balancing more
4105 * actively.
4106 */
4107 if (!balance)
4108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004110
4111 /*
4112 * next_balance will be updated only when there is a need.
4113 * When the cpu is attached to null domain for ex, it will not be
4114 * updated.
4115 */
4116 if (likely(update_next_balance))
4117 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004118}
4119
4120/*
4121 * run_rebalance_domains is triggered when needed from the scheduler tick.
4122 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4123 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4124 */
4125static void run_rebalance_domains(struct softirq_action *h)
4126{
Ingo Molnardd41f592007-07-09 18:51:59 +02004127 int this_cpu = smp_processor_id();
4128 struct rq *this_rq = cpu_rq(this_cpu);
4129 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4130 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004131
Ingo Molnardd41f592007-07-09 18:51:59 +02004132 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004133
4134#ifdef CONFIG_NO_HZ
4135 /*
4136 * If this cpu is the owner for idle load balancing, then do the
4137 * balancing on behalf of the other idle cpus whose ticks are
4138 * stopped.
4139 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004140 if (this_rq->idle_at_tick &&
4141 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004142 cpumask_t cpus = nohz.cpu_mask;
4143 struct rq *rq;
4144 int balance_cpu;
4145
Ingo Molnardd41f592007-07-09 18:51:59 +02004146 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004147 for_each_cpu_mask(balance_cpu, cpus) {
4148 /*
4149 * If this cpu gets work to do, stop the load balancing
4150 * work being done for other cpus. Next load
4151 * balancing owner will pick it up.
4152 */
4153 if (need_resched())
4154 break;
4155
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004156 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004157
4158 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004159 if (time_after(this_rq->next_balance, rq->next_balance))
4160 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004161 }
4162 }
4163#endif
4164}
4165
4166/*
4167 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4168 *
4169 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4170 * idle load balancing owner or decide to stop the periodic load balancing,
4171 * if the whole system is idle.
4172 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004173static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004174{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004175#ifdef CONFIG_NO_HZ
4176 /*
4177 * If we were in the nohz mode recently and busy at the current
4178 * scheduler tick, then check if we need to nominate new idle
4179 * load balancer.
4180 */
4181 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4182 rq->in_nohz_recently = 0;
4183
4184 if (atomic_read(&nohz.load_balancer) == cpu) {
4185 cpu_clear(cpu, nohz.cpu_mask);
4186 atomic_set(&nohz.load_balancer, -1);
4187 }
4188
4189 if (atomic_read(&nohz.load_balancer) == -1) {
4190 /*
4191 * simple selection for now: Nominate the
4192 * first cpu in the nohz list to be the next
4193 * ilb owner.
4194 *
4195 * TBD: Traverse the sched domains and nominate
4196 * the nearest cpu in the nohz.cpu_mask.
4197 */
4198 int ilb = first_cpu(nohz.cpu_mask);
4199
Mike Travis434d53b2008-04-04 18:11:04 -07004200 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004201 resched_cpu(ilb);
4202 }
4203 }
4204
4205 /*
4206 * If this cpu is idle and doing idle load balancing for all the
4207 * cpus with ticks stopped, is it time for that to stop?
4208 */
4209 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4210 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4211 resched_cpu(cpu);
4212 return;
4213 }
4214
4215 /*
4216 * If this cpu is idle and the idle load balancing is done by
4217 * someone else, then no need raise the SCHED_SOFTIRQ
4218 */
4219 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4220 cpu_isset(cpu, nohz.cpu_mask))
4221 return;
4222#endif
4223 if (time_after_eq(jiffies, rq->next_balance))
4224 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225}
Ingo Molnardd41f592007-07-09 18:51:59 +02004226
4227#else /* CONFIG_SMP */
4228
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229/*
4230 * on UP we do not need to balance between CPUs:
4231 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004232static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233{
4234}
Ingo Molnardd41f592007-07-09 18:51:59 +02004235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236#endif
4237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238DEFINE_PER_CPU(struct kernel_stat, kstat);
4239
4240EXPORT_PER_CPU_SYMBOL(kstat);
4241
4242/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004243 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4244 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004246unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004249 u64 ns, delta_exec;
4250 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004251
Ingo Molnar41b86e92007-07-09 18:51:58 +02004252 rq = task_rq_lock(p, &flags);
4253 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004254 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004255 update_rq_clock(rq);
4256 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004257 if ((s64)delta_exec > 0)
4258 ns += delta_exec;
4259 }
4260 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 return ns;
4263}
4264
4265/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 * Account user cpu time to a process.
4267 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 * @cputime: the cpu time spent in user space since the last update
4269 */
4270void account_user_time(struct task_struct *p, cputime_t cputime)
4271{
4272 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4273 cputime64_t tmp;
4274
4275 p->utime = cputime_add(p->utime, cputime);
4276
4277 /* Add user time to cpustat. */
4278 tmp = cputime_to_cputime64(cputime);
4279 if (TASK_NICE(p) > 0)
4280 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4281 else
4282 cpustat->user = cputime64_add(cpustat->user, tmp);
4283}
4284
4285/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004286 * Account guest cpu time to a process.
4287 * @p: the process that the cpu time gets accounted to
4288 * @cputime: the cpu time spent in virtual machine since the last update
4289 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004290static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004291{
4292 cputime64_t tmp;
4293 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4294
4295 tmp = cputime_to_cputime64(cputime);
4296
4297 p->utime = cputime_add(p->utime, cputime);
4298 p->gtime = cputime_add(p->gtime, cputime);
4299
4300 cpustat->user = cputime64_add(cpustat->user, tmp);
4301 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4302}
4303
4304/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004305 * Account scaled user cpu time to a process.
4306 * @p: the process that the cpu time gets accounted to
4307 * @cputime: the cpu time spent in user space since the last update
4308 */
4309void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4310{
4311 p->utimescaled = cputime_add(p->utimescaled, cputime);
4312}
4313
4314/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 * Account system cpu time to a process.
4316 * @p: the process that the cpu time gets accounted to
4317 * @hardirq_offset: the offset to subtract from hardirq_count()
4318 * @cputime: the cpu time spent in kernel space since the last update
4319 */
4320void account_system_time(struct task_struct *p, int hardirq_offset,
4321 cputime_t cputime)
4322{
4323 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004324 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 cputime64_t tmp;
4326
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004327 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4328 account_guest_time(p, cputime);
4329 return;
4330 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 p->stime = cputime_add(p->stime, cputime);
4333
4334 /* Add system time to cpustat. */
4335 tmp = cputime_to_cputime64(cputime);
4336 if (hardirq_count() - hardirq_offset)
4337 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4338 else if (softirq_count())
4339 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004340 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004342 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4344 else
4345 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4346 /* Account for system time used */
4347 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348}
4349
4350/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004351 * Account scaled system cpu time to a process.
4352 * @p: the process that the cpu time gets accounted to
4353 * @hardirq_offset: the offset to subtract from hardirq_count()
4354 * @cputime: the cpu time spent in kernel space since the last update
4355 */
4356void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4357{
4358 p->stimescaled = cputime_add(p->stimescaled, cputime);
4359}
4360
4361/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 * Account for involuntary wait time.
4363 * @p: the process from which the cpu time has been stolen
4364 * @steal: the cpu time spent in involuntary wait
4365 */
4366void account_steal_time(struct task_struct *p, cputime_t steal)
4367{
4368 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4369 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004370 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
4372 if (p == rq->idle) {
4373 p->stime = cputime_add(p->stime, steal);
4374 if (atomic_read(&rq->nr_iowait) > 0)
4375 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4376 else
4377 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004378 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4380}
4381
Christoph Lameter7835b982006-12-10 02:20:22 -08004382/*
4383 * This function gets called by the timer code, with HZ frequency.
4384 * We call it with interrupts disabled.
4385 *
4386 * It also gets called by the fork code, when changing the parent's
4387 * timeslices.
4388 */
4389void scheduler_tick(void)
4390{
Christoph Lameter7835b982006-12-10 02:20:22 -08004391 int cpu = smp_processor_id();
4392 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004393 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004394
4395 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004396
Ingo Molnardd41f592007-07-09 18:51:59 +02004397 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004398 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004399 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004400 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004401 spin_unlock(&rq->lock);
4402
Christoph Lametere418e1c2006-12-10 02:20:23 -08004403#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004404 rq->idle_at_tick = idle_cpu(cpu);
4405 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004406#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407}
4408
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004409#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
4410 defined(CONFIG_PREEMPT_TRACER))
4411
4412static inline unsigned long get_parent_ip(unsigned long addr)
4413{
4414 if (in_lock_functions(addr)) {
4415 addr = CALLER_ADDR2;
4416 if (in_lock_functions(addr))
4417 addr = CALLER_ADDR3;
4418 }
4419 return addr;
4420}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Srinivasa Ds43627582008-02-23 15:24:04 -08004422void __kprobes add_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((preempt_count() < 0)))
4429 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004430#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004432#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 /*
4434 * Spinlock count overflowing soon?
4435 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004436 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4437 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004438#endif
4439 if (preempt_count() == val)
4440 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441}
4442EXPORT_SYMBOL(add_preempt_count);
4443
Srinivasa Ds43627582008-02-23 15:24:04 -08004444void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004446#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 /*
4448 * Underflow?
4449 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004450 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4451 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 /*
4453 * Is the spinlock portion underflowing?
4454 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004455 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4456 !(preempt_count() & PREEMPT_MASK)))
4457 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004458#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004459
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004460 if (preempt_count() == val)
4461 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 preempt_count() -= val;
4463}
4464EXPORT_SYMBOL(sub_preempt_count);
4465
4466#endif
4467
4468/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004469 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004471static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472{
Satyam Sharma838225b2007-10-24 18:23:50 +02004473 struct pt_regs *regs = get_irq_regs();
4474
4475 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4476 prev->comm, prev->pid, preempt_count());
4477
Ingo Molnardd41f592007-07-09 18:51:59 +02004478 debug_show_held_locks(prev);
4479 if (irqs_disabled())
4480 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004481
4482 if (regs)
4483 show_regs(regs);
4484 else
4485 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Ingo Molnardd41f592007-07-09 18:51:59 +02004488/*
4489 * Various schedule()-time debugging checks and statistics:
4490 */
4491static inline void schedule_debug(struct task_struct *prev)
4492{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004494 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 * schedule() atomically, we ignore that path for now.
4496 * Otherwise, whine if we are scheduling when we should not be.
4497 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004498 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4499 __schedule_bug(prev);
4500
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4502
Ingo Molnar2d723762007-10-15 17:00:12 +02004503 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004504#ifdef CONFIG_SCHEDSTATS
4505 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004506 schedstat_inc(this_rq(), bkl_count);
4507 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004508 }
4509#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004510}
4511
4512/*
4513 * Pick up the highest-prio task:
4514 */
4515static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004516pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004517{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004518 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004519 struct task_struct *p;
4520
4521 /*
4522 * Optimization: we know that if all tasks are in
4523 * the fair class we can call that function directly:
4524 */
4525 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004526 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004527 if (likely(p))
4528 return p;
4529 }
4530
4531 class = sched_class_highest;
4532 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004533 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004534 if (p)
4535 return p;
4536 /*
4537 * Will never be NULL as the idle class always
4538 * returns a non-NULL p:
4539 */
4540 class = class->next;
4541 }
4542}
4543
4544/*
4545 * schedule() is the main scheduler function.
4546 */
4547asmlinkage void __sched schedule(void)
4548{
4549 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004550 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004551 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004552 int cpu;
4553
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554need_resched:
4555 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004556 cpu = smp_processor_id();
4557 rq = cpu_rq(cpu);
4558 rcu_qsctr_inc(cpu);
4559 prev = rq->curr;
4560 switch_count = &prev->nivcsw;
4561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 release_kernel_lock(prev);
4563need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
Ingo Molnardd41f592007-07-09 18:51:59 +02004565 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004567 hrtick_clear(rq);
4568
Ingo Molnar1e819952007-10-15 17:00:13 +02004569 /*
4570 * Do the rq-clock update outside the rq lock:
4571 */
4572 local_irq_disable();
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004573 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004574 spin_lock(&rq->lock);
4575 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Ingo Molnardd41f592007-07-09 18:51:59 +02004577 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4578 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004579 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004580 prev->state = TASK_RUNNING;
4581 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004582 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004583 }
4584 switch_count = &prev->nvcsw;
4585 }
4586
Steven Rostedt9a897c52008-01-25 21:08:22 +01004587#ifdef CONFIG_SMP
4588 if (prev->sched_class->pre_schedule)
4589 prev->sched_class->pre_schedule(rq, prev);
4590#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004591
Ingo Molnardd41f592007-07-09 18:51:59 +02004592 if (unlikely(!rq->nr_running))
4593 idle_balance(cpu, rq);
4594
Ingo Molnar31ee5292007-08-09 11:16:49 +02004595 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004596 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01004599 sched_info_switch(prev, next);
4600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 rq->nr_switches++;
4602 rq->curr = next;
4603 ++*switch_count;
4604
Ingo Molnardd41f592007-07-09 18:51:59 +02004605 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004606 /*
4607 * the context switch might have flipped the stack from under
4608 * us, hence refresh the local variables.
4609 */
4610 cpu = smp_processor_id();
4611 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 } else
4613 spin_unlock_irq(&rq->lock);
4614
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004615 hrtick_set(rq);
4616
4617 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 preempt_enable_no_resched();
4621 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4622 goto need_resched;
4623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624EXPORT_SYMBOL(schedule);
4625
4626#ifdef CONFIG_PREEMPT
4627/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004628 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004629 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 * occur there and call schedule directly.
4631 */
4632asmlinkage void __sched preempt_schedule(void)
4633{
4634 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004635
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 /*
4637 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004638 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004640 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 return;
4642
Andi Kleen3a5c3592007-10-15 17:00:14 +02004643 do {
4644 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004645 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004646 sub_preempt_count(PREEMPT_ACTIVE);
4647
4648 /*
4649 * Check again in case we missed a preemption opportunity
4650 * between schedule and now.
4651 */
4652 barrier();
4653 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655EXPORT_SYMBOL(preempt_schedule);
4656
4657/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004658 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 * off of irq context.
4660 * Note, that this is called and return with irqs disabled. This will
4661 * protect us against recursive calling from irq.
4662 */
4663asmlinkage void __sched preempt_schedule_irq(void)
4664{
4665 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004666
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004667 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 BUG_ON(ti->preempt_count || !irqs_disabled());
4669
Andi Kleen3a5c3592007-10-15 17:00:14 +02004670 do {
4671 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004672 local_irq_enable();
4673 schedule();
4674 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004675 sub_preempt_count(PREEMPT_ACTIVE);
4676
4677 /*
4678 * Check again in case we missed a preemption opportunity
4679 * between schedule and now.
4680 */
4681 barrier();
4682 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683}
4684
4685#endif /* CONFIG_PREEMPT */
4686
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004687int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4688 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004690 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692EXPORT_SYMBOL(default_wake_function);
4693
4694/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004695 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4696 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 * number) then we wake all the non-exclusive tasks and one exclusive task.
4698 *
4699 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004700 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4702 */
4703static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4704 int nr_exclusive, int sync, void *key)
4705{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004706 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004708 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004709 unsigned flags = curr->flags;
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004712 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 break;
4714 }
4715}
4716
4717/**
4718 * __wake_up - wake up threads blocked on a waitqueue.
4719 * @q: the waitqueue
4720 * @mode: which threads
4721 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004722 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004724void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004725 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
4727 unsigned long flags;
4728
4729 spin_lock_irqsave(&q->lock, flags);
4730 __wake_up_common(q, mode, nr_exclusive, 0, key);
4731 spin_unlock_irqrestore(&q->lock, flags);
4732}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733EXPORT_SYMBOL(__wake_up);
4734
4735/*
4736 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4737 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004738void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739{
4740 __wake_up_common(q, mode, 1, 0, NULL);
4741}
4742
4743/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004744 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 * @q: the waitqueue
4746 * @mode: which threads
4747 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4748 *
4749 * The sync wakeup differs that the waker knows that it will schedule
4750 * away soon, so while the target thread will be woken up, it will not
4751 * be migrated to another CPU - ie. the two threads are 'synchronized'
4752 * with each other. This can prevent needless bouncing between CPUs.
4753 *
4754 * On UP it can prevent extra preemption.
4755 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004756void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004757__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758{
4759 unsigned long flags;
4760 int sync = 1;
4761
4762 if (unlikely(!q))
4763 return;
4764
4765 if (unlikely(!nr_exclusive))
4766 sync = 0;
4767
4768 spin_lock_irqsave(&q->lock, flags);
4769 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4770 spin_unlock_irqrestore(&q->lock, flags);
4771}
4772EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4773
Ingo Molnarb15136e2007-10-24 18:23:48 +02004774void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
4776 unsigned long flags;
4777
4778 spin_lock_irqsave(&x->wait.lock, flags);
4779 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004780 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 spin_unlock_irqrestore(&x->wait.lock, flags);
4782}
4783EXPORT_SYMBOL(complete);
4784
Ingo Molnarb15136e2007-10-24 18:23:48 +02004785void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786{
4787 unsigned long flags;
4788
4789 spin_lock_irqsave(&x->wait.lock, flags);
4790 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004791 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 spin_unlock_irqrestore(&x->wait.lock, flags);
4793}
4794EXPORT_SYMBOL(complete_all);
4795
Andi Kleen8cbbe862007-10-15 17:00:14 +02004796static inline long __sched
4797do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 if (!x->done) {
4800 DECLARE_WAITQUEUE(wait, current);
4801
4802 wait.flags |= WQ_FLAG_EXCLUSIVE;
4803 __add_wait_queue_tail(&x->wait, &wait);
4804 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004805 if ((state == TASK_INTERRUPTIBLE &&
4806 signal_pending(current)) ||
4807 (state == TASK_KILLABLE &&
4808 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004809 __remove_wait_queue(&x->wait, &wait);
4810 return -ERESTARTSYS;
4811 }
4812 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004814 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004816 if (!timeout) {
4817 __remove_wait_queue(&x->wait, &wait);
4818 return timeout;
4819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 } while (!x->done);
4821 __remove_wait_queue(&x->wait, &wait);
4822 }
4823 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004824 return timeout;
4825}
4826
4827static long __sched
4828wait_for_common(struct completion *x, long timeout, int state)
4829{
4830 might_sleep();
4831
4832 spin_lock_irq(&x->wait.lock);
4833 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004835 return timeout;
4836}
4837
Ingo Molnarb15136e2007-10-24 18:23:48 +02004838void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004839{
4840 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841}
4842EXPORT_SYMBOL(wait_for_completion);
4843
Ingo Molnarb15136e2007-10-24 18:23:48 +02004844unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4846{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004847 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848}
4849EXPORT_SYMBOL(wait_for_completion_timeout);
4850
Andi Kleen8cbbe862007-10-15 17:00:14 +02004851int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
Andi Kleen51e97992007-10-18 21:32:55 +02004853 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4854 if (t == -ERESTARTSYS)
4855 return t;
4856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857}
4858EXPORT_SYMBOL(wait_for_completion_interruptible);
4859
Ingo Molnarb15136e2007-10-24 18:23:48 +02004860unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861wait_for_completion_interruptible_timeout(struct completion *x,
4862 unsigned long timeout)
4863{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004864 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
4866EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4867
Matthew Wilcox009e5772007-12-06 12:29:54 -05004868int __sched wait_for_completion_killable(struct completion *x)
4869{
4870 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4871 if (t == -ERESTARTSYS)
4872 return t;
4873 return 0;
4874}
4875EXPORT_SYMBOL(wait_for_completion_killable);
4876
Andi Kleen8cbbe862007-10-15 17:00:14 +02004877static long __sched
4878sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004879{
4880 unsigned long flags;
4881 wait_queue_t wait;
4882
4883 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Andi Kleen8cbbe862007-10-15 17:00:14 +02004885 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
Andi Kleen8cbbe862007-10-15 17:00:14 +02004887 spin_lock_irqsave(&q->lock, flags);
4888 __add_wait_queue(q, &wait);
4889 spin_unlock(&q->lock);
4890 timeout = schedule_timeout(timeout);
4891 spin_lock_irq(&q->lock);
4892 __remove_wait_queue(q, &wait);
4893 spin_unlock_irqrestore(&q->lock, flags);
4894
4895 return timeout;
4896}
4897
4898void __sched interruptible_sleep_on(wait_queue_head_t *q)
4899{
4900 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902EXPORT_SYMBOL(interruptible_sleep_on);
4903
Ingo Molnar0fec1712007-07-09 18:52:01 +02004904long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004905interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004907 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4910
Ingo Molnar0fec1712007-07-09 18:52:01 +02004911void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004913 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915EXPORT_SYMBOL(sleep_on);
4916
Ingo Molnar0fec1712007-07-09 18:52:01 +02004917long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004919 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921EXPORT_SYMBOL(sleep_on_timeout);
4922
Ingo Molnarb29739f2006-06-27 02:54:51 -07004923#ifdef CONFIG_RT_MUTEXES
4924
4925/*
4926 * rt_mutex_setprio - set the current priority of a task
4927 * @p: task
4928 * @prio: prio value (kernel-internal form)
4929 *
4930 * This function changes the 'effective' priority of a task. It does
4931 * not touch ->normal_prio like __setscheduler().
4932 *
4933 * Used by the rt_mutex code to implement priority inheritance logic.
4934 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004935void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004936{
4937 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004938 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004939 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004940 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004941
4942 BUG_ON(prio < 0 || prio > MAX_PRIO);
4943
4944 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004945 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004946
Andrew Mortond5f9f942007-05-08 20:27:06 -07004947 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004948 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004949 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004950 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004951 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004952 if (running)
4953 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004954
4955 if (rt_prio(prio))
4956 p->sched_class = &rt_sched_class;
4957 else
4958 p->sched_class = &fair_sched_class;
4959
Ingo Molnarb29739f2006-06-27 02:54:51 -07004960 p->prio = prio;
4961
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004962 if (running)
4963 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004964 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004965 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004966
4967 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004968 }
4969 task_rq_unlock(rq, &flags);
4970}
4971
4972#endif
4973
Ingo Molnar36c8b582006-07-03 00:25:41 -07004974void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975{
Ingo Molnardd41f592007-07-09 18:51:59 +02004976 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004978 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
4980 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4981 return;
4982 /*
4983 * We have to be careful, if called from sys_setpriority(),
4984 * the task might be in the middle of scheduling on another CPU.
4985 */
4986 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004987 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 /*
4989 * The RT priorities are set via sched_setscheduler(), but we still
4990 * allow the 'normal' nice value to be set - but as expected
4991 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004992 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004994 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 p->static_prio = NICE_TO_PRIO(nice);
4996 goto out_unlock;
4997 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004998 on_rq = p->se.on_rq;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02004999 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005000 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07005003 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005004 old_prio = p->prio;
5005 p->prio = effective_prio(p);
5006 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
Ingo Molnardd41f592007-07-09 18:51:59 +02005008 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005009 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07005011 * If the task increased its priority or is running and
5012 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005014 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 resched_task(rq->curr);
5016 }
5017out_unlock:
5018 task_rq_unlock(rq, &flags);
5019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020EXPORT_SYMBOL(set_user_nice);
5021
Matt Mackalle43379f2005-05-01 08:59:00 -07005022/*
5023 * can_nice - check if a task can reduce its nice value
5024 * @p: task
5025 * @nice: nice value
5026 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005027int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005028{
Matt Mackall024f4742005-08-18 11:24:19 -07005029 /* convert nice value [19,-20] to rlimit style value [1,40] */
5030 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005031
Matt Mackalle43379f2005-05-01 08:59:00 -07005032 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5033 capable(CAP_SYS_NICE));
5034}
5035
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036#ifdef __ARCH_WANT_SYS_NICE
5037
5038/*
5039 * sys_nice - change the priority of the current process.
5040 * @increment: priority increment
5041 *
5042 * sys_setpriority is a more generic, but much slower function that
5043 * does similar things.
5044 */
5045asmlinkage long sys_nice(int increment)
5046{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005047 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048
5049 /*
5050 * Setpriority might change our priority at the same moment.
5051 * We don't have to worry. Conceptually one call occurs first
5052 * and we have a single winner.
5053 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005054 if (increment < -40)
5055 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 if (increment > 40)
5057 increment = 40;
5058
5059 nice = PRIO_TO_NICE(current->static_prio) + increment;
5060 if (nice < -20)
5061 nice = -20;
5062 if (nice > 19)
5063 nice = 19;
5064
Matt Mackalle43379f2005-05-01 08:59:00 -07005065 if (increment < 0 && !can_nice(current, nice))
5066 return -EPERM;
5067
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 retval = security_task_setnice(current, nice);
5069 if (retval)
5070 return retval;
5071
5072 set_user_nice(current, nice);
5073 return 0;
5074}
5075
5076#endif
5077
5078/**
5079 * task_prio - return the priority value of a given task.
5080 * @p: the task in question.
5081 *
5082 * This is the priority value as seen by users in /proc.
5083 * RT tasks are offset by -200. Normal tasks are centered
5084 * around 0, value goes from -16 to +15.
5085 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005086int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087{
5088 return p->prio - MAX_RT_PRIO;
5089}
5090
5091/**
5092 * task_nice - return the nice value of a given task.
5093 * @p: the task in question.
5094 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005095int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096{
5097 return TASK_NICE(p);
5098}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005099EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
5101/**
5102 * idle_cpu - is a given cpu idle currently?
5103 * @cpu: the processor in question.
5104 */
5105int idle_cpu(int cpu)
5106{
5107 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5108}
5109
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110/**
5111 * idle_task - return the idle task for a given cpu.
5112 * @cpu: the processor in question.
5113 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005114struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115{
5116 return cpu_rq(cpu)->idle;
5117}
5118
5119/**
5120 * find_process_by_pid - find a process with a matching PID value.
5121 * @pid: the pid in question.
5122 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005123static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005125 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126}
5127
5128/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005129static void
5130__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131{
Ingo Molnardd41f592007-07-09 18:51:59 +02005132 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005133
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005135 switch (p->policy) {
5136 case SCHED_NORMAL:
5137 case SCHED_BATCH:
5138 case SCHED_IDLE:
5139 p->sched_class = &fair_sched_class;
5140 break;
5141 case SCHED_FIFO:
5142 case SCHED_RR:
5143 p->sched_class = &rt_sched_class;
5144 break;
5145 }
5146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005148 p->normal_prio = normal_prio(p);
5149 /* we are holding p->pi_lock already */
5150 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005151 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152}
5153
5154/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005155 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 * @p: the task in question.
5157 * @policy: new policy.
5158 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005159 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005160 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005162int sched_setscheduler(struct task_struct *p, int policy,
5163 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005165 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005167 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005168 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
Steven Rostedt66e53932006-06-27 02:54:44 -07005170 /* may grab non-irq protected spin_locks */
5171 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172recheck:
5173 /* double check policy once rq lock held */
5174 if (policy < 0)
5175 policy = oldpolicy = p->policy;
5176 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005177 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5178 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 /*
5181 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005182 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5183 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 */
5185 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005186 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005187 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005189 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 return -EINVAL;
5191
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005192 /*
5193 * Allow unprivileged RT tasks to decrease priority:
5194 */
5195 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005196 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005197 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005198
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005199 if (!lock_task_sighand(p, &flags))
5200 return -ESRCH;
5201 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5202 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005203
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005204 /* can't set/change the rt policy */
5205 if (policy != p->policy && !rlim_rtprio)
5206 return -EPERM;
5207
5208 /* can't increase priority */
5209 if (param->sched_priority > p->rt_priority &&
5210 param->sched_priority > rlim_rtprio)
5211 return -EPERM;
5212 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005213 /*
5214 * Like positive nice levels, dont allow tasks to
5215 * move out of SCHED_IDLE either:
5216 */
5217 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5218 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005219
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005220 /* can't change other user's priorities */
5221 if ((current->euid != p->euid) &&
5222 (current->euid != p->uid))
5223 return -EPERM;
5224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005226#ifdef CONFIG_RT_GROUP_SCHED
5227 /*
5228 * Do not allow realtime tasks into groups that have no runtime
5229 * assigned.
5230 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02005231 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005232 return -EPERM;
5233#endif
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 retval = security_task_setscheduler(p, policy, param);
5236 if (retval)
5237 return retval;
5238 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005239 * make sure no PI-waiters arrive (or leave) while we are
5240 * changing the priority of the task:
5241 */
5242 spin_lock_irqsave(&p->pi_lock, flags);
5243 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 * To be able to change p->policy safely, the apropriate
5245 * runqueue lock must be held.
5246 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005247 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 /* recheck policy now with rq lock held */
5249 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5250 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005251 __task_rq_unlock(rq);
5252 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 goto recheck;
5254 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005255 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005256 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005257 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005258 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005259 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005260 if (running)
5261 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005262
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005264 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005265
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005266 if (running)
5267 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005268 if (on_rq) {
5269 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005270
5271 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005273 __task_rq_unlock(rq);
5274 spin_unlock_irqrestore(&p->pi_lock, flags);
5275
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005276 rt_mutex_adjust_pi(p);
5277
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 return 0;
5279}
5280EXPORT_SYMBOL_GPL(sched_setscheduler);
5281
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005282static int
5283do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 struct sched_param lparam;
5286 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005287 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
5289 if (!param || pid < 0)
5290 return -EINVAL;
5291 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5292 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005293
5294 rcu_read_lock();
5295 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005297 if (p != NULL)
5298 retval = sched_setscheduler(p, policy, &lparam);
5299 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005300
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 return retval;
5302}
5303
5304/**
5305 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5306 * @pid: the pid in question.
5307 * @policy: new policy.
5308 * @param: structure containing the new RT priority.
5309 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005310asmlinkage long
5311sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312{
Jason Baronc21761f2006-01-18 17:43:03 -08005313 /* negative values for policy are not valid */
5314 if (policy < 0)
5315 return -EINVAL;
5316
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 return do_sched_setscheduler(pid, policy, param);
5318}
5319
5320/**
5321 * sys_sched_setparam - set/change the RT priority of a thread
5322 * @pid: the pid in question.
5323 * @param: structure containing the new RT priority.
5324 */
5325asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5326{
5327 return do_sched_setscheduler(pid, -1, param);
5328}
5329
5330/**
5331 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5332 * @pid: the pid in question.
5333 */
5334asmlinkage long sys_sched_getscheduler(pid_t pid)
5335{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005336 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005337 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
5339 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005340 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
5342 retval = -ESRCH;
5343 read_lock(&tasklist_lock);
5344 p = find_process_by_pid(pid);
5345 if (p) {
5346 retval = security_task_getscheduler(p);
5347 if (!retval)
5348 retval = p->policy;
5349 }
5350 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 return retval;
5352}
5353
5354/**
5355 * sys_sched_getscheduler - get the RT priority of a thread
5356 * @pid: the pid in question.
5357 * @param: structure containing the RT priority.
5358 */
5359asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5360{
5361 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005362 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005363 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364
5365 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005366 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
5368 read_lock(&tasklist_lock);
5369 p = find_process_by_pid(pid);
5370 retval = -ESRCH;
5371 if (!p)
5372 goto out_unlock;
5373
5374 retval = security_task_getscheduler(p);
5375 if (retval)
5376 goto out_unlock;
5377
5378 lp.sched_priority = p->rt_priority;
5379 read_unlock(&tasklist_lock);
5380
5381 /*
5382 * This one might sleep, we cannot do it with a spinlock held ...
5383 */
5384 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5385
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 return retval;
5387
5388out_unlock:
5389 read_unlock(&tasklist_lock);
5390 return retval;
5391}
5392
Mike Travisb53e9212008-04-04 18:11:08 -07005393long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005396 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005397 struct task_struct *p;
5398 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005400 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 read_lock(&tasklist_lock);
5402
5403 p = find_process_by_pid(pid);
5404 if (!p) {
5405 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005406 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 return -ESRCH;
5408 }
5409
5410 /*
5411 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005412 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 * usage count and then drop tasklist_lock.
5414 */
5415 get_task_struct(p);
5416 read_unlock(&tasklist_lock);
5417
5418 retval = -EPERM;
5419 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5420 !capable(CAP_SYS_NICE))
5421 goto out_unlock;
5422
David Quigleye7834f82006-06-23 02:03:59 -07005423 retval = security_task_setscheduler(p, 0, NULL);
5424 if (retval)
5425 goto out_unlock;
5426
Mike Travisf9a86fc2008-04-04 18:11:07 -07005427 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005429 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005430 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
Paul Menage8707d8b2007-10-18 23:40:22 -07005432 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005433 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005434 if (!cpus_subset(new_mask, cpus_allowed)) {
5435 /*
5436 * We must have raced with a concurrent cpuset
5437 * update. Just reset the cpus_allowed to the
5438 * cpuset's cpus_allowed
5439 */
5440 new_mask = cpus_allowed;
5441 goto again;
5442 }
5443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444out_unlock:
5445 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005446 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 return retval;
5448}
5449
5450static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5451 cpumask_t *new_mask)
5452{
5453 if (len < sizeof(cpumask_t)) {
5454 memset(new_mask, 0, sizeof(cpumask_t));
5455 } else if (len > sizeof(cpumask_t)) {
5456 len = sizeof(cpumask_t);
5457 }
5458 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5459}
5460
5461/**
5462 * sys_sched_setaffinity - set the cpu affinity of a process
5463 * @pid: pid of the process
5464 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5465 * @user_mask_ptr: user-space pointer to the new cpu mask
5466 */
5467asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5468 unsigned long __user *user_mask_ptr)
5469{
5470 cpumask_t new_mask;
5471 int retval;
5472
5473 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5474 if (retval)
5475 return retval;
5476
Mike Travisb53e9212008-04-04 18:11:08 -07005477 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478}
5479
5480/*
5481 * Represents all cpu's present in the system
5482 * In systems capable of hotplug, this map could dynamically grow
5483 * as new cpu's are detected in the system via any platform specific
5484 * method, such as ACPI for e.g.
5485 */
5486
Andi Kleen4cef0c62006-01-11 22:44:57 +01005487cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488EXPORT_SYMBOL(cpu_present_map);
5489
5490#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005491cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005492EXPORT_SYMBOL(cpu_online_map);
5493
Andi Kleen4cef0c62006-01-11 22:44:57 +01005494cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005495EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496#endif
5497
5498long sched_getaffinity(pid_t pid, cpumask_t *mask)
5499{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005500 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005503 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 read_lock(&tasklist_lock);
5505
5506 retval = -ESRCH;
5507 p = find_process_by_pid(pid);
5508 if (!p)
5509 goto out_unlock;
5510
David Quigleye7834f82006-06-23 02:03:59 -07005511 retval = security_task_getscheduler(p);
5512 if (retval)
5513 goto out_unlock;
5514
Jack Steiner2f7016d2006-02-01 03:05:18 -08005515 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
5517out_unlock:
5518 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005519 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
Ulrich Drepper9531b622007-08-09 11:16:46 +02005521 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522}
5523
5524/**
5525 * sys_sched_getaffinity - get the cpu affinity of a process
5526 * @pid: pid of the process
5527 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5528 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5529 */
5530asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5531 unsigned long __user *user_mask_ptr)
5532{
5533 int ret;
5534 cpumask_t mask;
5535
5536 if (len < sizeof(cpumask_t))
5537 return -EINVAL;
5538
5539 ret = sched_getaffinity(pid, &mask);
5540 if (ret < 0)
5541 return ret;
5542
5543 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5544 return -EFAULT;
5545
5546 return sizeof(cpumask_t);
5547}
5548
5549/**
5550 * sys_sched_yield - yield the current processor to other threads.
5551 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005552 * This function yields the current CPU to other tasks. If there are no
5553 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 */
5555asmlinkage long sys_sched_yield(void)
5556{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005557 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
Ingo Molnar2d723762007-10-15 17:00:12 +02005559 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005560 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
5562 /*
5563 * Since we are going to call schedule() anyway, there's
5564 * no need to preempt or enable interrupts:
5565 */
5566 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005567 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 _raw_spin_unlock(&rq->lock);
5569 preempt_enable_no_resched();
5570
5571 schedule();
5572
5573 return 0;
5574}
5575
Andrew Mortone7b38402006-06-30 01:56:00 -07005576static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005578#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5579 __might_sleep(__FILE__, __LINE__);
5580#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005581 /*
5582 * The BKS might be reacquired before we have dropped
5583 * PREEMPT_ACTIVE, which could trigger a second
5584 * cond_resched() call.
5585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 do {
5587 add_preempt_count(PREEMPT_ACTIVE);
5588 schedule();
5589 sub_preempt_count(PREEMPT_ACTIVE);
5590 } while (need_resched());
5591}
5592
Herbert Xu02b67cc32008-01-25 21:08:28 +01005593int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594{
Ingo Molnar94142322006-12-29 16:48:13 -08005595 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5596 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 __cond_resched();
5598 return 1;
5599 }
5600 return 0;
5601}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005602EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
5604/*
5605 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5606 * call schedule, and on return reacquire the lock.
5607 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005608 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 * operations here to prevent schedule() from being called twice (once via
5610 * spin_unlock(), once by hand).
5611 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005612int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613{
Nick Piggin95c354f2008-01-30 13:31:20 +01005614 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005615 int ret = 0;
5616
Nick Piggin95c354f2008-01-30 13:31:20 +01005617 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005619 if (resched && need_resched())
5620 __cond_resched();
5621 else
5622 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005623 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005626 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628EXPORT_SYMBOL(cond_resched_lock);
5629
5630int __sched cond_resched_softirq(void)
5631{
5632 BUG_ON(!in_softirq());
5633
Ingo Molnar94142322006-12-29 16:48:13 -08005634 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005635 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 __cond_resched();
5637 local_bh_disable();
5638 return 1;
5639 }
5640 return 0;
5641}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642EXPORT_SYMBOL(cond_resched_softirq);
5643
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644/**
5645 * yield - yield the current processor to other threads.
5646 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005647 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 * thread runnable and calls sys_sched_yield().
5649 */
5650void __sched yield(void)
5651{
5652 set_current_state(TASK_RUNNING);
5653 sys_sched_yield();
5654}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655EXPORT_SYMBOL(yield);
5656
5657/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005658 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 * that process accounting knows that this is a task in IO wait state.
5660 *
5661 * But don't do that if it is a deliberate, throttling IO wait (this task
5662 * has set its backing_dev_info: the queue against which it should throttle)
5663 */
5664void __sched io_schedule(void)
5665{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005666 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005668 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 atomic_inc(&rq->nr_iowait);
5670 schedule();
5671 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005672 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674EXPORT_SYMBOL(io_schedule);
5675
5676long __sched io_schedule_timeout(long timeout)
5677{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005678 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 long ret;
5680
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005681 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 atomic_inc(&rq->nr_iowait);
5683 ret = schedule_timeout(timeout);
5684 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005685 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 return ret;
5687}
5688
5689/**
5690 * sys_sched_get_priority_max - return maximum RT priority.
5691 * @policy: scheduling class.
5692 *
5693 * this syscall returns the maximum rt_priority that can be used
5694 * by a given scheduling class.
5695 */
5696asmlinkage long sys_sched_get_priority_max(int policy)
5697{
5698 int ret = -EINVAL;
5699
5700 switch (policy) {
5701 case SCHED_FIFO:
5702 case SCHED_RR:
5703 ret = MAX_USER_RT_PRIO-1;
5704 break;
5705 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005706 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005707 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 ret = 0;
5709 break;
5710 }
5711 return ret;
5712}
5713
5714/**
5715 * sys_sched_get_priority_min - return minimum RT priority.
5716 * @policy: scheduling class.
5717 *
5718 * this syscall returns the minimum rt_priority that can be used
5719 * by a given scheduling class.
5720 */
5721asmlinkage long sys_sched_get_priority_min(int policy)
5722{
5723 int ret = -EINVAL;
5724
5725 switch (policy) {
5726 case SCHED_FIFO:
5727 case SCHED_RR:
5728 ret = 1;
5729 break;
5730 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005731 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005732 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 ret = 0;
5734 }
5735 return ret;
5736}
5737
5738/**
5739 * sys_sched_rr_get_interval - return the default timeslice of a process.
5740 * @pid: pid of the process.
5741 * @interval: userspace pointer to the timeslice value.
5742 *
5743 * this syscall writes the default timeslice value of a given process
5744 * into the user-space timespec buffer. A value of '0' means infinity.
5745 */
5746asmlinkage
5747long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5748{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005749 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005750 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005751 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753
5754 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005755 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
5757 retval = -ESRCH;
5758 read_lock(&tasklist_lock);
5759 p = find_process_by_pid(pid);
5760 if (!p)
5761 goto out_unlock;
5762
5763 retval = security_task_getscheduler(p);
5764 if (retval)
5765 goto out_unlock;
5766
Ingo Molnar77034932007-12-04 17:04:39 +01005767 /*
5768 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5769 * tasks that are on an otherwise idle runqueue:
5770 */
5771 time_slice = 0;
5772 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005773 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005774 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005775 struct sched_entity *se = &p->se;
5776 unsigned long flags;
5777 struct rq *rq;
5778
5779 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005780 if (rq->cfs.load.weight)
5781 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005782 task_rq_unlock(rq, &flags);
5783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005785 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005788
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789out_unlock:
5790 read_unlock(&tasklist_lock);
5791 return retval;
5792}
5793
Steven Rostedt7c731e02008-05-12 21:20:41 +02005794static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005795
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005796void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005799 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005802 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005803 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005804#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005806 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005808 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809#else
5810 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005811 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005813 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814#endif
5815#ifdef CONFIG_DEBUG_STACK_USAGE
5816 {
Al Viro10ebffd2005-11-13 16:06:56 -08005817 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 while (!*n)
5819 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005820 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 }
5822#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005823 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005824 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005826 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827}
5828
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005829void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005831 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
Ingo Molnar4bd77322007-07-11 21:21:47 +02005833#if BITS_PER_LONG == 32
5834 printk(KERN_INFO
5835 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005837 printk(KERN_INFO
5838 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839#endif
5840 read_lock(&tasklist_lock);
5841 do_each_thread(g, p) {
5842 /*
5843 * reset the NMI-timeout, listing all files on a slow
5844 * console might take alot of time:
5845 */
5846 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005847 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005848 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 } while_each_thread(g, p);
5850
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005851 touch_all_softlockup_watchdogs();
5852
Ingo Molnardd41f592007-07-09 18:51:59 +02005853#ifdef CONFIG_SCHED_DEBUG
5854 sysrq_sched_debug_show();
5855#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005857 /*
5858 * Only show locks if all tasks are dumped:
5859 */
5860 if (state_filter == -1)
5861 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862}
5863
Ingo Molnar1df21052007-07-09 18:51:58 +02005864void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5865{
Ingo Molnardd41f592007-07-09 18:51:59 +02005866 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005867}
5868
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005869/**
5870 * init_idle - set up an idle thread for a given CPU
5871 * @idle: task in question
5872 * @cpu: cpu the idle task belongs to
5873 *
5874 * NOTE: this function does not set the idle thread's NEED_RESCHED
5875 * flag, to make booting more robust.
5876 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005877void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005879 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 unsigned long flags;
5881
Ingo Molnardd41f592007-07-09 18:51:59 +02005882 __sched_fork(idle);
5883 idle->se.exec_start = sched_clock();
5884
Ingo Molnarb29739f2006-06-27 02:54:51 -07005885 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005887 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888
5889 spin_lock_irqsave(&rq->lock, flags);
5890 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005891#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5892 idle->oncpu = 1;
5893#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 spin_unlock_irqrestore(&rq->lock, flags);
5895
5896 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005897#if defined(CONFIG_PREEMPT)
5898 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
5899#else
Al Viroa1261f542005-11-13 16:06:55 -08005900 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005901#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005902 /*
5903 * The idle tasks have their own, simple scheduling class:
5904 */
5905 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906}
5907
5908/*
5909 * In a system that switches off the HZ timer nohz_cpu_mask
5910 * indicates which cpus entered this state. This is used
5911 * in the rcu update to wait only for active cpus. For system
5912 * which do not switch off the HZ timer nohz_cpu_mask should
5913 * always be CPU_MASK_NONE.
5914 */
5915cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5916
Ingo Molnar19978ca2007-11-09 22:39:38 +01005917/*
5918 * Increase the granularity value when there are more CPUs,
5919 * because with more CPUs the 'effective latency' as visible
5920 * to users decreases. But the relationship is not linear,
5921 * so pick a second-best guess by going with the log2 of the
5922 * number of CPUs.
5923 *
5924 * This idea comes from the SD scheduler of Con Kolivas:
5925 */
5926static inline void sched_init_granularity(void)
5927{
5928 unsigned int factor = 1 + ilog2(num_online_cpus());
5929 const unsigned long limit = 200000000;
5930
5931 sysctl_sched_min_granularity *= factor;
5932 if (sysctl_sched_min_granularity > limit)
5933 sysctl_sched_min_granularity = limit;
5934
5935 sysctl_sched_latency *= factor;
5936 if (sysctl_sched_latency > limit)
5937 sysctl_sched_latency = limit;
5938
5939 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005940}
5941
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942#ifdef CONFIG_SMP
5943/*
5944 * This is how migration works:
5945 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005946 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 * runqueue and wake up that CPU's migration thread.
5948 * 2) we down() the locked semaphore => thread blocks.
5949 * 3) migration thread wakes up (implicitly it forces the migrated
5950 * thread off the CPU)
5951 * 4) it gets the migration request and checks whether the migrated
5952 * task is still in the wrong runqueue.
5953 * 5) if it's in the wrong runqueue then the migration thread removes
5954 * it and puts it into the right queue.
5955 * 6) migration thread up()s the semaphore.
5956 * 7) we wake up and the migration is done.
5957 */
5958
5959/*
5960 * Change a given task's CPU affinity. Migrate the thread to a
5961 * proper CPU and schedule it away if the CPU it's executing on
5962 * is removed from the allowed bitmask.
5963 *
5964 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005965 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 * call is not atomic; no spinlocks may be held.
5967 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005968int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005970 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005972 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005973 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974
5975 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005976 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 ret = -EINVAL;
5978 goto out;
5979 }
5980
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005981 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005982 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005983 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005984 p->cpus_allowed = *new_mask;
5985 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005986 }
5987
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005989 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 goto out;
5991
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005992 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 /* Need help from migration thread: drop lock and wait. */
5994 task_rq_unlock(rq, &flags);
5995 wake_up_process(rq->migration_thread);
5996 wait_for_completion(&req.done);
5997 tlb_migrate_finish(p->mm);
5998 return 0;
5999 }
6000out:
6001 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006002
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 return ret;
6004}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006005EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006
6007/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006008 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 * this because either it can't run here any more (set_cpus_allowed()
6010 * away from this CPU, or CPU going down), or because we're
6011 * attempting to rebalance this task on exec (sched_exec).
6012 *
6013 * So we race with normal scheduler movements, but that's OK, as long
6014 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006015 *
6016 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006018static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006020 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006021 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
6023 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006024 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025
6026 rq_src = cpu_rq(src_cpu);
6027 rq_dest = cpu_rq(dest_cpu);
6028
6029 double_rq_lock(rq_src, rq_dest);
6030 /* Already moved. */
6031 if (task_cpu(p) != src_cpu)
6032 goto out;
6033 /* Affinity changed (again). */
6034 if (!cpu_isset(dest_cpu, p->cpus_allowed))
6035 goto out;
6036
Ingo Molnardd41f592007-07-09 18:51:59 +02006037 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006038 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006039 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006040
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006042 if (on_rq) {
6043 activate_task(rq_dest, p, 0);
6044 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07006046 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047out:
6048 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006049 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050}
6051
6052/*
6053 * migration_thread - this is a highprio system thread that performs
6054 * thread migration by bumping thread off CPU then 'pushing' onto
6055 * another runqueue.
6056 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006057static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006060 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
6062 rq = cpu_rq(cpu);
6063 BUG_ON(rq->migration_thread != current);
6064
6065 set_current_state(TASK_INTERRUPTIBLE);
6066 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006067 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 spin_lock_irq(&rq->lock);
6071
6072 if (cpu_is_offline(cpu)) {
6073 spin_unlock_irq(&rq->lock);
6074 goto wait_to_die;
6075 }
6076
6077 if (rq->active_balance) {
6078 active_load_balance(rq, cpu);
6079 rq->active_balance = 0;
6080 }
6081
6082 head = &rq->migration_queue;
6083
6084 if (list_empty(head)) {
6085 spin_unlock_irq(&rq->lock);
6086 schedule();
6087 set_current_state(TASK_INTERRUPTIBLE);
6088 continue;
6089 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006090 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 list_del_init(head->next);
6092
Nick Piggin674311d2005-06-25 14:57:27 -07006093 spin_unlock(&rq->lock);
6094 __migrate_task(req->task, cpu, req->dest_cpu);
6095 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
6097 complete(&req->done);
6098 }
6099 __set_current_state(TASK_RUNNING);
6100 return 0;
6101
6102wait_to_die:
6103 /* Wait for kthread_stop */
6104 set_current_state(TASK_INTERRUPTIBLE);
6105 while (!kthread_should_stop()) {
6106 schedule();
6107 set_current_state(TASK_INTERRUPTIBLE);
6108 }
6109 __set_current_state(TASK_RUNNING);
6110 return 0;
6111}
6112
6113#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006114
6115static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6116{
6117 int ret;
6118
6119 local_irq_disable();
6120 ret = __migrate_task(p, src_cpu, dest_cpu);
6121 local_irq_enable();
6122 return ret;
6123}
6124
Kirill Korotaev054b9102006-12-10 02:20:11 -08006125/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006126 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006127 * NOTE: interrupts should be disabled by the caller
6128 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006129static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006131 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006133 struct rq *rq;
6134 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135
Andi Kleen3a5c3592007-10-15 17:00:14 +02006136 do {
6137 /* On same node? */
6138 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6139 cpus_and(mask, mask, p->cpus_allowed);
6140 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
Andi Kleen3a5c3592007-10-15 17:00:14 +02006142 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006143 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006144 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
Andi Kleen3a5c3592007-10-15 17:00:14 +02006146 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006147 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006148 cpumask_t cpus_allowed;
6149
6150 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006151 /*
6152 * Try to stay on the same cpuset, where the
6153 * current cpuset may be a subset of all cpus.
6154 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006155 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006156 * called within calls to cpuset_lock/cpuset_unlock.
6157 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006158 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006159 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006160 dest_cpu = any_online_cpu(p->cpus_allowed);
6161 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
Andi Kleen3a5c3592007-10-15 17:00:14 +02006163 /*
6164 * Don't tell them about moving exiting tasks or
6165 * kernel threads (both mm NULL), since they never
6166 * leave kernel.
6167 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006168 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006169 printk(KERN_INFO "process %d (%s) no "
6170 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006171 task_pid_nr(p), p->comm, dead_cpu);
6172 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006173 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006174 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175}
6176
6177/*
6178 * While a dead CPU has no uninterruptible tasks queued at this point,
6179 * it might still have a nonzero ->nr_uninterruptible counter, because
6180 * for performance reasons the counter is not stricly tracking tasks to
6181 * their home CPUs. So we just add the counter to another CPU's counter,
6182 * to keep the global sum constant after CPU-down:
6183 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006184static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185{
Mike Travis7c16ec52008-04-04 18:11:11 -07006186 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 unsigned long flags;
6188
6189 local_irq_save(flags);
6190 double_rq_lock(rq_src, rq_dest);
6191 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6192 rq_src->nr_uninterruptible = 0;
6193 double_rq_unlock(rq_src, rq_dest);
6194 local_irq_restore(flags);
6195}
6196
6197/* Run through task list and migrate tasks from the dead cpu. */
6198static void migrate_live_tasks(int src_cpu)
6199{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006200 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006202 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203
Ingo Molnar48f24c42006-07-03 00:25:40 -07006204 do_each_thread(t, p) {
6205 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 continue;
6207
Ingo Molnar48f24c42006-07-03 00:25:40 -07006208 if (task_cpu(p) == src_cpu)
6209 move_task_off_dead_cpu(src_cpu, p);
6210 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006212 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213}
6214
Ingo Molnardd41f592007-07-09 18:51:59 +02006215/*
6216 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006217 * It does so by boosting its priority to highest possible.
6218 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 */
6220void sched_idle_next(void)
6221{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006222 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006223 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 struct task_struct *p = rq->idle;
6225 unsigned long flags;
6226
6227 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006228 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
Ingo Molnar48f24c42006-07-03 00:25:40 -07006230 /*
6231 * Strictly not necessary since rest of the CPUs are stopped by now
6232 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 */
6234 spin_lock_irqsave(&rq->lock, flags);
6235
Ingo Molnardd41f592007-07-09 18:51:59 +02006236 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006237
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006238 update_rq_clock(rq);
6239 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
6241 spin_unlock_irqrestore(&rq->lock, flags);
6242}
6243
Ingo Molnar48f24c42006-07-03 00:25:40 -07006244/*
6245 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 * offline.
6247 */
6248void idle_task_exit(void)
6249{
6250 struct mm_struct *mm = current->active_mm;
6251
6252 BUG_ON(cpu_online(smp_processor_id()));
6253
6254 if (mm != &init_mm)
6255 switch_mm(mm, &init_mm, current);
6256 mmdrop(mm);
6257}
6258
Kirill Korotaev054b9102006-12-10 02:20:11 -08006259/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006260static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006262 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263
6264 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006265 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266
6267 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006268 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269
Ingo Molnar48f24c42006-07-03 00:25:40 -07006270 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271
6272 /*
6273 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006274 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 * fine.
6276 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006277 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006278 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006279 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280
Ingo Molnar48f24c42006-07-03 00:25:40 -07006281 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282}
6283
6284/* release_task() removes task from tasklist, so we won't find dead tasks. */
6285static void migrate_dead_tasks(unsigned int dead_cpu)
6286{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006287 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006288 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
Ingo Molnardd41f592007-07-09 18:51:59 +02006290 for ( ; ; ) {
6291 if (!rq->nr_running)
6292 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006293 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006294 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006295 if (!next)
6296 break;
6297 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006298
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 }
6300}
6301#endif /* CONFIG_HOTPLUG_CPU */
6302
Nick Piggine692ab52007-07-26 13:40:43 +02006303#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6304
6305static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006306 {
6307 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006308 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006309 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006310 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006311};
6312
6313static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006314 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006315 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006316 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006317 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006318 .child = sd_ctl_dir,
6319 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006320 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006321};
6322
6323static struct ctl_table *sd_alloc_ctl_entry(int n)
6324{
6325 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006326 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006327
Nick Piggine692ab52007-07-26 13:40:43 +02006328 return entry;
6329}
6330
Milton Miller6382bc92007-10-15 17:00:19 +02006331static void sd_free_ctl_entry(struct ctl_table **tablep)
6332{
Milton Millercd7900762007-10-17 16:55:11 +02006333 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006334
Milton Millercd7900762007-10-17 16:55:11 +02006335 /*
6336 * In the intermediate directories, both the child directory and
6337 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006338 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006339 * static strings and all have proc handlers.
6340 */
6341 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006342 if (entry->child)
6343 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006344 if (entry->proc_handler == NULL)
6345 kfree(entry->procname);
6346 }
Milton Miller6382bc92007-10-15 17:00:19 +02006347
6348 kfree(*tablep);
6349 *tablep = NULL;
6350}
6351
Nick Piggine692ab52007-07-26 13:40:43 +02006352static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006353set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006354 const char *procname, void *data, int maxlen,
6355 mode_t mode, proc_handler *proc_handler)
6356{
Nick Piggine692ab52007-07-26 13:40:43 +02006357 entry->procname = procname;
6358 entry->data = data;
6359 entry->maxlen = maxlen;
6360 entry->mode = mode;
6361 entry->proc_handler = proc_handler;
6362}
6363
6364static struct ctl_table *
6365sd_alloc_ctl_domain_table(struct sched_domain *sd)
6366{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006367 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006368
Milton Millerad1cdc12007-10-15 17:00:19 +02006369 if (table == NULL)
6370 return NULL;
6371
Alexey Dobriyane0361852007-08-09 11:16:46 +02006372 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006373 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006374 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006375 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006376 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006377 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006378 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006379 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006380 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006381 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006382 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006383 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006384 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006385 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006386 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006387 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006388 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006389 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006390 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006391 &sd->cache_nice_tries,
6392 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006393 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006394 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006395 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006396
6397 return table;
6398}
6399
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006400static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006401{
6402 struct ctl_table *entry, *table;
6403 struct sched_domain *sd;
6404 int domain_num = 0, i;
6405 char buf[32];
6406
6407 for_each_domain(cpu, sd)
6408 domain_num++;
6409 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006410 if (table == NULL)
6411 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006412
6413 i = 0;
6414 for_each_domain(cpu, sd) {
6415 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006416 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006417 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006418 entry->child = sd_alloc_ctl_domain_table(sd);
6419 entry++;
6420 i++;
6421 }
6422 return table;
6423}
6424
6425static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006426static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006427{
6428 int i, cpu_num = num_online_cpus();
6429 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6430 char buf[32];
6431
Milton Miller73785472007-10-24 18:23:48 +02006432 WARN_ON(sd_ctl_dir[0].child);
6433 sd_ctl_dir[0].child = entry;
6434
Milton Millerad1cdc12007-10-15 17:00:19 +02006435 if (entry == NULL)
6436 return;
6437
Milton Miller97b6ea72007-10-15 17:00:19 +02006438 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006439 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006440 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006441 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006442 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006443 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006444 }
Milton Miller73785472007-10-24 18:23:48 +02006445
6446 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006447 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6448}
Milton Miller6382bc92007-10-15 17:00:19 +02006449
Milton Miller73785472007-10-24 18:23:48 +02006450/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006451static void unregister_sched_domain_sysctl(void)
6452{
Milton Miller73785472007-10-24 18:23:48 +02006453 if (sd_sysctl_header)
6454 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006455 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006456 if (sd_ctl_dir[0].child)
6457 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006458}
Nick Piggine692ab52007-07-26 13:40:43 +02006459#else
Milton Miller6382bc92007-10-15 17:00:19 +02006460static void register_sched_domain_sysctl(void)
6461{
6462}
6463static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006464{
6465}
6466#endif
6467
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468/*
6469 * migration_call - callback that gets triggered when a CPU is added.
6470 * Here we can start up the necessary migration thread for the new CPU.
6471 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006472static int __cpuinit
6473migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006476 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006478 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479
6480 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006481
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006483 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006484 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 if (IS_ERR(p))
6486 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 kthread_bind(p, cpu);
6488 /* Must be high prio: stop_machine expects to yield to it. */
6489 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006490 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 task_rq_unlock(rq, &flags);
6492 cpu_rq(cpu)->migration_thread = p;
6493 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006494
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006496 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006497 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006499
6500 /* Update our root-domain */
6501 rq = cpu_rq(cpu);
6502 spin_lock_irqsave(&rq->lock, flags);
6503 if (rq->rd) {
6504 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6505 cpu_set(cpu, rq->rd->online);
6506 }
6507 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006509
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510#ifdef CONFIG_HOTPLUG_CPU
6511 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006512 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006513 if (!cpu_rq(cpu)->migration_thread)
6514 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006515 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006516 kthread_bind(cpu_rq(cpu)->migration_thread,
6517 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 kthread_stop(cpu_rq(cpu)->migration_thread);
6519 cpu_rq(cpu)->migration_thread = NULL;
6520 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006521
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006523 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006524 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 migrate_live_tasks(cpu);
6526 rq = cpu_rq(cpu);
6527 kthread_stop(rq->migration_thread);
6528 rq->migration_thread = NULL;
6529 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006530 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006531 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006532 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006534 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6535 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006537 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006538 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 migrate_nr_uninterruptible(rq);
6540 BUG_ON(rq->nr_running != 0);
6541
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006542 /*
6543 * No need to migrate the tasks: it was best-effort if
6544 * they didn't take sched_hotcpu_mutex. Just wake up
6545 * the requestors.
6546 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 spin_lock_irq(&rq->lock);
6548 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006549 struct migration_req *req;
6550
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006552 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553 list_del_init(&req->list);
6554 complete(&req->done);
6555 }
6556 spin_unlock_irq(&rq->lock);
6557 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006558
Gregory Haskins08f503b2008-03-10 17:59:11 -04006559 case CPU_DYING:
6560 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006561 /* Update our root-domain */
6562 rq = cpu_rq(cpu);
6563 spin_lock_irqsave(&rq->lock, flags);
6564 if (rq->rd) {
6565 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6566 cpu_clear(cpu, rq->rd->online);
6567 }
6568 spin_unlock_irqrestore(&rq->lock, flags);
6569 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570#endif
6571 }
6572 return NOTIFY_OK;
6573}
6574
6575/* Register at highest priority so that task migration (migrate_all_tasks)
6576 * happens before everything else.
6577 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006578static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 .notifier_call = migration_call,
6580 .priority = 10
6581};
6582
Adrian Bunke6fe6642007-11-09 22:39:39 +01006583void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584{
6585 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006586 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006587
6588 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006589 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6590 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6592 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593}
6594#endif
6595
6596#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006597
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006598#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006599
Mike Travis7c16ec52008-04-04 18:11:11 -07006600static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6601 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006602{
6603 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006604 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006605
Mike Travis434d53b2008-04-04 18:11:04 -07006606 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006607 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006608
6609 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6610
6611 if (!(sd->flags & SD_LOAD_BALANCE)) {
6612 printk("does not load-balance\n");
6613 if (sd->parent)
6614 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6615 " has parent");
6616 return -1;
6617 }
6618
6619 printk(KERN_CONT "span %s\n", str);
6620
6621 if (!cpu_isset(cpu, sd->span)) {
6622 printk(KERN_ERR "ERROR: domain->span does not contain "
6623 "CPU%d\n", cpu);
6624 }
6625 if (!cpu_isset(cpu, group->cpumask)) {
6626 printk(KERN_ERR "ERROR: domain->groups does not contain"
6627 " CPU%d\n", cpu);
6628 }
6629
6630 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6631 do {
6632 if (!group) {
6633 printk("\n");
6634 printk(KERN_ERR "ERROR: group is NULL\n");
6635 break;
6636 }
6637
6638 if (!group->__cpu_power) {
6639 printk(KERN_CONT "\n");
6640 printk(KERN_ERR "ERROR: domain->cpu_power not "
6641 "set\n");
6642 break;
6643 }
6644
6645 if (!cpus_weight(group->cpumask)) {
6646 printk(KERN_CONT "\n");
6647 printk(KERN_ERR "ERROR: empty group\n");
6648 break;
6649 }
6650
Mike Travis7c16ec52008-04-04 18:11:11 -07006651 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006652 printk(KERN_CONT "\n");
6653 printk(KERN_ERR "ERROR: repeated CPUs\n");
6654 break;
6655 }
6656
Mike Travis7c16ec52008-04-04 18:11:11 -07006657 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006658
Mike Travis434d53b2008-04-04 18:11:04 -07006659 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006660 printk(KERN_CONT " %s", str);
6661
6662 group = group->next;
6663 } while (group != sd->groups);
6664 printk(KERN_CONT "\n");
6665
Mike Travis7c16ec52008-04-04 18:11:11 -07006666 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006667 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6668
Mike Travis7c16ec52008-04-04 18:11:11 -07006669 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006670 printk(KERN_ERR "ERROR: parent span is not a superset "
6671 "of domain->span\n");
6672 return 0;
6673}
6674
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675static void sched_domain_debug(struct sched_domain *sd, int cpu)
6676{
Mike Travis7c16ec52008-04-04 18:11:11 -07006677 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 int level = 0;
6679
Nick Piggin41c7ce92005-06-25 14:57:24 -07006680 if (!sd) {
6681 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6682 return;
6683 }
6684
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6686
Mike Travis7c16ec52008-04-04 18:11:11 -07006687 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6688 if (!groupmask) {
6689 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6690 return;
6691 }
6692
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006693 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006694 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 level++;
6697 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006698 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006699 break;
6700 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006701 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702}
6703#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006704# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705#endif
6706
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006707static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006708{
6709 if (cpus_weight(sd->span) == 1)
6710 return 1;
6711
6712 /* Following flags need at least 2 groups */
6713 if (sd->flags & (SD_LOAD_BALANCE |
6714 SD_BALANCE_NEWIDLE |
6715 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006716 SD_BALANCE_EXEC |
6717 SD_SHARE_CPUPOWER |
6718 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006719 if (sd->groups != sd->groups->next)
6720 return 0;
6721 }
6722
6723 /* Following flags don't use groups */
6724 if (sd->flags & (SD_WAKE_IDLE |
6725 SD_WAKE_AFFINE |
6726 SD_WAKE_BALANCE))
6727 return 0;
6728
6729 return 1;
6730}
6731
Ingo Molnar48f24c42006-07-03 00:25:40 -07006732static int
6733sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006734{
6735 unsigned long cflags = sd->flags, pflags = parent->flags;
6736
6737 if (sd_degenerate(parent))
6738 return 1;
6739
6740 if (!cpus_equal(sd->span, parent->span))
6741 return 0;
6742
6743 /* Does parent contain flags not in child? */
6744 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6745 if (cflags & SD_WAKE_AFFINE)
6746 pflags &= ~SD_WAKE_BALANCE;
6747 /* Flags needing groups don't count if only 1 group in parent */
6748 if (parent->groups == parent->groups->next) {
6749 pflags &= ~(SD_LOAD_BALANCE |
6750 SD_BALANCE_NEWIDLE |
6751 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006752 SD_BALANCE_EXEC |
6753 SD_SHARE_CPUPOWER |
6754 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006755 }
6756 if (~cflags & pflags)
6757 return 0;
6758
6759 return 1;
6760}
6761
Gregory Haskins57d885f2008-01-25 21:08:18 +01006762static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6763{
6764 unsigned long flags;
6765 const struct sched_class *class;
6766
6767 spin_lock_irqsave(&rq->lock, flags);
6768
6769 if (rq->rd) {
6770 struct root_domain *old_rd = rq->rd;
6771
Ingo Molnar0eab9142008-01-25 21:08:19 +01006772 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006773 if (class->leave_domain)
6774 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006775 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006776
Gregory Haskinsdc938522008-01-25 21:08:26 +01006777 cpu_clear(rq->cpu, old_rd->span);
6778 cpu_clear(rq->cpu, old_rd->online);
6779
Gregory Haskins57d885f2008-01-25 21:08:18 +01006780 if (atomic_dec_and_test(&old_rd->refcount))
6781 kfree(old_rd);
6782 }
6783
6784 atomic_inc(&rd->refcount);
6785 rq->rd = rd;
6786
Gregory Haskinsdc938522008-01-25 21:08:26 +01006787 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006788 if (cpu_isset(rq->cpu, cpu_online_map))
6789 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006790
Ingo Molnar0eab9142008-01-25 21:08:19 +01006791 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006792 if (class->join_domain)
6793 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006794 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006795
6796 spin_unlock_irqrestore(&rq->lock, flags);
6797}
6798
Gregory Haskinsdc938522008-01-25 21:08:26 +01006799static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006800{
6801 memset(rd, 0, sizeof(*rd));
6802
Gregory Haskinsdc938522008-01-25 21:08:26 +01006803 cpus_clear(rd->span);
6804 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006805}
6806
6807static void init_defrootdomain(void)
6808{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006809 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006810 atomic_set(&def_root_domain.refcount, 1);
6811}
6812
Gregory Haskinsdc938522008-01-25 21:08:26 +01006813static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006814{
6815 struct root_domain *rd;
6816
6817 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6818 if (!rd)
6819 return NULL;
6820
Gregory Haskinsdc938522008-01-25 21:08:26 +01006821 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006822
6823 return rd;
6824}
6825
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006827 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 * hold the hotplug lock.
6829 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006830static void
6831cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006833 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006834 struct sched_domain *tmp;
6835
6836 /* Remove the sched domains which do not contribute to scheduling. */
6837 for (tmp = sd; tmp; tmp = tmp->parent) {
6838 struct sched_domain *parent = tmp->parent;
6839 if (!parent)
6840 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006841 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006842 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006843 if (parent->parent)
6844 parent->parent->child = tmp;
6845 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006846 }
6847
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006848 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006849 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006850 if (sd)
6851 sd->child = NULL;
6852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853
6854 sched_domain_debug(sd, cpu);
6855
Gregory Haskins57d885f2008-01-25 21:08:18 +01006856 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006857 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858}
6859
6860/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006861static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862
6863/* Setup the mask of cpus configured for isolated domains */
6864static int __init isolated_cpu_setup(char *str)
6865{
6866 int ints[NR_CPUS], i;
6867
6868 str = get_options(str, ARRAY_SIZE(ints), ints);
6869 cpus_clear(cpu_isolated_map);
6870 for (i = 1; i <= ints[0]; i++)
6871 if (ints[i] < NR_CPUS)
6872 cpu_set(ints[i], cpu_isolated_map);
6873 return 1;
6874}
6875
Ingo Molnar8927f492007-10-15 17:00:13 +02006876__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
6878/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006879 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6880 * to a function which identifies what group(along with sched group) a CPU
6881 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6882 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 *
6884 * init_sched_build_groups will build a circular linked list of the groups
6885 * covered by the given span, and will set each group's ->cpumask correctly,
6886 * and ->cpu_power to 0.
6887 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006888static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006889init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006890 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006891 struct sched_group **sg,
6892 cpumask_t *tmpmask),
6893 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894{
6895 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 int i;
6897
Mike Travis7c16ec52008-04-04 18:11:11 -07006898 cpus_clear(*covered);
6899
6900 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006901 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006902 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 int j;
6904
Mike Travis7c16ec52008-04-04 18:11:11 -07006905 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 continue;
6907
Mike Travis7c16ec52008-04-04 18:11:11 -07006908 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006909 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910
Mike Travis7c16ec52008-04-04 18:11:11 -07006911 for_each_cpu_mask(j, *span) {
6912 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 continue;
6914
Mike Travis7c16ec52008-04-04 18:11:11 -07006915 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 cpu_set(j, sg->cpumask);
6917 }
6918 if (!first)
6919 first = sg;
6920 if (last)
6921 last->next = sg;
6922 last = sg;
6923 }
6924 last->next = first;
6925}
6926
John Hawkes9c1cfda2005-09-06 15:18:14 -07006927#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928
John Hawkes9c1cfda2005-09-06 15:18:14 -07006929#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006930
John Hawkes9c1cfda2005-09-06 15:18:14 -07006931/**
6932 * find_next_best_node - find the next node to include in a sched_domain
6933 * @node: node whose sched_domain we're building
6934 * @used_nodes: nodes already in the sched_domain
6935 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006936 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006937 * finds the closest node not already in the @used_nodes map.
6938 *
6939 * Should use nodemask_t.
6940 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006941static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006942{
6943 int i, n, val, min_val, best_node = 0;
6944
6945 min_val = INT_MAX;
6946
6947 for (i = 0; i < MAX_NUMNODES; i++) {
6948 /* Start at @node */
6949 n = (node + i) % MAX_NUMNODES;
6950
6951 if (!nr_cpus_node(n))
6952 continue;
6953
6954 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006955 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006956 continue;
6957
6958 /* Simple min distance search */
6959 val = node_distance(node, n);
6960
6961 if (val < min_val) {
6962 min_val = val;
6963 best_node = n;
6964 }
6965 }
6966
Mike Travisc5f59f02008-04-04 18:11:10 -07006967 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006968 return best_node;
6969}
6970
6971/**
6972 * sched_domain_node_span - get a cpumask for a node's sched_domain
6973 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07006974 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07006975 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006976 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006977 * should be one that prevents unnecessary balancing, but also spreads tasks
6978 * out optimally.
6979 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07006980static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006981{
Mike Travisc5f59f02008-04-04 18:11:10 -07006982 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07006983 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006984 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006985
Mike Travis4bdbaad32008-04-15 16:35:52 -07006986 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07006987 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006988
Mike Travis4bdbaad32008-04-15 16:35:52 -07006989 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07006990 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006991
6992 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07006993 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006994
Mike Travisc5f59f02008-04-04 18:11:10 -07006995 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07006996 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006997 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006998}
6999#endif
7000
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007001int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007002
John Hawkes9c1cfda2005-09-06 15:18:14 -07007003/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07007004 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07007005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006#ifdef CONFIG_SCHED_SMT
7007static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007008static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007009
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007010static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007011cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7012 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007014 if (sg)
7015 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 return cpu;
7017}
7018#endif
7019
Ingo Molnar48f24c42006-07-03 00:25:40 -07007020/*
7021 * multi-core sched-domains:
7022 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007023#ifdef CONFIG_SCHED_MC
7024static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007025static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007026#endif
7027
7028#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007029static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007030cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7031 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007032{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007033 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007034
7035 *mask = per_cpu(cpu_sibling_map, cpu);
7036 cpus_and(*mask, *mask, *cpu_map);
7037 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007038 if (sg)
7039 *sg = &per_cpu(sched_group_core, group);
7040 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007041}
7042#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007043static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007044cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7045 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007046{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007047 if (sg)
7048 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007049 return cpu;
7050}
7051#endif
7052
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007054static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007055
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007056static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007057cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7058 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007060 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007061#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007062 *mask = cpu_coregroup_map(cpu);
7063 cpus_and(*mask, *mask, *cpu_map);
7064 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007065#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007066 *mask = per_cpu(cpu_sibling_map, cpu);
7067 cpus_and(*mask, *mask, *cpu_map);
7068 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007070 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007072 if (sg)
7073 *sg = &per_cpu(sched_group_phys, group);
7074 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075}
7076
7077#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007078/*
7079 * The init_sched_build_groups can't handle what we want to do with node
7080 * groups, so roll our own. Now each node has its own list of groups which
7081 * gets dynamically allocated.
7082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007084static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007085
7086static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007087static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007088
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007089static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007090 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007092 int group;
7093
Mike Travis7c16ec52008-04-04 18:11:11 -07007094 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7095 cpus_and(*nodemask, *nodemask, *cpu_map);
7096 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007097
7098 if (sg)
7099 *sg = &per_cpu(sched_group_allnodes, group);
7100 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007102
Siddha, Suresh B08069032006-03-27 01:15:23 -08007103static void init_numa_sched_groups_power(struct sched_group *group_head)
7104{
7105 struct sched_group *sg = group_head;
7106 int j;
7107
7108 if (!sg)
7109 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007110 do {
7111 for_each_cpu_mask(j, sg->cpumask) {
7112 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007113
Andi Kleen3a5c3592007-10-15 17:00:14 +02007114 sd = &per_cpu(phys_domains, j);
7115 if (j != first_cpu(sd->groups->cpumask)) {
7116 /*
7117 * Only add "power" once for each
7118 * physical package.
7119 */
7120 continue;
7121 }
7122
7123 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007124 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007125 sg = sg->next;
7126 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128#endif
7129
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007130#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007131/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007132static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007133{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007134 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007135
7136 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007137 struct sched_group **sched_group_nodes
7138 = sched_group_nodes_bycpu[cpu];
7139
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007140 if (!sched_group_nodes)
7141 continue;
7142
7143 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007144 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7145
Mike Travis7c16ec52008-04-04 18:11:11 -07007146 *nodemask = node_to_cpumask(i);
7147 cpus_and(*nodemask, *nodemask, *cpu_map);
7148 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007149 continue;
7150
7151 if (sg == NULL)
7152 continue;
7153 sg = sg->next;
7154next_sg:
7155 oldsg = sg;
7156 sg = sg->next;
7157 kfree(oldsg);
7158 if (oldsg != sched_group_nodes[i])
7159 goto next_sg;
7160 }
7161 kfree(sched_group_nodes);
7162 sched_group_nodes_bycpu[cpu] = NULL;
7163 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007164}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007165#else
Mike Travis7c16ec52008-04-04 18:11:11 -07007166static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007167{
7168}
7169#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007170
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007172 * Initialize sched groups cpu_power.
7173 *
7174 * cpu_power indicates the capacity of sched group, which is used while
7175 * distributing the load between different sched groups in a sched domain.
7176 * Typically cpu_power for all the groups in a sched domain will be same unless
7177 * there are asymmetries in the topology. If there are asymmetries, group
7178 * having more cpu_power will pickup more load compared to the group having
7179 * less cpu_power.
7180 *
7181 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7182 * the maximum number of tasks a group can handle in the presence of other idle
7183 * or lightly loaded groups in the same sched domain.
7184 */
7185static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7186{
7187 struct sched_domain *child;
7188 struct sched_group *group;
7189
7190 WARN_ON(!sd || !sd->groups);
7191
7192 if (cpu != first_cpu(sd->groups->cpumask))
7193 return;
7194
7195 child = sd->child;
7196
Eric Dumazet5517d862007-05-08 00:32:57 -07007197 sd->groups->__cpu_power = 0;
7198
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007199 /*
7200 * For perf policy, if the groups in child domain share resources
7201 * (for example cores sharing some portions of the cache hierarchy
7202 * or SMT), then set this domain groups cpu_power such that each group
7203 * can handle only one task, when there are other idle groups in the
7204 * same sched domain.
7205 */
7206 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7207 (child->flags &
7208 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007209 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007210 return;
7211 }
7212
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007213 /*
7214 * add cpu_power of each child group to this groups cpu_power
7215 */
7216 group = child->groups;
7217 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007218 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007219 group = group->next;
7220 } while (group != child->groups);
7221}
7222
7223/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007224 * Initializers for schedule domains
7225 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7226 */
7227
7228#define SD_INIT(sd, type) sd_init_##type(sd)
7229#define SD_INIT_FUNC(type) \
7230static noinline void sd_init_##type(struct sched_domain *sd) \
7231{ \
7232 memset(sd, 0, sizeof(*sd)); \
7233 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007234 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007235}
7236
7237SD_INIT_FUNC(CPU)
7238#ifdef CONFIG_NUMA
7239 SD_INIT_FUNC(ALLNODES)
7240 SD_INIT_FUNC(NODE)
7241#endif
7242#ifdef CONFIG_SCHED_SMT
7243 SD_INIT_FUNC(SIBLING)
7244#endif
7245#ifdef CONFIG_SCHED_MC
7246 SD_INIT_FUNC(MC)
7247#endif
7248
7249/*
7250 * To minimize stack usage kmalloc room for cpumasks and share the
7251 * space as the usage in build_sched_domains() dictates. Used only
7252 * if the amount of space is significant.
7253 */
7254struct allmasks {
7255 cpumask_t tmpmask; /* make this one first */
7256 union {
7257 cpumask_t nodemask;
7258 cpumask_t this_sibling_map;
7259 cpumask_t this_core_map;
7260 };
7261 cpumask_t send_covered;
7262
7263#ifdef CONFIG_NUMA
7264 cpumask_t domainspan;
7265 cpumask_t covered;
7266 cpumask_t notcovered;
7267#endif
7268};
7269
7270#if NR_CPUS > 128
7271#define SCHED_CPUMASK_ALLOC 1
7272#define SCHED_CPUMASK_FREE(v) kfree(v)
7273#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7274#else
7275#define SCHED_CPUMASK_ALLOC 0
7276#define SCHED_CPUMASK_FREE(v)
7277#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7278#endif
7279
7280#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7281 ((unsigned long)(a) + offsetof(struct allmasks, v))
7282
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007283static int default_relax_domain_level = -1;
7284
7285static int __init setup_relax_domain_level(char *str)
7286{
7287 default_relax_domain_level = simple_strtoul(str, NULL, 0);
7288 return 1;
7289}
7290__setup("relax_domain_level=", setup_relax_domain_level);
7291
7292static void set_domain_attribute(struct sched_domain *sd,
7293 struct sched_domain_attr *attr)
7294{
7295 int request;
7296
7297 if (!attr || attr->relax_domain_level < 0) {
7298 if (default_relax_domain_level < 0)
7299 return;
7300 else
7301 request = default_relax_domain_level;
7302 } else
7303 request = attr->relax_domain_level;
7304 if (request < sd->level) {
7305 /* turn off idle balance on this domain */
7306 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7307 } else {
7308 /* turn on idle balance on this domain */
7309 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7310 }
7311}
7312
Mike Travis7c16ec52008-04-04 18:11:11 -07007313/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007314 * Build sched domains for a given set of cpus and attach the sched domains
7315 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007317static int __build_sched_domains(const cpumask_t *cpu_map,
7318 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319{
7320 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007321 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007322 SCHED_CPUMASK_DECLARE(allmasks);
7323 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007324#ifdef CONFIG_NUMA
7325 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007326 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007327
7328 /*
7329 * Allocate the per-node list of sched groups
7330 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007331 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007332 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007333 if (!sched_group_nodes) {
7334 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007335 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007336 }
John Hawkesd1b55132005-09-06 15:18:14 -07007337#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338
Gregory Haskinsdc938522008-01-25 21:08:26 +01007339 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007340 if (!rd) {
7341 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007342#ifdef CONFIG_NUMA
7343 kfree(sched_group_nodes);
7344#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007345 return -ENOMEM;
7346 }
7347
Mike Travis7c16ec52008-04-04 18:11:11 -07007348#if SCHED_CPUMASK_ALLOC
7349 /* get space for all scratch cpumask variables */
7350 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7351 if (!allmasks) {
7352 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7353 kfree(rd);
7354#ifdef CONFIG_NUMA
7355 kfree(sched_group_nodes);
7356#endif
7357 return -ENOMEM;
7358 }
7359#endif
7360 tmpmask = (cpumask_t *)allmasks;
7361
7362
7363#ifdef CONFIG_NUMA
7364 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7365#endif
7366
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007368 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007370 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007372 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373
Mike Travis7c16ec52008-04-04 18:11:11 -07007374 *nodemask = node_to_cpumask(cpu_to_node(i));
7375 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376
7377#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007378 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007379 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007380 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007381 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007382 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007383 sd->span = *cpu_map;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007384 sd->first_cpu = first_cpu(sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07007385 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007386 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007387 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007388 } else
7389 p = NULL;
7390
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007392 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007393 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007394 sched_domain_node_span(cpu_to_node(i), &sd->span);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007395 sd->first_cpu = first_cpu(sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007396 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007397 if (p)
7398 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007399 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400#endif
7401
7402 p = sd;
7403 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007404 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007405 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007406 sd->span = *nodemask;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007407 sd->first_cpu = first_cpu(sd->span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007409 if (p)
7410 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007411 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007413#ifdef CONFIG_SCHED_MC
7414 p = sd;
7415 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007416 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007417 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007418 sd->span = cpu_coregroup_map(i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007419 sd->first_cpu = first_cpu(sd->span);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007420 cpus_and(sd->span, sd->span, *cpu_map);
7421 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007422 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007423 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007424#endif
7425
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426#ifdef CONFIG_SCHED_SMT
7427 p = sd;
7428 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007429 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007430 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007431 sd->span = per_cpu(cpu_sibling_map, i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007432 sd->first_cpu = first_cpu(sd->span);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007433 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007435 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007436 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437#endif
7438 }
7439
7440#ifdef CONFIG_SCHED_SMT
7441 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007442 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007443 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7444 SCHED_CPUMASK_VAR(send_covered, allmasks);
7445
7446 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7447 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7448 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 continue;
7450
Ingo Molnardd41f592007-07-09 18:51:59 +02007451 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007452 &cpu_to_cpu_group,
7453 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 }
7455#endif
7456
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007457#ifdef CONFIG_SCHED_MC
7458 /* Set up multi-core groups */
7459 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007460 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7461 SCHED_CPUMASK_VAR(send_covered, allmasks);
7462
7463 *this_core_map = cpu_coregroup_map(i);
7464 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7465 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007466 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007467
Ingo Molnardd41f592007-07-09 18:51:59 +02007468 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007469 &cpu_to_core_group,
7470 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007471 }
7472#endif
7473
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 /* Set up physical groups */
7475 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007476 SCHED_CPUMASK_VAR(nodemask, allmasks);
7477 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478
Mike Travis7c16ec52008-04-04 18:11:11 -07007479 *nodemask = node_to_cpumask(i);
7480 cpus_and(*nodemask, *nodemask, *cpu_map);
7481 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 continue;
7483
Mike Travis7c16ec52008-04-04 18:11:11 -07007484 init_sched_build_groups(nodemask, cpu_map,
7485 &cpu_to_phys_group,
7486 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487 }
7488
7489#ifdef CONFIG_NUMA
7490 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007491 if (sd_allnodes) {
7492 SCHED_CPUMASK_VAR(send_covered, allmasks);
7493
7494 init_sched_build_groups(cpu_map, cpu_map,
7495 &cpu_to_allnodes_group,
7496 send_covered, tmpmask);
7497 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007498
7499 for (i = 0; i < MAX_NUMNODES; i++) {
7500 /* Set up node groups */
7501 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007502 SCHED_CPUMASK_VAR(nodemask, allmasks);
7503 SCHED_CPUMASK_VAR(domainspan, allmasks);
7504 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007505 int j;
7506
Mike Travis7c16ec52008-04-04 18:11:11 -07007507 *nodemask = node_to_cpumask(i);
7508 cpus_clear(*covered);
7509
7510 cpus_and(*nodemask, *nodemask, *cpu_map);
7511 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007512 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007513 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007514 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007515
Mike Travis4bdbaad32008-04-15 16:35:52 -07007516 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007517 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007518
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007519 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007520 if (!sg) {
7521 printk(KERN_WARNING "Can not alloc domain group for "
7522 "node %d\n", i);
7523 goto error;
7524 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007525 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007526 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007527 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007528
John Hawkes9c1cfda2005-09-06 15:18:14 -07007529 sd = &per_cpu(node_domains, j);
7530 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007531 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007532 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007533 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007534 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007535 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007536 prev = sg;
7537
7538 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007539 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007540 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007541 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007542
Mike Travis7c16ec52008-04-04 18:11:11 -07007543 cpus_complement(*notcovered, *covered);
7544 cpus_and(*tmpmask, *notcovered, *cpu_map);
7545 cpus_and(*tmpmask, *tmpmask, *domainspan);
7546 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007547 break;
7548
Mike Travis7c16ec52008-04-04 18:11:11 -07007549 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7550 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007551 continue;
7552
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007553 sg = kmalloc_node(sizeof(struct sched_group),
7554 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007555 if (!sg) {
7556 printk(KERN_WARNING
7557 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007558 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007559 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007560 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007561 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007562 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007563 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007564 prev->next = sg;
7565 prev = sg;
7566 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568#endif
7569
7570 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007571#ifdef CONFIG_SCHED_SMT
7572 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007573 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7574
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007575 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007576 }
7577#endif
7578#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007579 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007580 struct sched_domain *sd = &per_cpu(core_domains, i);
7581
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007582 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007583 }
7584#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007586 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007587 struct sched_domain *sd = &per_cpu(phys_domains, i);
7588
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007589 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590 }
7591
John Hawkes9c1cfda2005-09-06 15:18:14 -07007592#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007593 for (i = 0; i < MAX_NUMNODES; i++)
7594 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007595
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007596 if (sd_allnodes) {
7597 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007598
Mike Travis7c16ec52008-04-04 18:11:11 -07007599 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7600 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007601 init_numa_sched_groups_power(sg);
7602 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007603#endif
7604
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007606 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607 struct sched_domain *sd;
7608#ifdef CONFIG_SCHED_SMT
7609 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007610#elif defined(CONFIG_SCHED_MC)
7611 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612#else
7613 sd = &per_cpu(phys_domains, i);
7614#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007615 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007617
Mike Travis7c16ec52008-04-04 18:11:11 -07007618 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007619 return 0;
7620
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007621#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007622error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007623 free_sched_groups(cpu_map, tmpmask);
7624 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007625 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007626#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627}
Paul Jackson029190c2007-10-18 23:40:20 -07007628
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007629static int build_sched_domains(const cpumask_t *cpu_map)
7630{
7631 return __build_sched_domains(cpu_map, NULL);
7632}
7633
Paul Jackson029190c2007-10-18 23:40:20 -07007634static cpumask_t *doms_cur; /* current sched domains */
7635static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007636static struct sched_domain_attr *dattr_cur; /* attribues of custom domains
7637 in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007638
7639/*
7640 * Special case: If a kmalloc of a doms_cur partition (array of
7641 * cpumask_t) fails, then fallback to a single sched domain,
7642 * as determined by the single cpumask_t fallback_doms.
7643 */
7644static cpumask_t fallback_doms;
7645
Heiko Carstens22e52b02008-03-12 18:31:59 +01007646void __attribute__((weak)) arch_update_cpu_topology(void)
7647{
7648}
7649
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007650/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007651 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007652 * For now this just excludes isolated cpus, but could be used to
7653 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007654 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007655static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007656{
Milton Miller73785472007-10-24 18:23:48 +02007657 int err;
7658
Heiko Carstens22e52b02008-03-12 18:31:59 +01007659 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007660 ndoms_cur = 1;
7661 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7662 if (!doms_cur)
7663 doms_cur = &fallback_doms;
7664 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007665 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007666 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007667 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007668
7669 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007670}
7671
Mike Travis7c16ec52008-04-04 18:11:11 -07007672static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7673 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674{
Mike Travis7c16ec52008-04-04 18:11:11 -07007675 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007676}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007678/*
7679 * Detach sched domains from a group of cpus specified in cpu_map
7680 * These cpus will now be attached to the NULL domain
7681 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007682static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007683{
Mike Travis7c16ec52008-04-04 18:11:11 -07007684 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007685 int i;
7686
Milton Miller6382bc92007-10-15 17:00:19 +02007687 unregister_sched_domain_sysctl();
7688
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007689 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007690 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007691 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007692 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007693}
7694
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007695/* handle null as "default" */
7696static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7697 struct sched_domain_attr *new, int idx_new)
7698{
7699 struct sched_domain_attr tmp;
7700
7701 /* fast path */
7702 if (!new && !cur)
7703 return 1;
7704
7705 tmp = SD_ATTR_INIT;
7706 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7707 new ? (new + idx_new) : &tmp,
7708 sizeof(struct sched_domain_attr));
7709}
7710
Paul Jackson029190c2007-10-18 23:40:20 -07007711/*
7712 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007713 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007714 * doms_new[] to the current sched domain partitioning, doms_cur[].
7715 * It destroys each deleted domain and builds each new domain.
7716 *
7717 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007718 * The masks don't intersect (don't overlap.) We should setup one
7719 * sched domain for each mask. CPUs not in any of the cpumasks will
7720 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007721 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7722 * it as it is.
7723 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007724 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7725 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007726 * failed the kmalloc call, then it can pass in doms_new == NULL,
7727 * and partition_sched_domains() will fallback to the single partition
7728 * 'fallback_doms'.
7729 *
7730 * Call with hotplug lock held
7731 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007732void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7733 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007734{
7735 int i, j;
7736
Heiko Carstens712555e2008-04-28 11:33:07 +02007737 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007738
Milton Miller73785472007-10-24 18:23:48 +02007739 /* always unregister in case we don't destroy any domains */
7740 unregister_sched_domain_sysctl();
7741
Paul Jackson029190c2007-10-18 23:40:20 -07007742 if (doms_new == NULL) {
7743 ndoms_new = 1;
7744 doms_new = &fallback_doms;
7745 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007746 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007747 }
7748
7749 /* Destroy deleted domains */
7750 for (i = 0; i < ndoms_cur; i++) {
7751 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007752 if (cpus_equal(doms_cur[i], doms_new[j])
7753 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007754 goto match1;
7755 }
7756 /* no match - a current sched domain not in new doms_new[] */
7757 detach_destroy_domains(doms_cur + i);
7758match1:
7759 ;
7760 }
7761
7762 /* Build new domains */
7763 for (i = 0; i < ndoms_new; i++) {
7764 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007765 if (cpus_equal(doms_new[i], doms_cur[j])
7766 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007767 goto match2;
7768 }
7769 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007770 __build_sched_domains(doms_new + i,
7771 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007772match2:
7773 ;
7774 }
7775
7776 /* Remember the new sched domains */
7777 if (doms_cur != &fallback_doms)
7778 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007779 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007780 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007781 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007782 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007783
7784 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007785
Heiko Carstens712555e2008-04-28 11:33:07 +02007786 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007787}
7788
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007789#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007790int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007791{
7792 int err;
7793
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007794 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007795 mutex_lock(&sched_domains_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007796 detach_destroy_domains(&cpu_online_map);
7797 err = arch_init_sched_domains(&cpu_online_map);
Heiko Carstens712555e2008-04-28 11:33:07 +02007798 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007799 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007800
7801 return err;
7802}
7803
7804static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7805{
7806 int ret;
7807
7808 if (buf[0] != '0' && buf[0] != '1')
7809 return -EINVAL;
7810
7811 if (smt)
7812 sched_smt_power_savings = (buf[0] == '1');
7813 else
7814 sched_mc_power_savings = (buf[0] == '1');
7815
7816 ret = arch_reinit_sched_domains();
7817
7818 return ret ? ret : count;
7819}
7820
Adrian Bunk6707de002007-08-12 18:08:19 +02007821#ifdef CONFIG_SCHED_MC
7822static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7823{
7824 return sprintf(page, "%u\n", sched_mc_power_savings);
7825}
7826static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7827 const char *buf, size_t count)
7828{
7829 return sched_power_savings_store(buf, count, 0);
7830}
7831static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7832 sched_mc_power_savings_store);
7833#endif
7834
7835#ifdef CONFIG_SCHED_SMT
7836static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7837{
7838 return sprintf(page, "%u\n", sched_smt_power_savings);
7839}
7840static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7841 const char *buf, size_t count)
7842{
7843 return sched_power_savings_store(buf, count, 1);
7844}
7845static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7846 sched_smt_power_savings_store);
7847#endif
7848
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007849int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7850{
7851 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007852
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007853#ifdef CONFIG_SCHED_SMT
7854 if (smt_capable())
7855 err = sysfs_create_file(&cls->kset.kobj,
7856 &attr_sched_smt_power_savings.attr);
7857#endif
7858#ifdef CONFIG_SCHED_MC
7859 if (!err && mc_capable())
7860 err = sysfs_create_file(&cls->kset.kobj,
7861 &attr_sched_mc_power_savings.attr);
7862#endif
7863 return err;
7864}
7865#endif
7866
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007868 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007870 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871 * which will prevent rebalancing while the sched domains are recalculated.
7872 */
7873static int update_sched_domains(struct notifier_block *nfb,
7874 unsigned long action, void *hcpu)
7875{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 switch (action) {
7877 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007878 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007880 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007881 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882 return NOTIFY_OK;
7883
7884 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007885 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007887 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007889 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007891 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 /*
7893 * Fall through and re-initialise the domains.
7894 */
7895 break;
7896 default:
7897 return NOTIFY_DONE;
7898 }
7899
7900 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007901 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902
7903 return NOTIFY_OK;
7904}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905
7906void __init sched_init_smp(void)
7907{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007908 cpumask_t non_isolated_cpus;
7909
Mike Travis434d53b2008-04-04 18:11:04 -07007910#if defined(CONFIG_NUMA)
7911 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7912 GFP_KERNEL);
7913 BUG_ON(sched_group_nodes_bycpu == NULL);
7914#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007915 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007916 mutex_lock(&sched_domains_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007917 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007918 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007919 if (cpus_empty(non_isolated_cpus))
7920 cpu_set(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007921 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007922 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923 /* XXX: Theoretical race here - CPU may be hotplugged now */
7924 hotcpu_notifier(update_sched_domains, 0);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007925 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007926
7927 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07007928 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007929 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007930 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931}
7932#else
7933void __init sched_init_smp(void)
7934{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007935 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936}
7937#endif /* CONFIG_SMP */
7938
7939int in_sched_functions(unsigned long addr)
7940{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 return in_lock_functions(addr) ||
7942 (addr >= (unsigned long)__sched_text_start
7943 && addr < (unsigned long)__sched_text_end);
7944}
7945
Alexey Dobriyana9957442007-10-15 17:00:13 +02007946static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007947{
7948 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007949 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02007950#ifdef CONFIG_FAIR_GROUP_SCHED
7951 cfs_rq->rq = rq;
7952#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007953 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007954}
7955
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007956static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7957{
7958 struct rt_prio_array *array;
7959 int i;
7960
7961 array = &rt_rq->active;
7962 for (i = 0; i < MAX_RT_PRIO; i++) {
7963 INIT_LIST_HEAD(array->queue + i);
7964 __clear_bit(i, array->bitmap);
7965 }
7966 /* delimiter for bitsearch: */
7967 __set_bit(MAX_RT_PRIO, array->bitmap);
7968
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007969#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007970 rt_rq->highest_prio = MAX_RT_PRIO;
7971#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007972#ifdef CONFIG_SMP
7973 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007974 rt_rq->overloaded = 0;
7975#endif
7976
7977 rt_rq->rt_time = 0;
7978 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007979 rt_rq->rt_runtime = 0;
7980 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007981
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007982#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007983 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007984 rt_rq->rq = rq;
7985#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007986}
7987
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007988#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007989static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7990 struct sched_entity *se, int cpu, int add,
7991 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007992{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007993 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007994 tg->cfs_rq[cpu] = cfs_rq;
7995 init_cfs_rq(cfs_rq, rq);
7996 cfs_rq->tg = tg;
7997 if (add)
7998 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7999
8000 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008001 /* se could be NULL for init_task_group */
8002 if (!se)
8003 return;
8004
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008005 if (!parent)
8006 se->cfs_rq = &rq->cfs;
8007 else
8008 se->cfs_rq = parent->my_q;
8009
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008010 se->my_q = cfs_rq;
8011 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008012 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008013 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008014}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008015#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008016
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008017#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008018static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8019 struct sched_rt_entity *rt_se, int cpu, int add,
8020 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008021{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008022 struct rq *rq = cpu_rq(cpu);
8023
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008024 tg->rt_rq[cpu] = rt_rq;
8025 init_rt_rq(rt_rq, rq);
8026 rt_rq->tg = tg;
8027 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008028 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008029 if (add)
8030 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8031
8032 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008033 if (!rt_se)
8034 return;
8035
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008036 if (!parent)
8037 rt_se->rt_rq = &rq->rt;
8038 else
8039 rt_se->rt_rq = parent->my_q;
8040
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008041 rt_se->rt_rq = &rq->rt;
8042 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008043 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008044 INIT_LIST_HEAD(&rt_se->run_list);
8045}
8046#endif
8047
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048void __init sched_init(void)
8049{
Ingo Molnardd41f592007-07-09 18:51:59 +02008050 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008051 unsigned long alloc_size = 0, ptr;
8052
8053#ifdef CONFIG_FAIR_GROUP_SCHED
8054 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8055#endif
8056#ifdef CONFIG_RT_GROUP_SCHED
8057 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8058#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008059#ifdef CONFIG_USER_SCHED
8060 alloc_size *= 2;
8061#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008062 /*
8063 * As sched_init() is called before page_alloc is setup,
8064 * we use alloc_bootmem().
8065 */
8066 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07008067 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07008068
8069#ifdef CONFIG_FAIR_GROUP_SCHED
8070 init_task_group.se = (struct sched_entity **)ptr;
8071 ptr += nr_cpu_ids * sizeof(void **);
8072
8073 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8074 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008075
8076#ifdef CONFIG_USER_SCHED
8077 root_task_group.se = (struct sched_entity **)ptr;
8078 ptr += nr_cpu_ids * sizeof(void **);
8079
8080 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8081 ptr += nr_cpu_ids * sizeof(void **);
8082#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008083#endif
8084#ifdef CONFIG_RT_GROUP_SCHED
8085 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8086 ptr += nr_cpu_ids * sizeof(void **);
8087
8088 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008089 ptr += nr_cpu_ids * sizeof(void **);
8090
8091#ifdef CONFIG_USER_SCHED
8092 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8093 ptr += nr_cpu_ids * sizeof(void **);
8094
8095 root_task_group.rt_rq = (struct rt_rq **)ptr;
8096 ptr += nr_cpu_ids * sizeof(void **);
8097#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008098#endif
8099 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008100
Gregory Haskins57d885f2008-01-25 21:08:18 +01008101#ifdef CONFIG_SMP
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008102 init_aggregate();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008103 init_defrootdomain();
8104#endif
8105
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008106 init_rt_bandwidth(&def_rt_bandwidth,
8107 global_rt_period(), global_rt_runtime());
8108
8109#ifdef CONFIG_RT_GROUP_SCHED
8110 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8111 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008112#ifdef CONFIG_USER_SCHED
8113 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8114 global_rt_period(), RUNTIME_INF);
8115#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008116#endif
8117
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008118#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008119 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008120 INIT_LIST_HEAD(&init_task_group.children);
8121
8122#ifdef CONFIG_USER_SCHED
8123 INIT_LIST_HEAD(&root_task_group.children);
8124 init_task_group.parent = &root_task_group;
8125 list_add(&init_task_group.siblings, &root_task_group.children);
8126#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008127#endif
8128
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008129 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008130 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008131
8132 rq = cpu_rq(i);
8133 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07008134 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07008135 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008136 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008137 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008138#ifdef CONFIG_FAIR_GROUP_SCHED
8139 init_task_group.shares = init_task_group_load;
8140 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008141#ifdef CONFIG_CGROUP_SCHED
8142 /*
8143 * How much cpu bandwidth does init_task_group get?
8144 *
8145 * In case of task-groups formed thr' the cgroup filesystem, it
8146 * gets 100% of the cpu resources in the system. This overall
8147 * system cpu resource is divided among the tasks of
8148 * init_task_group and its child task-groups in a fair manner,
8149 * based on each entity's (task or task-group's) weight
8150 * (se->load.weight).
8151 *
8152 * In other words, if init_task_group has 10 tasks of weight
8153 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8154 * then A0's share of the cpu resource is:
8155 *
8156 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8157 *
8158 * We achieve this by letting init_task_group's tasks sit
8159 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8160 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008161 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008162#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008163 root_task_group.shares = NICE_0_LOAD;
8164 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008165 /*
8166 * In case of task-groups formed thr' the user id of tasks,
8167 * init_task_group represents tasks belonging to root user.
8168 * Hence it forms a sibling of all subsequent groups formed.
8169 * In this case, init_task_group gets only a fraction of overall
8170 * system cpu resource, based on the weight assigned to root
8171 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8172 * by letting tasks of init_task_group sit in a separate cfs_rq
8173 * (init_cfs_rq) and having one entity represent this group of
8174 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8175 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008176 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008177 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008178 &per_cpu(init_sched_entity, i), i, 1,
8179 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008180
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008181#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008182#endif /* CONFIG_FAIR_GROUP_SCHED */
8183
8184 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008185#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008186 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008187#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008188 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008189#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008190 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008191 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008192 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008193 &per_cpu(init_sched_rt_entity, i), i, 1,
8194 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008195#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197
Ingo Molnardd41f592007-07-09 18:51:59 +02008198 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8199 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008200#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008201 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008202 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008204 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008205 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008206 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207 rq->migration_thread = NULL;
8208 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008209 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008211 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213 }
8214
Peter Williams2dd73a42006-06-27 02:54:34 -07008215 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008216
Avi Kivitye107be32007-07-26 13:40:43 +02008217#ifdef CONFIG_PREEMPT_NOTIFIERS
8218 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8219#endif
8220
Christoph Lameterc9819f42006-12-10 02:20:25 -08008221#ifdef CONFIG_SMP
8222 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8223#endif
8224
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008225#ifdef CONFIG_RT_MUTEXES
8226 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8227#endif
8228
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229 /*
8230 * The boot idle thread does lazy MMU switching as well:
8231 */
8232 atomic_inc(&init_mm.mm_count);
8233 enter_lazy_tlb(&init_mm, current);
8234
8235 /*
8236 * Make us the idle thread. Technically, schedule() should not be
8237 * called from this thread, however somewhere below it might be,
8238 * but because we are the idle thread, we just pick up running again
8239 * when this runqueue becomes "idle".
8240 */
8241 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008242 /*
8243 * During early bootup we pretend to be a normal task:
8244 */
8245 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008246
8247 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248}
8249
8250#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8251void __might_sleep(char *file, int line)
8252{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008253#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254 static unsigned long prev_jiffy; /* ratelimiting */
8255
8256 if ((in_atomic() || irqs_disabled()) &&
8257 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8258 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8259 return;
8260 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008261 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262 " context at %s:%d\n", file, line);
8263 printk("in_atomic():%d, irqs_disabled():%d\n",
8264 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008265 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008266 if (irqs_disabled())
8267 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268 dump_stack();
8269 }
8270#endif
8271}
8272EXPORT_SYMBOL(__might_sleep);
8273#endif
8274
8275#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008276static void normalize_task(struct rq *rq, struct task_struct *p)
8277{
8278 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02008279
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008280 update_rq_clock(rq);
8281 on_rq = p->se.on_rq;
8282 if (on_rq)
8283 deactivate_task(rq, p, 0);
8284 __setscheduler(rq, p, SCHED_NORMAL, 0);
8285 if (on_rq) {
8286 activate_task(rq, p, 0);
8287 resched_task(rq->curr);
8288 }
8289}
8290
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291void normalize_rt_tasks(void)
8292{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008293 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008295 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008296
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008297 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008298 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008299 /*
8300 * Only normalize user tasks:
8301 */
8302 if (!p->mm)
8303 continue;
8304
Ingo Molnardd41f592007-07-09 18:51:59 +02008305 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008306#ifdef CONFIG_SCHEDSTATS
8307 p->se.wait_start = 0;
8308 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008309 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008310#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008311
8312 if (!rt_task(p)) {
8313 /*
8314 * Renice negative nice level userspace
8315 * tasks back to 0:
8316 */
8317 if (TASK_NICE(p) < 0 && p->mm)
8318 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008322 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008323 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324
Ingo Molnar178be792007-10-15 17:00:18 +02008325 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008326
Ingo Molnarb29739f2006-06-27 02:54:51 -07008327 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008328 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008329 } while_each_thread(g, p);
8330
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008331 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332}
8333
8334#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008335
8336#ifdef CONFIG_IA64
8337/*
8338 * These functions are only useful for the IA64 MCA handling.
8339 *
8340 * They can only be called when the whole system has been
8341 * stopped - every CPU needs to be quiescent, and no scheduling
8342 * activity can take place. Using them for anything else would
8343 * be a serious bug, and as a result, they aren't even visible
8344 * under any other configuration.
8345 */
8346
8347/**
8348 * curr_task - return the current task for a given cpu.
8349 * @cpu: the processor in question.
8350 *
8351 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8352 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008353struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008354{
8355 return cpu_curr(cpu);
8356}
8357
8358/**
8359 * set_curr_task - set the current task for a given cpu.
8360 * @cpu: the processor in question.
8361 * @p: the task pointer to set.
8362 *
8363 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008364 * are serviced on a separate stack. It allows the architecture to switch the
8365 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008366 * must be called with all CPU's synchronized, and interrupts disabled, the
8367 * and caller must save the original value of the current task (see
8368 * curr_task() above) and restore that value before reenabling interrupts and
8369 * re-starting the system.
8370 *
8371 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8372 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008373void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008374{
8375 cpu_curr(cpu) = p;
8376}
8377
8378#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008379
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008380#ifdef CONFIG_FAIR_GROUP_SCHED
8381static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008382{
8383 int i;
8384
8385 for_each_possible_cpu(i) {
8386 if (tg->cfs_rq)
8387 kfree(tg->cfs_rq[i]);
8388 if (tg->se)
8389 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008390 }
8391
8392 kfree(tg->cfs_rq);
8393 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008394}
8395
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008396static
8397int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008398{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008399 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008400 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008401 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008402 int i;
8403
Mike Travis434d53b2008-04-04 18:11:04 -07008404 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008405 if (!tg->cfs_rq)
8406 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008407 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008408 if (!tg->se)
8409 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008410
8411 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008412
8413 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008414 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008415
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008416 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8417 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008418 if (!cfs_rq)
8419 goto err;
8420
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008421 se = kmalloc_node(sizeof(struct sched_entity),
8422 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008423 if (!se)
8424 goto err;
8425
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008426 parent_se = parent ? parent->se[i] : NULL;
8427 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008428 }
8429
8430 return 1;
8431
8432 err:
8433 return 0;
8434}
8435
8436static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8437{
8438 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8439 &cpu_rq(cpu)->leaf_cfs_rq_list);
8440}
8441
8442static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8443{
8444 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8445}
8446#else
8447static inline void free_fair_sched_group(struct task_group *tg)
8448{
8449}
8450
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008451static inline
8452int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008453{
8454 return 1;
8455}
8456
8457static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8458{
8459}
8460
8461static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8462{
8463}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008464#endif
8465
8466#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008467static void free_rt_sched_group(struct task_group *tg)
8468{
8469 int i;
8470
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008471 destroy_rt_bandwidth(&tg->rt_bandwidth);
8472
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008473 for_each_possible_cpu(i) {
8474 if (tg->rt_rq)
8475 kfree(tg->rt_rq[i]);
8476 if (tg->rt_se)
8477 kfree(tg->rt_se[i]);
8478 }
8479
8480 kfree(tg->rt_rq);
8481 kfree(tg->rt_se);
8482}
8483
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008484static
8485int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008486{
8487 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008488 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008489 struct rq *rq;
8490 int i;
8491
Mike Travis434d53b2008-04-04 18:11:04 -07008492 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008493 if (!tg->rt_rq)
8494 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008495 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008496 if (!tg->rt_se)
8497 goto err;
8498
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008499 init_rt_bandwidth(&tg->rt_bandwidth,
8500 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008501
8502 for_each_possible_cpu(i) {
8503 rq = cpu_rq(i);
8504
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008505 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8506 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8507 if (!rt_rq)
8508 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008509
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008510 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8511 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8512 if (!rt_se)
8513 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008514
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008515 parent_se = parent ? parent->rt_se[i] : NULL;
8516 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008517 }
8518
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008519 return 1;
8520
8521 err:
8522 return 0;
8523}
8524
8525static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8526{
8527 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8528 &cpu_rq(cpu)->leaf_rt_rq_list);
8529}
8530
8531static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8532{
8533 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8534}
8535#else
8536static inline void free_rt_sched_group(struct task_group *tg)
8537{
8538}
8539
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008540static inline
8541int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008542{
8543 return 1;
8544}
8545
8546static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8547{
8548}
8549
8550static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8551{
8552}
8553#endif
8554
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008555#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008556static void free_sched_group(struct task_group *tg)
8557{
8558 free_fair_sched_group(tg);
8559 free_rt_sched_group(tg);
8560 kfree(tg);
8561}
8562
8563/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008564struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008565{
8566 struct task_group *tg;
8567 unsigned long flags;
8568 int i;
8569
8570 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8571 if (!tg)
8572 return ERR_PTR(-ENOMEM);
8573
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008574 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008575 goto err;
8576
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008577 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008578 goto err;
8579
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008580 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008581 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008582 register_fair_sched_group(tg, i);
8583 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008584 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008585 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008586
8587 WARN_ON(!parent); /* root should already exist */
8588
8589 tg->parent = parent;
8590 list_add_rcu(&tg->siblings, &parent->children);
8591 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008592 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008593
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008594 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008595
8596err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008597 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008598 return ERR_PTR(-ENOMEM);
8599}
8600
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008601/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008602static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008603{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008604 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008605 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008606}
8607
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008608/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008609void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008610{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008611 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008612 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008613
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008614 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008615 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008616 unregister_fair_sched_group(tg, i);
8617 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008618 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008619 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008620 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008621 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008622
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008623 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008624 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008625}
8626
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008627/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008628 * The caller of this function should have put the task in its new group
8629 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8630 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008631 */
8632void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008633{
8634 int on_rq, running;
8635 unsigned long flags;
8636 struct rq *rq;
8637
8638 rq = task_rq_lock(tsk, &flags);
8639
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008640 update_rq_clock(rq);
8641
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008642 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008643 on_rq = tsk->se.on_rq;
8644
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008645 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008646 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008647 if (unlikely(running))
8648 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008649
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008650 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008651
Peter Zijlstra810b3812008-02-29 15:21:01 -05008652#ifdef CONFIG_FAIR_GROUP_SCHED
8653 if (tsk->sched_class->moved_group)
8654 tsk->sched_class->moved_group(tsk);
8655#endif
8656
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008657 if (unlikely(running))
8658 tsk->sched_class->set_curr_task(rq);
8659 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008660 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008661
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008662 task_rq_unlock(rq, &flags);
8663}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008664#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008665
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008666#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008667static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008668{
8669 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008670 int on_rq;
8671
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008672 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008673 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008674 dequeue_entity(cfs_rq, se, 0);
8675
8676 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008677 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008678
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008679 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008680 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008681}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008682
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008683static void set_se_shares(struct sched_entity *se, unsigned long shares)
8684{
8685 struct cfs_rq *cfs_rq = se->cfs_rq;
8686 struct rq *rq = cfs_rq->rq;
8687 unsigned long flags;
8688
8689 spin_lock_irqsave(&rq->lock, flags);
8690 __set_se_shares(se, shares);
8691 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008692}
8693
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008694static DEFINE_MUTEX(shares_mutex);
8695
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008696int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008697{
8698 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008699 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008700
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008701 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008702 * We can't change the weight of the root cgroup.
8703 */
8704 if (!tg->se[0])
8705 return -EINVAL;
8706
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008707 if (shares < MIN_SHARES)
8708 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008709 else if (shares > MAX_SHARES)
8710 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008711
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008712 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008713 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008714 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008715
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008716 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008717 for_each_possible_cpu(i)
8718 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008719 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008720 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008721
8722 /* wait for any ongoing reference to this group to finish */
8723 synchronize_sched();
8724
8725 /*
8726 * Now we are free to modify the group's share on each cpu
8727 * w/o tripping rebalance_share or load_balance_fair.
8728 */
8729 tg->shares = shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008730 for_each_possible_cpu(i) {
8731 /*
8732 * force a rebalance
8733 */
8734 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008735 set_se_shares(tg->se[i], shares);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008736 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008737
8738 /*
8739 * Enable load balance activity on this group, by inserting it back on
8740 * each cpu's rq->leaf_cfs_rq_list.
8741 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008742 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008743 for_each_possible_cpu(i)
8744 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008745 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008746 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008747done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008748 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008749 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008750}
8751
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008752unsigned long sched_group_shares(struct task_group *tg)
8753{
8754 return tg->shares;
8755}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008756#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008757
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008758#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008759/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008760 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008761 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008762static DEFINE_MUTEX(rt_constraints_mutex);
8763
8764static unsigned long to_ratio(u64 period, u64 runtime)
8765{
8766 if (runtime == RUNTIME_INF)
8767 return 1ULL << 16;
8768
Roman Zippel6f6d6a12008-05-01 04:34:28 -07008769 return div64_u64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008770}
8771
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008772#ifdef CONFIG_CGROUP_SCHED
8773static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8774{
8775 struct task_group *tgi, *parent = tg->parent;
8776 unsigned long total = 0;
8777
8778 if (!parent) {
8779 if (global_rt_period() < period)
8780 return 0;
8781
8782 return to_ratio(period, runtime) <
8783 to_ratio(global_rt_period(), global_rt_runtime());
8784 }
8785
8786 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8787 return 0;
8788
8789 rcu_read_lock();
8790 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8791 if (tgi == tg)
8792 continue;
8793
8794 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8795 tgi->rt_bandwidth.rt_runtime);
8796 }
8797 rcu_read_unlock();
8798
8799 return total + to_ratio(period, runtime) <
8800 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8801 parent->rt_bandwidth.rt_runtime);
8802}
8803#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008804static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008805{
8806 struct task_group *tgi;
8807 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008808 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008809 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008810
8811 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008812 list_for_each_entry_rcu(tgi, &task_groups, list) {
8813 if (tgi == tg)
8814 continue;
8815
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008816 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8817 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008818 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008819 rcu_read_unlock();
8820
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008821 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008822}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008823#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008824
Dhaval Giani521f1a242008-02-28 15:21:56 +05308825/* Must be called with tasklist_lock held */
8826static inline int tg_has_rt_tasks(struct task_group *tg)
8827{
8828 struct task_struct *g, *p;
8829 do_each_thread(g, p) {
8830 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8831 return 1;
8832 } while_each_thread(g, p);
8833 return 0;
8834}
8835
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008836static int tg_set_bandwidth(struct task_group *tg,
8837 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008838{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008839 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008840
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008841 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308842 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008843 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308844 err = -EBUSY;
8845 goto unlock;
8846 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008847 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8848 err = -EINVAL;
8849 goto unlock;
8850 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008851
8852 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008853 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8854 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008855
8856 for_each_possible_cpu(i) {
8857 struct rt_rq *rt_rq = tg->rt_rq[i];
8858
8859 spin_lock(&rt_rq->rt_runtime_lock);
8860 rt_rq->rt_runtime = rt_runtime;
8861 spin_unlock(&rt_rq->rt_runtime_lock);
8862 }
8863 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008864 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308865 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008866 mutex_unlock(&rt_constraints_mutex);
8867
8868 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008869}
8870
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008871int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8872{
8873 u64 rt_runtime, rt_period;
8874
8875 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8876 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8877 if (rt_runtime_us < 0)
8878 rt_runtime = RUNTIME_INF;
8879
8880 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8881}
8882
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008883long sched_group_rt_runtime(struct task_group *tg)
8884{
8885 u64 rt_runtime_us;
8886
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008887 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008888 return -1;
8889
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008890 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008891 do_div(rt_runtime_us, NSEC_PER_USEC);
8892 return rt_runtime_us;
8893}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008894
8895int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8896{
8897 u64 rt_runtime, rt_period;
8898
8899 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8900 rt_runtime = tg->rt_bandwidth.rt_runtime;
8901
8902 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8903}
8904
8905long sched_group_rt_period(struct task_group *tg)
8906{
8907 u64 rt_period_us;
8908
8909 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8910 do_div(rt_period_us, NSEC_PER_USEC);
8911 return rt_period_us;
8912}
8913
8914static int sched_rt_global_constraints(void)
8915{
8916 int ret = 0;
8917
8918 mutex_lock(&rt_constraints_mutex);
8919 if (!__rt_schedulable(NULL, 1, 0))
8920 ret = -EINVAL;
8921 mutex_unlock(&rt_constraints_mutex);
8922
8923 return ret;
8924}
8925#else
8926static int sched_rt_global_constraints(void)
8927{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008928 unsigned long flags;
8929 int i;
8930
8931 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
8932 for_each_possible_cpu(i) {
8933 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8934
8935 spin_lock(&rt_rq->rt_runtime_lock);
8936 rt_rq->rt_runtime = global_rt_runtime();
8937 spin_unlock(&rt_rq->rt_runtime_lock);
8938 }
8939 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
8940
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008941 return 0;
8942}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008943#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008944
8945int sched_rt_handler(struct ctl_table *table, int write,
8946 struct file *filp, void __user *buffer, size_t *lenp,
8947 loff_t *ppos)
8948{
8949 int ret;
8950 int old_period, old_runtime;
8951 static DEFINE_MUTEX(mutex);
8952
8953 mutex_lock(&mutex);
8954 old_period = sysctl_sched_rt_period;
8955 old_runtime = sysctl_sched_rt_runtime;
8956
8957 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8958
8959 if (!ret && write) {
8960 ret = sched_rt_global_constraints();
8961 if (ret) {
8962 sysctl_sched_rt_period = old_period;
8963 sysctl_sched_rt_runtime = old_runtime;
8964 } else {
8965 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8966 def_rt_bandwidth.rt_period =
8967 ns_to_ktime(global_rt_period());
8968 }
8969 }
8970 mutex_unlock(&mutex);
8971
8972 return ret;
8973}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008974
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008975#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008976
8977/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008978static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008979{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008980 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8981 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008982}
8983
8984static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008985cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008986{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008987 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008988
Paul Menage2b01dfe2007-10-24 18:23:50 +02008989 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008990 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008991 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008992 return &init_task_group.css;
8993 }
8994
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008995 parent = cgroup_tg(cgrp->parent);
8996 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008997 if (IS_ERR(tg))
8998 return ERR_PTR(-ENOMEM);
8999
9000 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009001 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009002
9003 return &tg->css;
9004}
9005
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009006static void
9007cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009008{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009009 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009010
9011 sched_destroy_group(tg);
9012}
9013
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009014static int
9015cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9016 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009017{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009018#ifdef CONFIG_RT_GROUP_SCHED
9019 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009020 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009021 return -EINVAL;
9022#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009023 /* We don't support RT-tasks being in separate groups */
9024 if (tsk->sched_class != &fair_sched_class)
9025 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009026#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009027
9028 return 0;
9029}
9030
9031static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009032cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009033 struct cgroup *old_cont, struct task_struct *tsk)
9034{
9035 sched_move_task(tsk);
9036}
9037
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009038#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009039static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009040 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009041{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009042 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009043}
9044
Paul Menagef4c753b2008-04-29 00:59:56 -07009045static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009046{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009047 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009048
9049 return (u64) tg->shares;
9050}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009051#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009052
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009053#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07009054static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009055 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009056{
Paul Menage06ecb272008-04-29 01:00:06 -07009057 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009058}
9059
Paul Menage06ecb272008-04-29 01:00:06 -07009060static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009061{
Paul Menage06ecb272008-04-29 01:00:06 -07009062 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009063}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009064
9065static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9066 u64 rt_period_us)
9067{
9068 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9069}
9070
9071static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9072{
9073 return sched_group_rt_period(cgroup_tg(cgrp));
9074}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009075#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009076
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009077static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009078#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009079 {
9080 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009081 .read_u64 = cpu_shares_read_u64,
9082 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009083 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009084#endif
9085#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009086 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009087 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009088 .read_s64 = cpu_rt_runtime_read,
9089 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009090 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009091 {
9092 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009093 .read_u64 = cpu_rt_period_read_uint,
9094 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009095 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009096#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009097};
9098
9099static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9100{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009101 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009102}
9103
9104struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009105 .name = "cpu",
9106 .create = cpu_cgroup_create,
9107 .destroy = cpu_cgroup_destroy,
9108 .can_attach = cpu_cgroup_can_attach,
9109 .attach = cpu_cgroup_attach,
9110 .populate = cpu_cgroup_populate,
9111 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009112 .early_init = 1,
9113};
9114
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009115#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009116
9117#ifdef CONFIG_CGROUP_CPUACCT
9118
9119/*
9120 * CPU accounting code for task groups.
9121 *
9122 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9123 * (balbir@in.ibm.com).
9124 */
9125
9126/* track cpu usage of a group of tasks */
9127struct cpuacct {
9128 struct cgroup_subsys_state css;
9129 /* cpuusage holds pointer to a u64-type object on every cpu */
9130 u64 *cpuusage;
9131};
9132
9133struct cgroup_subsys cpuacct_subsys;
9134
9135/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309136static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009137{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309138 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009139 struct cpuacct, css);
9140}
9141
9142/* return cpu accounting group to which this task belongs */
9143static inline struct cpuacct *task_ca(struct task_struct *tsk)
9144{
9145 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9146 struct cpuacct, css);
9147}
9148
9149/* create a new cpu accounting group */
9150static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309151 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009152{
9153 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9154
9155 if (!ca)
9156 return ERR_PTR(-ENOMEM);
9157
9158 ca->cpuusage = alloc_percpu(u64);
9159 if (!ca->cpuusage) {
9160 kfree(ca);
9161 return ERR_PTR(-ENOMEM);
9162 }
9163
9164 return &ca->css;
9165}
9166
9167/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009168static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309169cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009170{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309171 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009172
9173 free_percpu(ca->cpuusage);
9174 kfree(ca);
9175}
9176
9177/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309178static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009179{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309180 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009181 u64 totalcpuusage = 0;
9182 int i;
9183
9184 for_each_possible_cpu(i) {
9185 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9186
9187 /*
9188 * Take rq->lock to make 64-bit addition safe on 32-bit
9189 * platforms.
9190 */
9191 spin_lock_irq(&cpu_rq(i)->lock);
9192 totalcpuusage += *cpuusage;
9193 spin_unlock_irq(&cpu_rq(i)->lock);
9194 }
9195
9196 return totalcpuusage;
9197}
9198
Dhaval Giani0297b802008-02-29 10:02:44 +05309199static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9200 u64 reset)
9201{
9202 struct cpuacct *ca = cgroup_ca(cgrp);
9203 int err = 0;
9204 int i;
9205
9206 if (reset) {
9207 err = -EINVAL;
9208 goto out;
9209 }
9210
9211 for_each_possible_cpu(i) {
9212 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9213
9214 spin_lock_irq(&cpu_rq(i)->lock);
9215 *cpuusage = 0;
9216 spin_unlock_irq(&cpu_rq(i)->lock);
9217 }
9218out:
9219 return err;
9220}
9221
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009222static struct cftype files[] = {
9223 {
9224 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009225 .read_u64 = cpuusage_read,
9226 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009227 },
9228};
9229
Dhaval Giani32cd7562008-02-29 10:02:43 +05309230static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009231{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309232 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009233}
9234
9235/*
9236 * charge this task's execution time to its accounting group.
9237 *
9238 * called with rq->lock held.
9239 */
9240static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9241{
9242 struct cpuacct *ca;
9243
9244 if (!cpuacct_subsys.active)
9245 return;
9246
9247 ca = task_ca(tsk);
9248 if (ca) {
9249 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9250
9251 *cpuusage += cputime;
9252 }
9253}
9254
9255struct cgroup_subsys cpuacct_subsys = {
9256 .name = "cpuacct",
9257 .create = cpuacct_create,
9258 .destroy = cpuacct_destroy,
9259 .populate = cpuacct_populate,
9260 .subsys_id = cpuacct_subsys_id,
9261};
9262#endif /* CONFIG_CGROUP_CPUACCT */