blob: 1ec3fb2efee6d6bcccb72cc292a59e0b82ba959c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/sched.c
3 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
35#include <linux/smp_lock.h>
36#include <asm/mmu_context.h>
37#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/completion.h>
40#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070041#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
57#include <linux/kthread.h>
58#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020059#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/syscalls.h>
61#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070062#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080063#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070064#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070065#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020066#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020067#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010068#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070069#include <linux/tick.h>
Mike Travis434d53b2008-04-04 18:11:04 -070070#include <linux/bootmem.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020071#include <linux/debugfs.h>
72#include <linux/ctype.h>
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020073#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Eric Dumazet5517d862007-05-08 00:32:57 -070075#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020076#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * Convert user-nice values [ -20 ... 0 ... 19 ]
80 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
81 * and back.
82 */
83#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
84#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
85#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
86
87/*
88 * 'User priority' is the nice value converted to something we
89 * can work with better when scaling various scheduler parameters,
90 * it's a [ 0 ... 39 ] range.
91 */
92#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
93#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
94#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
95
96/*
Ingo Molnard7876a02008-01-25 21:08:19 +010097 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +010099#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200101#define NICE_0_LOAD SCHED_LOAD_SCALE
102#define NICE_0_SHIFT SCHED_LOAD_SHIFT
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * These are the 'tuning knobs' of the scheduler:
106 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200107 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * Timeslices get refilled after they expire.
109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700111
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200112/*
113 * single value that denotes runtime == period, ie unlimited time.
114 */
115#define RUNTIME_INF ((u64)~0ULL)
116
Eric Dumazet5517d862007-05-08 00:32:57 -0700117#ifdef CONFIG_SMP
118/*
119 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
120 * Since cpu_power is a 'constant', we can use a reciprocal divide.
121 */
122static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
123{
124 return reciprocal_divide(load, sg->reciprocal_cpu_power);
125}
126
127/*
128 * Each time a sched group cpu_power is changed,
129 * we must compute its reciprocal value
130 */
131static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
132{
133 sg->__cpu_power += val;
134 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
135}
136#endif
137
Ingo Molnare05606d2007-07-09 18:51:59 +0200138static inline int rt_policy(int policy)
139{
140 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
141 return 1;
142 return 0;
143}
144
145static inline int task_has_rt_policy(struct task_struct *p)
146{
147 return rt_policy(p->policy);
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200151 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200153struct rt_prio_array {
154 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
155 struct list_head queue[MAX_RT_PRIO];
156};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200158struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100159 /* nests inside the rq lock: */
160 spinlock_t rt_runtime_lock;
161 ktime_t rt_period;
162 u64 rt_runtime;
163 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200164};
165
166static struct rt_bandwidth def_rt_bandwidth;
167
168static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
169
170static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
171{
172 struct rt_bandwidth *rt_b =
173 container_of(timer, struct rt_bandwidth, rt_period_timer);
174 ktime_t now;
175 int overrun;
176 int idle = 0;
177
178 for (;;) {
179 now = hrtimer_cb_get_time(timer);
180 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
181
182 if (!overrun)
183 break;
184
185 idle = do_sched_rt_period_timer(rt_b, overrun);
186 }
187
188 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
189}
190
191static
192void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
193{
194 rt_b->rt_period = ns_to_ktime(period);
195 rt_b->rt_runtime = runtime;
196
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200197 spin_lock_init(&rt_b->rt_runtime_lock);
198
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200199 hrtimer_init(&rt_b->rt_period_timer,
200 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
201 rt_b->rt_period_timer.function = sched_rt_period_timer;
202 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
203}
204
205static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
206{
207 ktime_t now;
208
209 if (rt_b->rt_runtime == RUNTIME_INF)
210 return;
211
212 if (hrtimer_active(&rt_b->rt_period_timer))
213 return;
214
215 spin_lock(&rt_b->rt_runtime_lock);
216 for (;;) {
217 if (hrtimer_active(&rt_b->rt_period_timer))
218 break;
219
220 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
221 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
222 hrtimer_start(&rt_b->rt_period_timer,
223 rt_b->rt_period_timer.expires,
224 HRTIMER_MODE_ABS);
225 }
226 spin_unlock(&rt_b->rt_runtime_lock);
227}
228
229#ifdef CONFIG_RT_GROUP_SCHED
230static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
231{
232 hrtimer_cancel(&rt_b->rt_period_timer);
233}
234#endif
235
Heiko Carstens712555e2008-04-28 11:33:07 +0200236/*
237 * sched_domains_mutex serializes calls to arch_init_sched_domains,
238 * detach_destroy_domains and partition_sched_domains.
239 */
240static DEFINE_MUTEX(sched_domains_mutex);
241
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100242#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200243
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700244#include <linux/cgroup.h>
245
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200246struct cfs_rq;
247
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100248static LIST_HEAD(task_groups);
249
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200250/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200251struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100252#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700253 struct cgroup_subsys_state css;
254#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100255
256#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200257 /* schedulable entities of this group on each cpu */
258 struct sched_entity **se;
259 /* runqueue "owned" by this group on each cpu */
260 struct cfs_rq **cfs_rq;
261 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100262#endif
263
264#ifdef CONFIG_RT_GROUP_SCHED
265 struct sched_rt_entity **rt_se;
266 struct rt_rq **rt_rq;
267
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200268 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100269#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100270
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100271 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100272 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200273
274 struct task_group *parent;
275 struct list_head siblings;
276 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200277};
278
Dhaval Giani354d60c2008-04-19 19:44:59 +0200279#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200280
281/*
282 * Root task group.
283 * Every UID task group (including init_task_group aka UID-0) will
284 * be a child to this group.
285 */
286struct task_group root_task_group;
287
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100288#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200289/* Default task group's sched entity on each cpu */
290static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
291/* Default task group's cfs_rq on each cpu */
292static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100293#endif
294
295#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100296static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
297static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100298#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200299#else
300#define root_task_group init_task_group
Dhaval Giani354d60c2008-04-19 19:44:59 +0200301#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100302
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100303/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100304 * a task group's cpu shares.
305 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100306static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100307
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100308#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100309#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100310# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200311#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100312# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200313#endif
314
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800315/*
316 * A weight of 0, 1 or ULONG_MAX can cause arithmetics problems.
317 * (The default weight is 1024 - so there's no practical
318 * limitation from this.)
319 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200320#define MIN_SHARES 2
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800321#define MAX_SHARES (ULONG_MAX - 1)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200322
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100323static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100324#endif
325
326/* Default task group.
327 * Every task in system belong to this group at bootup.
328 */
Mike Travis434d53b2008-04-04 18:11:04 -0700329struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200330
331/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200332static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200333{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200334 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200335
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100336#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200337 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100338#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700339 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
340 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200341#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100342 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200343#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200344 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200345}
346
347/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100348static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200349{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100350#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100351 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
352 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100353#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100354
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100355#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100356 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
357 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100358#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200359}
360
361#else
362
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100363static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200364
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100365#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200366
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200367/* CFS-related fields in a runqueue */
368struct cfs_rq {
369 struct load_weight load;
370 unsigned long nr_running;
371
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200372 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200373 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200374
375 struct rb_root tasks_timeline;
376 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200377
378 struct list_head tasks;
379 struct list_head *balance_iterator;
380
381 /*
382 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200383 * It is set to NULL otherwise (i.e when none are currently running).
384 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100385 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200386
387 unsigned long nr_spread_over;
388
Ingo Molnar62160e3f2007-10-15 17:00:03 +0200389#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200390 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
391
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100392 /*
393 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200394 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
395 * (like users, containers etc.)
396 *
397 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
398 * list is used during load balance.
399 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100400 struct list_head leaf_cfs_rq_list;
401 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200402
403#ifdef CONFIG_SMP
404 unsigned long task_weight;
405 unsigned long shares;
406 /*
407 * We need space to build a sched_domain wide view of the full task
408 * group tree, in order to avoid depending on dynamic memory allocation
409 * during the load balancing we place this in the per cpu task group
410 * hierarchy. This limits the load balancing to one instance per cpu,
411 * but more should not be needed anyway.
412 */
413 struct aggregate_struct {
414 /*
415 * load = weight(cpus) * f(tg)
416 *
417 * Where f(tg) is the recursive weight fraction assigned to
418 * this group.
419 */
420 unsigned long load;
421
422 /*
423 * part of the group weight distributed to this span.
424 */
425 unsigned long shares;
426
427 /*
428 * The sum of all runqueue weights within this span.
429 */
430 unsigned long rq_weight;
431
432 /*
433 * Weight contributed by tasks; this is the part we can
434 * influence by moving tasks around.
435 */
436 unsigned long task_weight;
437 } aggregate;
438#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200439#endif
440};
441
442/* Real-Time classes' related field in a runqueue: */
443struct rt_rq {
444 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100445 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100446#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100447 int highest_prio; /* highest queued rt task prio */
448#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100449#ifdef CONFIG_SMP
Gregory Haskins73fe6aae2008-01-25 21:08:07 +0100450 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc12008-01-25 21:08:12 +0100451 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100452#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100453 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100454 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200455 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100456 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200457 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100458
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100459#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100460 unsigned long rt_nr_boosted;
461
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100462 struct rq *rq;
463 struct list_head leaf_rt_rq_list;
464 struct task_group *tg;
465 struct sched_rt_entity *rt_se;
466#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200467};
468
Gregory Haskins57d885f2008-01-25 21:08:18 +0100469#ifdef CONFIG_SMP
470
471/*
472 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100473 * variables. Each exclusive cpuset essentially defines an island domain by
474 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100475 * exclusive cpuset is created, we also create and attach a new root-domain
476 * object.
477 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100478 */
479struct root_domain {
480 atomic_t refcount;
481 cpumask_t span;
482 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100483
Ingo Molnar0eab9142008-01-25 21:08:19 +0100484 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100485 * The "RT overload" flag: it gets set if a CPU has more than
486 * one runnable RT task.
487 */
488 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100489 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100490};
491
Gregory Haskinsdc938522008-01-25 21:08:26 +0100492/*
493 * By default the system creates a single root-domain with all cpus as
494 * members (mimicking the global state we have today).
495 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100496static struct root_domain def_root_domain;
497
498#endif
499
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200500/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * This is the main, per-CPU runqueue data structure.
502 *
503 * Locking rule: those places that want to lock multiple runqueues
504 * (such as the load balancing or the thread migration code), lock
505 * acquire operations must be ordered by ascending &runqueue.
506 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700507struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200508 /* runqueue lock: */
509 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /*
512 * nr_running and cpu_load should be in the same cacheline because
513 * remote CPUs use both these fields when doing load calculation.
514 */
515 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200516 #define CPU_LOAD_IDX_MAX 5
517 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700518 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700519#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200520 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700521 unsigned char in_nohz_recently;
522#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200523 /* capture load from *all* tasks on this cpu: */
524 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200525 unsigned long nr_load_updates;
526 u64 nr_switches;
527
528 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100529 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100530
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200531#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200532 /* list of leaf cfs_rq on this cpu: */
533 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100534#endif
535#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100536 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /*
540 * This is part of a global counter where only the total sum
541 * over all CPUs matters. A task can increase this counter on
542 * one CPU and if it got migrated afterwards it may decrease
543 * it on another CPU. Always updated under the runqueue lock:
544 */
545 unsigned long nr_uninterruptible;
546
Ingo Molnar36c8b582006-07-03 00:25:41 -0700547 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800548 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200550
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200551 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 atomic_t nr_iowait;
554
555#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100556 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct sched_domain *sd;
558
559 /* For active balancing */
560 int active_balance;
561 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200562 /* cpu of this runqueue: */
563 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Ingo Molnar36c8b582006-07-03 00:25:41 -0700565 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct list_head migration_queue;
567#endif
568
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100569#ifdef CONFIG_SCHED_HRTICK
570 unsigned long hrtick_flags;
571 ktime_t hrtick_expire;
572 struct hrtimer hrtick_timer;
573#endif
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575#ifdef CONFIG_SCHEDSTATS
576 /* latency stats */
577 struct sched_info rq_sched_info;
578
579 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200580 unsigned int yld_exp_empty;
581 unsigned int yld_act_empty;
582 unsigned int yld_both_empty;
583 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200586 unsigned int sched_switch;
587 unsigned int sched_count;
588 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200591 unsigned int ttwu_count;
592 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200593
594 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200595 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700597 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598};
599
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700600static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Ingo Molnardd41f592007-07-09 18:51:59 +0200602static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
603{
604 rq->curr->sched_class->check_preempt_curr(rq, p);
605}
606
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700607static inline int cpu_of(struct rq *rq)
608{
609#ifdef CONFIG_SMP
610 return rq->cpu;
611#else
612 return 0;
613#endif
614}
615
Ingo Molnar20d315d2007-07-09 18:51:58 +0200616/*
Nick Piggin674311d2005-06-25 14:57:27 -0700617 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700618 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700619 *
620 * The domain tree of any CPU may only be accessed from within
621 * preempt-disabled sections.
622 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700623#define for_each_domain(cpu, __sd) \
624 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
627#define this_rq() (&__get_cpu_var(runqueues))
628#define task_rq(p) cpu_rq(task_cpu(p))
629#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
630
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200631static inline void update_rq_clock(struct rq *rq)
632{
633 rq->clock = sched_clock_cpu(cpu_of(rq));
634}
635
Ingo Molnare436d802007-07-19 21:28:35 +0200636/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200637 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
638 */
639#ifdef CONFIG_SCHED_DEBUG
640# define const_debug __read_mostly
641#else
642# define const_debug static const
643#endif
644
Ingo Molnar017730c2008-05-12 21:20:52 +0200645/**
646 * runqueue_is_locked
647 *
648 * Returns true if the current cpu runqueue is locked.
649 * This interface allows printk to be called with the runqueue lock
650 * held and know whether or not it is OK to wake up the klogd.
651 */
652int runqueue_is_locked(void)
653{
654 int cpu = get_cpu();
655 struct rq *rq = cpu_rq(cpu);
656 int ret;
657
658 ret = spin_is_locked(&rq->lock);
659 put_cpu();
660 return ret;
661}
662
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200663/*
664 * Debugging: various feature bits
665 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200666
667#define SCHED_FEAT(name, enabled) \
668 __SCHED_FEAT_##name ,
669
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200670enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200671#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200672};
673
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200674#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200675
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200676#define SCHED_FEAT(name, enabled) \
677 (1UL << __SCHED_FEAT_##name) * enabled |
678
679const_debug unsigned int sysctl_sched_features =
680#include "sched_features.h"
681 0;
682
683#undef SCHED_FEAT
684
685#ifdef CONFIG_SCHED_DEBUG
686#define SCHED_FEAT(name, enabled) \
687 #name ,
688
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700689static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200690#include "sched_features.h"
691 NULL
692};
693
694#undef SCHED_FEAT
695
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700696static int sched_feat_open(struct inode *inode, struct file *filp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200697{
698 filp->private_data = inode->i_private;
699 return 0;
700}
701
702static ssize_t
703sched_feat_read(struct file *filp, char __user *ubuf,
704 size_t cnt, loff_t *ppos)
705{
706 char *buf;
707 int r = 0;
708 int len = 0;
709 int i;
710
711 for (i = 0; sched_feat_names[i]; i++) {
712 len += strlen(sched_feat_names[i]);
713 len += 4;
714 }
715
716 buf = kmalloc(len + 2, GFP_KERNEL);
717 if (!buf)
718 return -ENOMEM;
719
720 for (i = 0; sched_feat_names[i]; i++) {
721 if (sysctl_sched_features & (1UL << i))
722 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
723 else
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200724 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200725 }
726
727 r += sprintf(buf + r, "\n");
728 WARN_ON(r >= len + 2);
729
730 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
731
732 kfree(buf);
733
734 return r;
735}
736
737static ssize_t
738sched_feat_write(struct file *filp, const char __user *ubuf,
739 size_t cnt, loff_t *ppos)
740{
741 char buf[64];
742 char *cmp = buf;
743 int neg = 0;
744 int i;
745
746 if (cnt > 63)
747 cnt = 63;
748
749 if (copy_from_user(&buf, ubuf, cnt))
750 return -EFAULT;
751
752 buf[cnt] = 0;
753
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200754 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200755 neg = 1;
756 cmp += 3;
757 }
758
759 for (i = 0; sched_feat_names[i]; i++) {
760 int len = strlen(sched_feat_names[i]);
761
762 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
763 if (neg)
764 sysctl_sched_features &= ~(1UL << i);
765 else
766 sysctl_sched_features |= (1UL << i);
767 break;
768 }
769 }
770
771 if (!sched_feat_names[i])
772 return -EINVAL;
773
774 filp->f_pos += cnt;
775
776 return cnt;
777}
778
779static struct file_operations sched_feat_fops = {
780 .open = sched_feat_open,
781 .read = sched_feat_read,
782 .write = sched_feat_write,
783};
784
785static __init int sched_init_debug(void)
786{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200787 debugfs_create_file("sched_features", 0644, NULL, NULL,
788 &sched_feat_fops);
789
790 return 0;
791}
792late_initcall(sched_init_debug);
793
794#endif
795
796#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200797
798/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100799 * Number of tasks to iterate in a single balance run.
800 * Limited because this is done with IRQs disabled.
801 */
802const_debug unsigned int sysctl_sched_nr_migrate = 32;
803
804/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100805 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100806 * default: 1s
807 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100808unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100809
Ingo Molnar6892b752008-02-13 14:02:36 +0100810static __read_mostly int scheduler_running;
811
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100812/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100813 * part of the period that we allow rt tasks to run in us.
814 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100815 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100816int sysctl_sched_rt_runtime = 950000;
817
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200818static inline u64 global_rt_period(void)
819{
820 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
821}
822
823static inline u64 global_rt_runtime(void)
824{
825 if (sysctl_sched_rt_period < 0)
826 return RUNTIME_INF;
827
828 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
829}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100830
Ingo Molnar690229a2008-04-23 09:31:35 +0200831unsigned long long time_sync_thresh = 100000;
Ingo Molnar27ec4402008-02-28 21:00:21 +0100832
833static DEFINE_PER_CPU(unsigned long long, time_offset);
834static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
835
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100836/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100837 * Global lock which we take every now and then to synchronize
838 * the CPUs time. This method is not warp-safe, but it's good
839 * enough to synchronize slowly diverging time sources and thus
840 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200841 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100842static DEFINE_SPINLOCK(time_sync_lock);
843static unsigned long long prev_global_time;
844
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200845static unsigned long long __sync_cpu_clock(unsigned long long time, int cpu)
Ingo Molnar27ec4402008-02-28 21:00:21 +0100846{
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200847 /*
848 * We want this inlined, to not get tracer function calls
849 * in this critical section:
850 */
851 spin_acquire(&time_sync_lock.dep_map, 0, 0, _THIS_IP_);
852 __raw_spin_lock(&time_sync_lock.raw_lock);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100853
854 if (time < prev_global_time) {
855 per_cpu(time_offset, cpu) += prev_global_time - time;
856 time = prev_global_time;
857 } else {
858 prev_global_time = time;
859 }
860
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200861 __raw_spin_unlock(&time_sync_lock.raw_lock);
862 spin_release(&time_sync_lock.dep_map, 1, _THIS_IP_);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100863
864 return time;
865}
866
867static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200868{
Ingo Molnare436d802007-07-19 21:28:35 +0200869 unsigned long long now;
Ingo Molnare436d802007-07-19 21:28:35 +0200870
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100871 /*
872 * Only call sched_clock() if the scheduler has already been
873 * initialized (some code might call cpu_clock() very early):
874 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100875 if (unlikely(!scheduler_running))
876 return 0;
877
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200878 now = sched_clock_cpu(cpu);
Ingo Molnare436d802007-07-19 21:28:35 +0200879
880 return now;
881}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100882
883/*
884 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
885 * clock constructed from sched_clock():
886 */
887unsigned long long cpu_clock(int cpu)
888{
889 unsigned long long prev_cpu_time, time, delta_time;
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200890 unsigned long flags;
Ingo Molnar27ec4402008-02-28 21:00:21 +0100891
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200892 local_irq_save(flags);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100893 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
894 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
895 delta_time = time-prev_cpu_time;
896
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200897 if (unlikely(delta_time > time_sync_thresh)) {
Ingo Molnar27ec4402008-02-28 21:00:21 +0100898 time = __sync_cpu_clock(time, cpu);
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200899 per_cpu(prev_cpu_time, cpu) = time;
900 }
901 local_irq_restore(flags);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100902
903 return time;
904}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200905EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700908# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700910#ifndef finish_arch_switch
911# define finish_arch_switch(prev) do { } while (0)
912#endif
913
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100914static inline int task_current(struct rq *rq, struct task_struct *p)
915{
916 return rq->curr == p;
917}
918
Nick Piggin4866cde2005-06-25 14:57:23 -0700919#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700920static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700921{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100922 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700923}
924
Ingo Molnar70b97a72006-07-03 00:25:42 -0700925static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700926{
927}
928
Ingo Molnar70b97a72006-07-03 00:25:42 -0700929static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700930{
Ingo Molnarda04c032005-09-13 11:17:59 +0200931#ifdef CONFIG_DEBUG_SPINLOCK
932 /* this is a valid case when another task releases the spinlock */
933 rq->lock.owner = current;
934#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700935 /*
936 * If we are tracking spinlock dependencies then we have to
937 * fix up the runqueue lock - which gets 'carried over' from
938 * prev into current:
939 */
940 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
941
Nick Piggin4866cde2005-06-25 14:57:23 -0700942 spin_unlock_irq(&rq->lock);
943}
944
945#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700946static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700947{
948#ifdef CONFIG_SMP
949 return p->oncpu;
950#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100951 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700952#endif
953}
954
Ingo Molnar70b97a72006-07-03 00:25:42 -0700955static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700956{
957#ifdef CONFIG_SMP
958 /*
959 * We can optimise this out completely for !SMP, because the
960 * SMP rebalancing from interrupt is the only thing that cares
961 * here.
962 */
963 next->oncpu = 1;
964#endif
965#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
966 spin_unlock_irq(&rq->lock);
967#else
968 spin_unlock(&rq->lock);
969#endif
970}
971
Ingo Molnar70b97a72006-07-03 00:25:42 -0700972static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700973{
974#ifdef CONFIG_SMP
975 /*
976 * After ->oncpu is cleared, the task can be moved to a different CPU.
977 * We must ensure this doesn't happen until the switch is completely
978 * finished.
979 */
980 smp_wmb();
981 prev->oncpu = 0;
982#endif
983#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
984 local_irq_enable();
985#endif
986}
987#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700990 * __task_rq_lock - lock the runqueue a given task resides on.
991 * Must be called interrupts disabled.
992 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700993static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700994 __acquires(rq->lock)
995{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200996 for (;;) {
997 struct rq *rq = task_rq(p);
998 spin_lock(&rq->lock);
999 if (likely(rq == task_rq(p)))
1000 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -07001001 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001002 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07001003}
1004
1005/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001007 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 * explicitly disabling preemption.
1009 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001010static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 __acquires(rq->lock)
1012{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001013 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Andi Kleen3a5c3592007-10-15 17:00:14 +02001015 for (;;) {
1016 local_irq_save(*flags);
1017 rq = task_rq(p);
1018 spin_lock(&rq->lock);
1019 if (likely(rq == task_rq(p)))
1020 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Alexey Dobriyana9957442007-10-15 17:00:13 +02001025static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001026 __releases(rq->lock)
1027{
1028 spin_unlock(&rq->lock);
1029}
1030
Ingo Molnar70b97a72006-07-03 00:25:42 -07001031static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 __releases(rq->lock)
1033{
1034 spin_unlock_irqrestore(&rq->lock, *flags);
1035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001038 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001040static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 __acquires(rq->lock)
1042{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001043 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 local_irq_disable();
1046 rq = this_rq();
1047 spin_lock(&rq->lock);
1048
1049 return rq;
1050}
1051
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001052static void __resched_task(struct task_struct *p, int tif_bit);
1053
1054static inline void resched_task(struct task_struct *p)
1055{
1056 __resched_task(p, TIF_NEED_RESCHED);
1057}
1058
1059#ifdef CONFIG_SCHED_HRTICK
1060/*
1061 * Use HR-timers to deliver accurate preemption points.
1062 *
1063 * Its all a bit involved since we cannot program an hrt while holding the
1064 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1065 * reschedule event.
1066 *
1067 * When we get rescheduled we reprogram the hrtick_timer outside of the
1068 * rq->lock.
1069 */
1070static inline void resched_hrt(struct task_struct *p)
1071{
1072 __resched_task(p, TIF_HRTICK_RESCHED);
1073}
1074
1075static inline void resched_rq(struct rq *rq)
1076{
1077 unsigned long flags;
1078
1079 spin_lock_irqsave(&rq->lock, flags);
1080 resched_task(rq->curr);
1081 spin_unlock_irqrestore(&rq->lock, flags);
1082}
1083
1084enum {
1085 HRTICK_SET, /* re-programm hrtick_timer */
1086 HRTICK_RESET, /* not a new slice */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001087 HRTICK_BLOCK, /* stop hrtick operations */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001088};
1089
1090/*
1091 * Use hrtick when:
1092 * - enabled by features
1093 * - hrtimer is actually high res
1094 */
1095static inline int hrtick_enabled(struct rq *rq)
1096{
1097 if (!sched_feat(HRTICK))
1098 return 0;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001099 if (unlikely(test_bit(HRTICK_BLOCK, &rq->hrtick_flags)))
1100 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001101 return hrtimer_is_hres_active(&rq->hrtick_timer);
1102}
1103
1104/*
1105 * Called to set the hrtick timer state.
1106 *
1107 * called with rq->lock held and irqs disabled
1108 */
1109static void hrtick_start(struct rq *rq, u64 delay, int reset)
1110{
1111 assert_spin_locked(&rq->lock);
1112
1113 /*
1114 * preempt at: now + delay
1115 */
1116 rq->hrtick_expire =
1117 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1118 /*
1119 * indicate we need to program the timer
1120 */
1121 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1122 if (reset)
1123 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1124
1125 /*
1126 * New slices are called from the schedule path and don't need a
1127 * forced reschedule.
1128 */
1129 if (reset)
1130 resched_hrt(rq->curr);
1131}
1132
1133static void hrtick_clear(struct rq *rq)
1134{
1135 if (hrtimer_active(&rq->hrtick_timer))
1136 hrtimer_cancel(&rq->hrtick_timer);
1137}
1138
1139/*
1140 * Update the timer from the possible pending state.
1141 */
1142static void hrtick_set(struct rq *rq)
1143{
1144 ktime_t time;
1145 int set, reset;
1146 unsigned long flags;
1147
1148 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1149
1150 spin_lock_irqsave(&rq->lock, flags);
1151 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1152 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1153 time = rq->hrtick_expire;
1154 clear_thread_flag(TIF_HRTICK_RESCHED);
1155 spin_unlock_irqrestore(&rq->lock, flags);
1156
1157 if (set) {
1158 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1159 if (reset && !hrtimer_active(&rq->hrtick_timer))
1160 resched_rq(rq);
1161 } else
1162 hrtick_clear(rq);
1163}
1164
1165/*
1166 * High-resolution timer tick.
1167 * Runs from hardirq context with interrupts disabled.
1168 */
1169static enum hrtimer_restart hrtick(struct hrtimer *timer)
1170{
1171 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1172
1173 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1174
1175 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001176 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001177 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1178 spin_unlock(&rq->lock);
1179
1180 return HRTIMER_NORESTART;
1181}
1182
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001183static void hotplug_hrtick_disable(int cpu)
1184{
1185 struct rq *rq = cpu_rq(cpu);
1186 unsigned long flags;
1187
1188 spin_lock_irqsave(&rq->lock, flags);
1189 rq->hrtick_flags = 0;
1190 __set_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1191 spin_unlock_irqrestore(&rq->lock, flags);
1192
1193 hrtick_clear(rq);
1194}
1195
1196static void hotplug_hrtick_enable(int cpu)
1197{
1198 struct rq *rq = cpu_rq(cpu);
1199 unsigned long flags;
1200
1201 spin_lock_irqsave(&rq->lock, flags);
1202 __clear_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1203 spin_unlock_irqrestore(&rq->lock, flags);
1204}
1205
1206static int
1207hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1208{
1209 int cpu = (int)(long)hcpu;
1210
1211 switch (action) {
1212 case CPU_UP_CANCELED:
1213 case CPU_UP_CANCELED_FROZEN:
1214 case CPU_DOWN_PREPARE:
1215 case CPU_DOWN_PREPARE_FROZEN:
1216 case CPU_DEAD:
1217 case CPU_DEAD_FROZEN:
1218 hotplug_hrtick_disable(cpu);
1219 return NOTIFY_OK;
1220
1221 case CPU_UP_PREPARE:
1222 case CPU_UP_PREPARE_FROZEN:
1223 case CPU_DOWN_FAILED:
1224 case CPU_DOWN_FAILED_FROZEN:
1225 case CPU_ONLINE:
1226 case CPU_ONLINE_FROZEN:
1227 hotplug_hrtick_enable(cpu);
1228 return NOTIFY_OK;
1229 }
1230
1231 return NOTIFY_DONE;
1232}
1233
1234static void init_hrtick(void)
1235{
1236 hotcpu_notifier(hotplug_hrtick, 0);
1237}
1238
1239static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001240{
1241 rq->hrtick_flags = 0;
1242 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1243 rq->hrtick_timer.function = hrtick;
1244 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1245}
1246
1247void hrtick_resched(void)
1248{
1249 struct rq *rq;
1250 unsigned long flags;
1251
1252 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1253 return;
1254
1255 local_irq_save(flags);
1256 rq = cpu_rq(smp_processor_id());
1257 hrtick_set(rq);
1258 local_irq_restore(flags);
1259}
1260#else
1261static inline void hrtick_clear(struct rq *rq)
1262{
1263}
1264
1265static inline void hrtick_set(struct rq *rq)
1266{
1267}
1268
1269static inline void init_rq_hrtick(struct rq *rq)
1270{
1271}
1272
1273void hrtick_resched(void)
1274{
1275}
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001276
1277static inline void init_hrtick(void)
1278{
1279}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001280#endif
1281
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001282/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001283 * resched_task - mark a task 'to be rescheduled now'.
1284 *
1285 * On UP this means the setting of the need_resched flag, on SMP it
1286 * might also involve a cross-CPU call to trigger the scheduler on
1287 * the target CPU.
1288 */
1289#ifdef CONFIG_SMP
1290
1291#ifndef tsk_is_polling
1292#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1293#endif
1294
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001295static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001296{
1297 int cpu;
1298
1299 assert_spin_locked(&task_rq(p)->lock);
1300
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001301 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001302 return;
1303
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001304 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001305
1306 cpu = task_cpu(p);
1307 if (cpu == smp_processor_id())
1308 return;
1309
1310 /* NEED_RESCHED must be visible before we test polling */
1311 smp_mb();
1312 if (!tsk_is_polling(p))
1313 smp_send_reschedule(cpu);
1314}
1315
1316static void resched_cpu(int cpu)
1317{
1318 struct rq *rq = cpu_rq(cpu);
1319 unsigned long flags;
1320
1321 if (!spin_trylock_irqsave(&rq->lock, flags))
1322 return;
1323 resched_task(cpu_curr(cpu));
1324 spin_unlock_irqrestore(&rq->lock, flags);
1325}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001326
1327#ifdef CONFIG_NO_HZ
1328/*
1329 * When add_timer_on() enqueues a timer into the timer wheel of an
1330 * idle CPU then this timer might expire before the next timer event
1331 * which is scheduled to wake up that CPU. In case of a completely
1332 * idle system the next event might even be infinite time into the
1333 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1334 * leaves the inner idle loop so the newly added timer is taken into
1335 * account when the CPU goes back to idle and evaluates the timer
1336 * wheel for the next timer event.
1337 */
1338void wake_up_idle_cpu(int cpu)
1339{
1340 struct rq *rq = cpu_rq(cpu);
1341
1342 if (cpu == smp_processor_id())
1343 return;
1344
1345 /*
1346 * This is safe, as this function is called with the timer
1347 * wheel base lock of (cpu) held. When the CPU is on the way
1348 * to idle and has not yet set rq->curr to idle then it will
1349 * be serialized on the timer wheel base lock and take the new
1350 * timer into account automatically.
1351 */
1352 if (rq->curr != rq->idle)
1353 return;
1354
1355 /*
1356 * We can set TIF_RESCHED on the idle task of the other CPU
1357 * lockless. The worst case is that the other CPU runs the
1358 * idle task through an additional NOOP schedule()
1359 */
1360 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1361
1362 /* NEED_RESCHED must be visible before we test polling */
1363 smp_mb();
1364 if (!tsk_is_polling(rq->idle))
1365 smp_send_reschedule(cpu);
1366}
1367#endif
1368
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001369#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001370static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001371{
1372 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001373 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001374}
1375#endif
1376
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001377#if BITS_PER_LONG == 32
1378# define WMULT_CONST (~0UL)
1379#else
1380# define WMULT_CONST (1UL << 32)
1381#endif
1382
1383#define WMULT_SHIFT 32
1384
Ingo Molnar194081e2007-08-09 11:16:51 +02001385/*
1386 * Shift right and round:
1387 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001388#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001389
Peter Zijlstra8f1bc3852008-04-19 19:45:00 +02001390/*
1391 * delta *= weight / lw
1392 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001393static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001394calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1395 struct load_weight *lw)
1396{
1397 u64 tmp;
1398
Peter Zijlstrae05510d2008-05-05 23:56:17 +02001399 if (!lw->inv_weight)
1400 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)/(lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001401
1402 tmp = (u64)delta_exec * weight;
1403 /*
1404 * Check whether we'd overflow the 64-bit multiplication:
1405 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001406 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001407 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001408 WMULT_SHIFT/2);
1409 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001410 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001411
Ingo Molnarecf691d2007-08-02 17:41:40 +02001412 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001413}
1414
Ingo Molnar10919852007-10-15 17:00:04 +02001415static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001416{
1417 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001418 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001419}
1420
Ingo Molnar10919852007-10-15 17:00:04 +02001421static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001422{
1423 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001424 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001425}
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001428 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1429 * of tasks with abnormal "nice" values across CPUs the contribution that
1430 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001431 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001432 * scaled version of the new time slice allocation that they receive on time
1433 * slice expiry etc.
1434 */
1435
Ingo Molnardd41f592007-07-09 18:51:59 +02001436#define WEIGHT_IDLEPRIO 2
1437#define WMULT_IDLEPRIO (1 << 31)
1438
1439/*
1440 * Nice levels are multiplicative, with a gentle 10% change for every
1441 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1442 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1443 * that remained on nice 0.
1444 *
1445 * The "10% effect" is relative and cumulative: from _any_ nice level,
1446 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee62007-07-16 09:46:30 +02001447 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1448 * If a task goes up by ~10% and another task goes down by ~10% then
1449 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001450 */
1451static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001452 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1453 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1454 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1455 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1456 /* 0 */ 1024, 820, 655, 526, 423,
1457 /* 5 */ 335, 272, 215, 172, 137,
1458 /* 10 */ 110, 87, 70, 56, 45,
1459 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001460};
1461
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001462/*
1463 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1464 *
1465 * In cases where the weight does not change often, we can use the
1466 * precalculated inverse to speed up arithmetics by turning divisions
1467 * into multiplications:
1468 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001469static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001470 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1471 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1472 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1473 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1474 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1475 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1476 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1477 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001478};
Peter Williams2dd73a42006-06-27 02:54:34 -07001479
Ingo Molnardd41f592007-07-09 18:51:59 +02001480static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1481
1482/*
1483 * runqueue iterator, to support SMP load-balancing between different
1484 * scheduling classes, without having to expose their internal data
1485 * structures to the load-balancing proper:
1486 */
1487struct rq_iterator {
1488 void *arg;
1489 struct task_struct *(*start)(void *);
1490 struct task_struct *(*next)(void *);
1491};
1492
Peter Williamse1d14842007-10-24 18:23:51 +02001493#ifdef CONFIG_SMP
1494static unsigned long
1495balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1496 unsigned long max_load_move, struct sched_domain *sd,
1497 enum cpu_idle_type idle, int *all_pinned,
1498 int *this_best_prio, struct rq_iterator *iterator);
1499
1500static int
1501iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1502 struct sched_domain *sd, enum cpu_idle_type idle,
1503 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001504#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001505
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001506#ifdef CONFIG_CGROUP_CPUACCT
1507static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1508#else
1509static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1510#endif
1511
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001512static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1513{
1514 update_load_add(&rq->load, load);
1515}
1516
1517static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1518{
1519 update_load_sub(&rq->load, load);
1520}
1521
Gregory Haskinse7693a32008-01-25 21:08:09 +01001522#ifdef CONFIG_SMP
1523static unsigned long source_load(int cpu, int type);
1524static unsigned long target_load(int cpu, int type);
1525static unsigned long cpu_avg_load_per_task(int cpu);
1526static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001527
1528#ifdef CONFIG_FAIR_GROUP_SCHED
1529
1530/*
1531 * Group load balancing.
1532 *
1533 * We calculate a few balance domain wide aggregate numbers; load and weight.
1534 * Given the pictures below, and assuming each item has equal weight:
1535 *
1536 * root 1 - thread
1537 * / | \ A - group
1538 * A 1 B
1539 * /|\ / \
1540 * C 2 D 3 4
1541 * | |
1542 * 5 6
1543 *
1544 * load:
1545 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1546 * which equals 1/9-th of the total load.
1547 *
1548 * shares:
1549 * The weight of this group on the selected cpus.
1550 *
1551 * rq_weight:
1552 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1553 * B would get 2.
1554 *
1555 * task_weight:
1556 * Part of the rq_weight contributed by tasks; all groups except B would
1557 * get 1, B gets 2.
1558 */
1559
1560static inline struct aggregate_struct *
1561aggregate(struct task_group *tg, struct sched_domain *sd)
1562{
1563 return &tg->cfs_rq[sd->first_cpu]->aggregate;
1564}
1565
1566typedef void (*aggregate_func)(struct task_group *, struct sched_domain *);
1567
1568/*
1569 * Iterate the full tree, calling @down when first entering a node and @up when
1570 * leaving it for the final time.
1571 */
1572static
1573void aggregate_walk_tree(aggregate_func down, aggregate_func up,
1574 struct sched_domain *sd)
1575{
1576 struct task_group *parent, *child;
1577
1578 rcu_read_lock();
1579 parent = &root_task_group;
1580down:
1581 (*down)(parent, sd);
1582 list_for_each_entry_rcu(child, &parent->children, siblings) {
1583 parent = child;
1584 goto down;
1585
1586up:
1587 continue;
1588 }
1589 (*up)(parent, sd);
1590
1591 child = parent;
1592 parent = parent->parent;
1593 if (parent)
1594 goto up;
1595 rcu_read_unlock();
1596}
1597
1598/*
1599 * Calculate the aggregate runqueue weight.
1600 */
1601static
1602void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd)
1603{
1604 unsigned long rq_weight = 0;
1605 unsigned long task_weight = 0;
1606 int i;
1607
1608 for_each_cpu_mask(i, sd->span) {
1609 rq_weight += tg->cfs_rq[i]->load.weight;
1610 task_weight += tg->cfs_rq[i]->task_weight;
1611 }
1612
1613 aggregate(tg, sd)->rq_weight = rq_weight;
1614 aggregate(tg, sd)->task_weight = task_weight;
1615}
1616
1617/*
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001618 * Compute the weight of this group on the given cpus.
1619 */
1620static
1621void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd)
1622{
1623 unsigned long shares = 0;
1624 int i;
1625
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001626 for_each_cpu_mask(i, sd->span)
1627 shares += tg->cfs_rq[i]->shares;
1628
Peter Zijlstra3f5087a2008-04-25 00:25:08 +02001629 if ((!shares && aggregate(tg, sd)->rq_weight) || shares > tg->shares)
1630 shares = tg->shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001631
1632 aggregate(tg, sd)->shares = shares;
1633}
1634
1635/*
1636 * Compute the load fraction assigned to this group, relies on the aggregate
1637 * weight and this group's parent's load, i.e. top-down.
1638 */
1639static
1640void aggregate_group_load(struct task_group *tg, struct sched_domain *sd)
1641{
1642 unsigned long load;
1643
1644 if (!tg->parent) {
1645 int i;
1646
1647 load = 0;
1648 for_each_cpu_mask(i, sd->span)
1649 load += cpu_rq(i)->load.weight;
1650
1651 } else {
1652 load = aggregate(tg->parent, sd)->load;
1653
1654 /*
1655 * shares is our weight in the parent's rq so
1656 * shares/parent->rq_weight gives our fraction of the load
1657 */
1658 load *= aggregate(tg, sd)->shares;
1659 load /= aggregate(tg->parent, sd)->rq_weight + 1;
1660 }
1661
1662 aggregate(tg, sd)->load = load;
1663}
1664
1665static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1666
1667/*
1668 * Calculate and set the cpu's group shares.
1669 */
1670static void
1671__update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,
1672 int tcpu)
1673{
1674 int boost = 0;
1675 unsigned long shares;
1676 unsigned long rq_weight;
1677
1678 if (!tg->se[tcpu])
1679 return;
1680
1681 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1682
1683 /*
1684 * If there are currently no tasks on the cpu pretend there is one of
1685 * average load so that when a new task gets to run here it will not
1686 * get delayed by group starvation.
1687 */
1688 if (!rq_weight) {
1689 boost = 1;
1690 rq_weight = NICE_0_LOAD;
1691 }
1692
1693 /*
1694 * \Sum shares * rq_weight
1695 * shares = -----------------------
1696 * \Sum rq_weight
1697 *
1698 */
1699 shares = aggregate(tg, sd)->shares * rq_weight;
1700 shares /= aggregate(tg, sd)->rq_weight + 1;
1701
1702 /*
1703 * record the actual number of shares, not the boosted amount.
1704 */
1705 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1706
1707 if (shares < MIN_SHARES)
1708 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08001709 else if (shares > MAX_SHARES)
1710 shares = MAX_SHARES;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001711
1712 __set_se_shares(tg->se[tcpu], shares);
1713}
1714
1715/*
1716 * Re-adjust the weights on the cpu the task came from and on the cpu the
1717 * task went to.
1718 */
1719static void
1720__move_group_shares(struct task_group *tg, struct sched_domain *sd,
1721 int scpu, int dcpu)
1722{
1723 unsigned long shares;
1724
1725 shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1726
1727 __update_group_shares_cpu(tg, sd, scpu);
1728 __update_group_shares_cpu(tg, sd, dcpu);
1729
1730 /*
1731 * ensure we never loose shares due to rounding errors in the
1732 * above redistribution.
1733 */
1734 shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1735 if (shares)
1736 tg->cfs_rq[dcpu]->shares += shares;
1737}
1738
1739/*
1740 * Because changing a group's shares changes the weight of the super-group
1741 * we need to walk up the tree and change all shares until we hit the root.
1742 */
1743static void
1744move_group_shares(struct task_group *tg, struct sched_domain *sd,
1745 int scpu, int dcpu)
1746{
1747 while (tg) {
1748 __move_group_shares(tg, sd, scpu, dcpu);
1749 tg = tg->parent;
1750 }
1751}
1752
1753static
1754void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd)
1755{
1756 unsigned long shares = aggregate(tg, sd)->shares;
1757 int i;
1758
1759 for_each_cpu_mask(i, sd->span) {
1760 struct rq *rq = cpu_rq(i);
1761 unsigned long flags;
1762
1763 spin_lock_irqsave(&rq->lock, flags);
1764 __update_group_shares_cpu(tg, sd, i);
1765 spin_unlock_irqrestore(&rq->lock, flags);
1766 }
1767
1768 aggregate_group_shares(tg, sd);
1769
1770 /*
1771 * ensure we never loose shares due to rounding errors in the
1772 * above redistribution.
1773 */
1774 shares -= aggregate(tg, sd)->shares;
1775 if (shares) {
1776 tg->cfs_rq[sd->first_cpu]->shares += shares;
1777 aggregate(tg, sd)->shares += shares;
1778 }
1779}
1780
1781/*
1782 * Calculate the accumulative weight and recursive load of each task group
1783 * while walking down the tree.
1784 */
1785static
1786void aggregate_get_down(struct task_group *tg, struct sched_domain *sd)
1787{
1788 aggregate_group_weight(tg, sd);
1789 aggregate_group_shares(tg, sd);
1790 aggregate_group_load(tg, sd);
1791}
1792
1793/*
1794 * Rebalance the cpu shares while walking back up the tree.
1795 */
1796static
1797void aggregate_get_up(struct task_group *tg, struct sched_domain *sd)
1798{
1799 aggregate_group_set_shares(tg, sd);
1800}
1801
1802static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1803
1804static void __init init_aggregate(void)
1805{
1806 int i;
1807
1808 for_each_possible_cpu(i)
1809 spin_lock_init(&per_cpu(aggregate_lock, i));
1810}
1811
1812static int get_aggregate(struct sched_domain *sd)
1813{
1814 if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu)))
1815 return 0;
1816
1817 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd);
1818 return 1;
1819}
1820
1821static void put_aggregate(struct sched_domain *sd)
1822{
1823 spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu));
1824}
1825
1826static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1827{
1828 cfs_rq->shares = shares;
1829}
1830
1831#else
1832
1833static inline void init_aggregate(void)
1834{
1835}
1836
1837static inline int get_aggregate(struct sched_domain *sd)
1838{
1839 return 0;
1840}
1841
1842static inline void put_aggregate(struct sched_domain *sd)
1843{
1844}
1845#endif
1846
1847#else /* CONFIG_SMP */
1848
1849#ifdef CONFIG_FAIR_GROUP_SCHED
1850static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1851{
1852}
1853#endif
1854
Gregory Haskinse7693a32008-01-25 21:08:09 +01001855#endif /* CONFIG_SMP */
1856
Ingo Molnardd41f592007-07-09 18:51:59 +02001857#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001858#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001859#include "sched_fair.c"
1860#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001861#ifdef CONFIG_SCHED_DEBUG
1862# include "sched_debug.c"
1863#endif
1864
1865#define sched_class_highest (&rt_sched_class)
1866
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001867static void inc_nr_running(struct rq *rq)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001868{
1869 rq->nr_running++;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001870}
1871
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001872static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001873{
1874 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001875}
1876
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001877static void set_load_weight(struct task_struct *p)
1878{
1879 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001880 p->se.load.weight = prio_to_weight[0] * 2;
1881 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1882 return;
1883 }
1884
1885 /*
1886 * SCHED_IDLE tasks get minimal weight:
1887 */
1888 if (p->policy == SCHED_IDLE) {
1889 p->se.load.weight = WEIGHT_IDLEPRIO;
1890 p->se.load.inv_weight = WMULT_IDLEPRIO;
1891 return;
1892 }
1893
1894 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1895 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001896}
1897
Ingo Molnar8159f872007-08-09 11:16:49 +02001898static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001899{
1900 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001901 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001902 p->se.on_rq = 1;
1903}
1904
Ingo Molnar69be72c2007-08-09 11:16:49 +02001905static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001906{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001907 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001908 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001909}
1910
1911/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001912 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001913 */
Ingo Molnar14531182007-07-09 18:51:59 +02001914static inline int __normal_prio(struct task_struct *p)
1915{
Ingo Molnardd41f592007-07-09 18:51:59 +02001916 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001917}
1918
1919/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001920 * Calculate the expected normal priority: i.e. priority
1921 * without taking RT-inheritance into account. Might be
1922 * boosted by interactivity modifiers. Changes upon fork,
1923 * setprio syscalls, and whenever the interactivity
1924 * estimator recalculates.
1925 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001926static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001927{
1928 int prio;
1929
Ingo Molnare05606d2007-07-09 18:51:59 +02001930 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001931 prio = MAX_RT_PRIO-1 - p->rt_priority;
1932 else
1933 prio = __normal_prio(p);
1934 return prio;
1935}
1936
1937/*
1938 * Calculate the current priority, i.e. the priority
1939 * taken into account by the scheduler. This value might
1940 * be boosted by RT tasks, or might be boosted by
1941 * interactivity modifiers. Will be RT if the task got
1942 * RT-boosted. If not then it returns p->normal_prio.
1943 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001944static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001945{
1946 p->normal_prio = normal_prio(p);
1947 /*
1948 * If we are RT tasks or we were boosted to RT priority,
1949 * keep the priority unchanged. Otherwise, update priority
1950 * to the normal priority:
1951 */
1952 if (!rt_prio(p->prio))
1953 return p->normal_prio;
1954 return p->prio;
1955}
1956
1957/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001958 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001960static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001962 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001963 rq->nr_uninterruptible--;
1964
Ingo Molnar8159f872007-08-09 11:16:49 +02001965 enqueue_task(rq, p, wakeup);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001966 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
1969/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 * deactivate_task - remove a task from the runqueue.
1971 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001972static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001974 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001975 rq->nr_uninterruptible++;
1976
Ingo Molnar69be72c2007-08-09 11:16:49 +02001977 dequeue_task(rq, p, sleep);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001978 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981/**
1982 * task_curr - is this task currently executing on a CPU?
1983 * @p: the task in question.
1984 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001985inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 return cpu_curr(task_cpu(p)) == p;
1988}
1989
Peter Williams2dd73a42006-06-27 02:54:34 -07001990/* Used instead of source_load when we know the type == 0 */
1991unsigned long weighted_cpuload(const int cpu)
1992{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001993 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001994}
1995
1996static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1997{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001998 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001999#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01002000 /*
2001 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2002 * successfuly executed on another CPU. We must ensure that updates of
2003 * per-task data have been completed by this moment.
2004 */
2005 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02002006 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02002007#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07002008}
2009
Steven Rostedtcb469842008-01-25 21:08:22 +01002010static inline void check_class_changed(struct rq *rq, struct task_struct *p,
2011 const struct sched_class *prev_class,
2012 int oldprio, int running)
2013{
2014 if (prev_class != p->sched_class) {
2015 if (prev_class->switched_from)
2016 prev_class->switched_from(rq, p, running);
2017 p->sched_class->switched_to(rq, p, running);
2018 } else
2019 p->sched_class->prio_changed(rq, p, oldprio, running);
2020}
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02002023
Ingo Molnarcc367732007-10-15 17:00:18 +02002024/*
2025 * Is this task likely cache-hot:
2026 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002027static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002028task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2029{
2030 s64 delta;
2031
Ingo Molnarf540a602008-03-15 17:10:34 +01002032 /*
2033 * Buddy candidates are cache hot:
2034 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01002035 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01002036 return 1;
2037
Ingo Molnarcc367732007-10-15 17:00:18 +02002038 if (p->sched_class != &fair_sched_class)
2039 return 0;
2040
Ingo Molnar6bc16652007-10-15 17:00:18 +02002041 if (sysctl_sched_migration_cost == -1)
2042 return 1;
2043 if (sysctl_sched_migration_cost == 0)
2044 return 0;
2045
Ingo Molnarcc367732007-10-15 17:00:18 +02002046 delta = now - p->se.exec_start;
2047
2048 return delta < (s64)sysctl_sched_migration_cost;
2049}
2050
2051
Ingo Molnardd41f592007-07-09 18:51:59 +02002052void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002053{
Ingo Molnardd41f592007-07-09 18:51:59 +02002054 int old_cpu = task_cpu(p);
2055 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002056 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
2057 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02002058 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002059
2060 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002061
2062#ifdef CONFIG_SCHEDSTATS
2063 if (p->se.wait_start)
2064 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002065 if (p->se.sleep_start)
2066 p->se.sleep_start -= clock_offset;
2067 if (p->se.block_start)
2068 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02002069 if (old_cpu != new_cpu) {
2070 schedstat_inc(p, se.nr_migrations);
2071 if (task_hot(p, old_rq->clock, NULL))
2072 schedstat_inc(p, se.nr_forced2_migrations);
2073 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002074#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002075 p->se.vruntime -= old_cfsrq->min_vruntime -
2076 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02002077
2078 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002079}
2080
Ingo Molnar70b97a72006-07-03 00:25:42 -07002081struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Ingo Molnar36c8b582006-07-03 00:25:41 -07002084 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 int dest_cpu;
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002088};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090/*
2091 * The task's runqueue lock must be held.
2092 * Returns true if you have to wait for migration thread.
2093 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002094static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002095migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002097 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 /*
2100 * If the task is not on a runqueue (and not running), then
2101 * it is sufficient to simply update the task's cpu field.
2102 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002103 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 set_task_cpu(p, dest_cpu);
2105 return 0;
2106 }
2107
2108 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 req->task = p;
2110 req->dest_cpu = dest_cpu;
2111 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return 1;
2114}
2115
2116/*
2117 * wait_task_inactive - wait for a thread to unschedule.
2118 *
2119 * The caller must ensure that the task *will* unschedule sometime soon,
2120 * else this function might spin for a *long* time. This function can't
2121 * be called with interrupts off, or it may introduce deadlock with
2122 * smp_call_function() if an IPI is sent by the same process we are
2123 * waiting to become inactive.
2124 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002125void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002128 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002129 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Andi Kleen3a5c3592007-10-15 17:00:14 +02002131 for (;;) {
2132 /*
2133 * We do the initial early heuristics without holding
2134 * any task-queue locks at all. We'll only try to get
2135 * the runqueue lock when things look like they will
2136 * work out!
2137 */
2138 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002139
Andi Kleen3a5c3592007-10-15 17:00:14 +02002140 /*
2141 * If the task is actively running on another CPU
2142 * still, just relax and busy-wait without holding
2143 * any locks.
2144 *
2145 * NOTE! Since we don't hold any locks, it's not
2146 * even sure that "rq" stays as the right runqueue!
2147 * But we don't care, since "task_running()" will
2148 * return false if the runqueue has changed and p
2149 * is actually now running somewhere else!
2150 */
2151 while (task_running(rq, p))
2152 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002153
Andi Kleen3a5c3592007-10-15 17:00:14 +02002154 /*
2155 * Ok, time to look more closely! We need the rq
2156 * lock now, to be *sure*. If we're wrong, we'll
2157 * just go back and repeat.
2158 */
2159 rq = task_rq_lock(p, &flags);
2160 running = task_running(rq, p);
2161 on_rq = p->se.on_rq;
2162 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002163
Andi Kleen3a5c3592007-10-15 17:00:14 +02002164 /*
2165 * Was it really running after all now that we
2166 * checked with the proper locks actually held?
2167 *
2168 * Oops. Go back and try again..
2169 */
2170 if (unlikely(running)) {
2171 cpu_relax();
2172 continue;
2173 }
2174
2175 /*
2176 * It's not enough that it's not actively running,
2177 * it must be off the runqueue _entirely_, and not
2178 * preempted!
2179 *
2180 * So if it wa still runnable (but just not actively
2181 * running right now), it's preempted, and we should
2182 * yield - it could be a while.
2183 */
2184 if (unlikely(on_rq)) {
2185 schedule_timeout_uninterruptible(1);
2186 continue;
2187 }
2188
2189 /*
2190 * Ahh, all good. It wasn't running, and it wasn't
2191 * runnable, which means that it will never become
2192 * running in the future either. We're all done!
2193 */
2194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
2198/***
2199 * kick_process - kick a running thread to enter/exit the kernel
2200 * @p: the to-be-kicked thread
2201 *
2202 * Cause a process which is running on another CPU to enter
2203 * kernel-mode, without any delay. (to get signals handled.)
2204 *
2205 * NOTE: this function doesnt have to take the runqueue lock,
2206 * because all it wants to ensure is that the remote task enters
2207 * the kernel. If the IPI races and the task has been migrated
2208 * to another CPU then no harm is done and the purpose has been
2209 * achieved as well.
2210 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002211void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
2213 int cpu;
2214
2215 preempt_disable();
2216 cpu = task_cpu(p);
2217 if ((cpu != smp_processor_id()) && task_curr(p))
2218 smp_send_reschedule(cpu);
2219 preempt_enable();
2220}
2221
2222/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002223 * Return a low guess at the load of a migration-source cpu weighted
2224 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 *
2226 * We want to under-estimate the load of migration sources, to
2227 * balance conservatively.
2228 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002229static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002230{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002231 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002232 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002233
Peter Williams2dd73a42006-06-27 02:54:34 -07002234 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002235 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002236
Ingo Molnardd41f592007-07-09 18:51:59 +02002237 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
2240/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002241 * Return a high guess at the load of a migration-target cpu weighted
2242 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002244static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002245{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002246 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002247 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002248
Peter Williams2dd73a42006-06-27 02:54:34 -07002249 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002250 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002251
Ingo Molnardd41f592007-07-09 18:51:59 +02002252 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002253}
2254
2255/*
2256 * Return the average load per task on the cpu's run queue
2257 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002258static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07002259{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002260 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002261 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07002262 unsigned long n = rq->nr_running;
2263
Ingo Molnardd41f592007-07-09 18:51:59 +02002264 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
Nick Piggin147cbb42005-06-25 14:57:19 -07002267/*
2268 * find_idlest_group finds and returns the least busy CPU group within the
2269 * domain.
2270 */
2271static struct sched_group *
2272find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2273{
2274 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2275 unsigned long min_load = ULONG_MAX, this_load = 0;
2276 int load_idx = sd->forkexec_idx;
2277 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2278
2279 do {
2280 unsigned long load, avg_load;
2281 int local_group;
2282 int i;
2283
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002284 /* Skip over this group if it has no CPUs allowed */
2285 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002286 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002287
Nick Piggin147cbb42005-06-25 14:57:19 -07002288 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002289
2290 /* Tally up the load of all CPUs in the group */
2291 avg_load = 0;
2292
2293 for_each_cpu_mask(i, group->cpumask) {
2294 /* Bias balancing toward cpus of our domain */
2295 if (local_group)
2296 load = source_load(i, load_idx);
2297 else
2298 load = target_load(i, load_idx);
2299
2300 avg_load += load;
2301 }
2302
2303 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002304 avg_load = sg_div_cpu_power(group,
2305 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002306
2307 if (local_group) {
2308 this_load = avg_load;
2309 this = group;
2310 } else if (avg_load < min_load) {
2311 min_load = avg_load;
2312 idlest = group;
2313 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002314 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002315
2316 if (!idlest || 100*this_load < imbalance*min_load)
2317 return NULL;
2318 return idlest;
2319}
2320
2321/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002322 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002323 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002324static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002325find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2326 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002327{
2328 unsigned long load, min_load = ULONG_MAX;
2329 int idlest = -1;
2330 int i;
2331
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002332 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002333 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002334
Mike Travis7c16ec52008-04-04 18:11:11 -07002335 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002336 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002337
2338 if (load < min_load || (load == min_load && i == this_cpu)) {
2339 min_load = load;
2340 idlest = i;
2341 }
2342 }
2343
2344 return idlest;
2345}
2346
Nick Piggin476d1392005-06-25 14:57:29 -07002347/*
2348 * sched_balance_self: balance the current task (running on cpu) in domains
2349 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2350 * SD_BALANCE_EXEC.
2351 *
2352 * Balance, ie. select the least loaded group.
2353 *
2354 * Returns the target CPU number, or the same CPU if no balancing is needed.
2355 *
2356 * preempt must be disabled.
2357 */
2358static int sched_balance_self(int cpu, int flag)
2359{
2360 struct task_struct *t = current;
2361 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002362
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002363 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002364 /*
2365 * If power savings logic is enabled for a domain, stop there.
2366 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002367 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2368 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002369 if (tmp->flags & flag)
2370 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002371 }
Nick Piggin476d1392005-06-25 14:57:29 -07002372
2373 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002374 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002375 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002376 int new_cpu, weight;
2377
2378 if (!(sd->flags & flag)) {
2379 sd = sd->child;
2380 continue;
2381 }
Nick Piggin476d1392005-06-25 14:57:29 -07002382
2383 span = sd->span;
2384 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002385 if (!group) {
2386 sd = sd->child;
2387 continue;
2388 }
Nick Piggin476d1392005-06-25 14:57:29 -07002389
Mike Travis7c16ec52008-04-04 18:11:11 -07002390 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002391 if (new_cpu == -1 || new_cpu == cpu) {
2392 /* Now try balancing at a lower domain level of cpu */
2393 sd = sd->child;
2394 continue;
2395 }
Nick Piggin476d1392005-06-25 14:57:29 -07002396
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002397 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002398 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002399 sd = NULL;
2400 weight = cpus_weight(span);
2401 for_each_domain(cpu, tmp) {
2402 if (weight <= cpus_weight(tmp->span))
2403 break;
2404 if (tmp->flags & flag)
2405 sd = tmp;
2406 }
2407 /* while loop will break here if sd == NULL */
2408 }
2409
2410 return cpu;
2411}
2412
2413#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Ingo Molnar4e655512008-05-12 21:20:52 +02002415#ifdef CONFIG_CONTEXT_SWITCH_TRACER
2416
Ingo Molnar36fc25a2008-05-12 21:20:53 +02002417void ftrace_task(struct task_struct *p, void *__tr, void *__data)
2418{
2419#if 0
2420 /*
2421 * trace timeline tree
2422 */
2423 __trace_special(__tr, __data,
2424 p->pid, p->se.vruntime, p->se.sum_exec_runtime);
2425#else
2426 /*
2427 * trace balance metrics
2428 */
2429 __trace_special(__tr, __data,
2430 p->pid, p->se.avg_overlap, 0);
2431#endif
2432}
2433
Ingo Molnar4e655512008-05-12 21:20:52 +02002434void ftrace_all_fair_tasks(void *__rq, void *__tr, void *__data)
2435{
Ingo Molnar4e655512008-05-12 21:20:52 +02002436 struct task_struct *p;
Ingo Molnar24cd5d12008-05-12 21:20:52 +02002437 struct sched_entity *se;
Ingo Molnar4e655512008-05-12 21:20:52 +02002438 struct rb_node *curr;
2439 struct rq *rq = __rq;
2440
Ingo Molnar24cd5d12008-05-12 21:20:52 +02002441 if (rq->cfs.curr) {
2442 p = task_of(rq->cfs.curr);
Ingo Molnar36fc25a2008-05-12 21:20:53 +02002443 ftrace_task(p, __tr, __data);
Ingo Molnar24cd5d12008-05-12 21:20:52 +02002444 }
2445 if (rq->cfs.next) {
2446 p = task_of(rq->cfs.next);
Ingo Molnar36fc25a2008-05-12 21:20:53 +02002447 ftrace_task(p, __tr, __data);
Ingo Molnar24cd5d12008-05-12 21:20:52 +02002448 }
2449
Ingo Molnar36fc25a2008-05-12 21:20:53 +02002450 for (curr = first_fair(&rq->cfs); curr; curr = rb_next(curr)) {
Ingo Molnar4e655512008-05-12 21:20:52 +02002451 se = rb_entry(curr, struct sched_entity, run_node);
2452 if (!entity_is_task(se))
2453 continue;
2454
2455 p = task_of(se);
Ingo Molnar36fc25a2008-05-12 21:20:53 +02002456 ftrace_task(p, __tr, __data);
Ingo Molnar4e655512008-05-12 21:20:52 +02002457 }
2458}
2459
2460#endif
2461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462/***
2463 * try_to_wake_up - wake up a thread
2464 * @p: the to-be-woken-up thread
2465 * @state: the mask of task states that can be woken
2466 * @sync: do a synchronous wakeup?
2467 *
2468 * Put it on the run-queue if it's not already there. The "current"
2469 * thread is always on the run-queue (except when the actual
2470 * re-schedule is in progress), and as such you're allowed to do
2471 * the simpler "current->state = TASK_RUNNING" to mark yourself
2472 * runnable without the overhead of this.
2473 *
2474 * returns failure only if the task is already active.
2475 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002476static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Ingo Molnarcc367732007-10-15 17:00:18 +02002478 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 unsigned long flags;
2480 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002481 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Ingo Molnarb85d0662008-03-16 20:03:22 +01002483 if (!sched_feat(SYNC_WAKEUPS))
2484 sync = 0;
2485
Linus Torvalds04e2f172008-02-23 18:05:03 -08002486 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 rq = task_rq_lock(p, &flags);
2488 old_state = p->state;
2489 if (!(old_state & state))
2490 goto out;
2491
Ingo Molnardd41f592007-07-09 18:51:59 +02002492 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 goto out_running;
2494
2495 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002496 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 this_cpu = smp_processor_id();
2498
2499#ifdef CONFIG_SMP
2500 if (unlikely(task_running(rq, p)))
2501 goto out_activate;
2502
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002503 cpu = p->sched_class->select_task_rq(p, sync);
2504 if (cpu != orig_cpu) {
2505 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 task_rq_unlock(rq, &flags);
2507 /* might preempt at this point */
2508 rq = task_rq_lock(p, &flags);
2509 old_state = p->state;
2510 if (!(old_state & state))
2511 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002512 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 goto out_running;
2514
2515 this_cpu = smp_processor_id();
2516 cpu = task_cpu(p);
2517 }
2518
Gregory Haskinse7693a32008-01-25 21:08:09 +01002519#ifdef CONFIG_SCHEDSTATS
2520 schedstat_inc(rq, ttwu_count);
2521 if (cpu == this_cpu)
2522 schedstat_inc(rq, ttwu_local);
2523 else {
2524 struct sched_domain *sd;
2525 for_each_domain(this_cpu, sd) {
2526 if (cpu_isset(cpu, sd->span)) {
2527 schedstat_inc(sd, ttwu_wake_remote);
2528 break;
2529 }
2530 }
2531 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002532#endif
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534out_activate:
2535#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002536 schedstat_inc(p, se.nr_wakeups);
2537 if (sync)
2538 schedstat_inc(p, se.nr_wakeups_sync);
2539 if (orig_cpu != cpu)
2540 schedstat_inc(p, se.nr_wakeups_migrate);
2541 if (cpu == this_cpu)
2542 schedstat_inc(p, se.nr_wakeups_local);
2543 else
2544 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002545 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002546 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 success = 1;
2548
2549out_running:
Peter Zijlstra5429db22008-05-12 21:20:53 +02002550 ftrace_wake_up_task(rq, p, rq->curr);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002551 check_preempt_curr(rq, p);
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002554#ifdef CONFIG_SMP
2555 if (p->sched_class->task_wake_up)
2556 p->sched_class->task_wake_up(rq, p);
2557#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558out:
2559 task_rq_unlock(rq, &flags);
2560
2561 return success;
2562}
2563
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002564int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002566 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568EXPORT_SYMBOL(wake_up_process);
2569
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002570int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
2572 return try_to_wake_up(p, state, 0);
2573}
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575/*
2576 * Perform scheduler related setup for a newly forked process p.
2577 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002578 *
2579 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002581static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
Ingo Molnardd41f592007-07-09 18:51:59 +02002583 p->se.exec_start = 0;
2584 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002585 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002586 p->se.last_wakeup = 0;
2587 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002588
2589#ifdef CONFIG_SCHEDSTATS
2590 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002591 p->se.sum_sleep_runtime = 0;
2592 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002593 p->se.block_start = 0;
2594 p->se.sleep_max = 0;
2595 p->se.block_max = 0;
2596 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002597 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002598 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002599#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002600
Peter Zijlstrafa717062008-01-25 21:08:27 +01002601 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002602 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002603 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002604
Avi Kivitye107be32007-07-26 13:40:43 +02002605#ifdef CONFIG_PREEMPT_NOTIFIERS
2606 INIT_HLIST_HEAD(&p->preempt_notifiers);
2607#endif
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 /*
2610 * We mark the process as running here, but have not actually
2611 * inserted it onto the runqueue yet. This guarantees that
2612 * nobody will actually run it, and a signal or other external
2613 * event cannot wake it up and insert it on the runqueue either.
2614 */
2615 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002616}
2617
2618/*
2619 * fork()/clone()-time setup:
2620 */
2621void sched_fork(struct task_struct *p, int clone_flags)
2622{
2623 int cpu = get_cpu();
2624
2625 __sched_fork(p);
2626
2627#ifdef CONFIG_SMP
2628 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2629#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002630 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002631
2632 /*
2633 * Make sure we do not leak PI boosting priority to the child:
2634 */
2635 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002636 if (!rt_prio(p->prio))
2637 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002638
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002639#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002640 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002641 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002643#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002644 p->oncpu = 0;
2645#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002647 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002648 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002650 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
2653/*
2654 * wake_up_new_task - wake up a newly created task for the first time.
2655 *
2656 * This function will do some initial scheduler statistics housekeeping
2657 * that must be done for every newly created context, then puts the task
2658 * on the runqueue and wakes it.
2659 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002660void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
2662 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002663 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002667 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 p->prio = effective_prio(p);
2670
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002671 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002672 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002675 * Let the scheduling class do new task startup
2676 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002678 p->sched_class->task_new(rq, p);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002679 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 }
Ingo Molnar4e655512008-05-12 21:20:52 +02002681 ftrace_wake_up_task(rq, p, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02002682 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002683#ifdef CONFIG_SMP
2684 if (p->sched_class->task_wake_up)
2685 p->sched_class->task_wake_up(rq, p);
2686#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002687 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688}
2689
Avi Kivitye107be32007-07-26 13:40:43 +02002690#ifdef CONFIG_PREEMPT_NOTIFIERS
2691
2692/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002693 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2694 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002695 */
2696void preempt_notifier_register(struct preempt_notifier *notifier)
2697{
2698 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2699}
2700EXPORT_SYMBOL_GPL(preempt_notifier_register);
2701
2702/**
2703 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002704 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002705 *
2706 * This is safe to call from within a preemption notifier.
2707 */
2708void preempt_notifier_unregister(struct preempt_notifier *notifier)
2709{
2710 hlist_del(&notifier->link);
2711}
2712EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2713
2714static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2715{
2716 struct preempt_notifier *notifier;
2717 struct hlist_node *node;
2718
2719 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2720 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2721}
2722
2723static void
2724fire_sched_out_preempt_notifiers(struct task_struct *curr,
2725 struct task_struct *next)
2726{
2727 struct preempt_notifier *notifier;
2728 struct hlist_node *node;
2729
2730 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2731 notifier->ops->sched_out(notifier, next);
2732}
2733
2734#else
2735
2736static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2737{
2738}
2739
2740static void
2741fire_sched_out_preempt_notifiers(struct task_struct *curr,
2742 struct task_struct *next)
2743{
2744}
2745
2746#endif
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002749 * prepare_task_switch - prepare to switch tasks
2750 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002751 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002752 * @next: the task we are going to switch to.
2753 *
2754 * This is called with the rq lock held and interrupts off. It must
2755 * be paired with a subsequent finish_task_switch after the context
2756 * switch.
2757 *
2758 * prepare_task_switch sets up locking and calls architecture specific
2759 * hooks.
2760 */
Avi Kivitye107be32007-07-26 13:40:43 +02002761static inline void
2762prepare_task_switch(struct rq *rq, struct task_struct *prev,
2763 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002764{
Avi Kivitye107be32007-07-26 13:40:43 +02002765 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002766 prepare_lock_switch(rq, next);
2767 prepare_arch_switch(next);
2768}
2769
2770/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002772 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 * @prev: the thread we just switched away from.
2774 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002775 * finish_task_switch must be called after the context switch, paired
2776 * with a prepare_task_switch call before the context switch.
2777 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2778 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 *
2780 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002781 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 * with the lock held can cause deadlocks; see schedule() for
2783 * details.)
2784 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002785static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 __releases(rq->lock)
2787{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002789 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 rq->prev_mm = NULL;
2792
2793 /*
2794 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002795 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002796 * schedule one last time. The schedule call will never return, and
2797 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002798 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 * still held, otherwise prev could be scheduled on another cpu, die
2800 * there before we look at prev->state, and then the reference would
2801 * be dropped twice.
2802 * Manfred Spraul <manfred@colorfullife.com>
2803 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002804 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002805 finish_arch_switch(prev);
2806 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002807#ifdef CONFIG_SMP
2808 if (current->sched_class->post_schedule)
2809 current->sched_class->post_schedule(rq);
2810#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002811
Avi Kivitye107be32007-07-26 13:40:43 +02002812 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (mm)
2814 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002815 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002816 /*
2817 * Remove function-return probe instances associated with this
2818 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002819 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002820 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823}
2824
2825/**
2826 * schedule_tail - first thing a freshly forked thread must call.
2827 * @prev: the thread we just switched away from.
2828 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002829asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 __releases(rq->lock)
2831{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002832 struct rq *rq = this_rq();
2833
Nick Piggin4866cde2005-06-25 14:57:23 -07002834 finish_task_switch(rq, prev);
2835#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2836 /* In this case, finish_task_switch does not reenable preemption */
2837 preempt_enable();
2838#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002840 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
2842
2843/*
2844 * context_switch - switch to the new MM and the new
2845 * thread's register state.
2846 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002847static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002848context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002849 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Ingo Molnardd41f592007-07-09 18:51:59 +02002851 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Avi Kivitye107be32007-07-26 13:40:43 +02002853 prepare_task_switch(rq, prev, next);
Ingo Molnar4e655512008-05-12 21:20:52 +02002854 ftrace_ctx_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002855 mm = next->mm;
2856 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002857 /*
2858 * For paravirt, this is coupled with an exit in switch_to to
2859 * combine the page table reload and the switch backend into
2860 * one hypercall.
2861 */
2862 arch_enter_lazy_cpu_mode();
2863
Ingo Molnardd41f592007-07-09 18:51:59 +02002864 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 next->active_mm = oldmm;
2866 atomic_inc(&oldmm->mm_count);
2867 enter_lazy_tlb(oldmm, next);
2868 } else
2869 switch_mm(oldmm, mm, next);
2870
Ingo Molnardd41f592007-07-09 18:51:59 +02002871 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 rq->prev_mm = oldmm;
2874 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002875 /*
2876 * Since the runqueue lock will be released by the next
2877 * task (which is an invalid locking op but in the case
2878 * of the scheduler it's an obvious special-case), so we
2879 * do an early lockdep release here:
2880 */
2881#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002882 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002883#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 /* Here we just switch the register state and the stack. */
2886 switch_to(prev, next, prev);
2887
Ingo Molnardd41f592007-07-09 18:51:59 +02002888 barrier();
2889 /*
2890 * this_rq must be evaluated again because prev may have moved
2891 * CPUs since it called schedule(), thus the 'rq' on its stack
2892 * frame will be invalid.
2893 */
2894 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895}
2896
2897/*
2898 * nr_running, nr_uninterruptible and nr_context_switches:
2899 *
2900 * externally visible scheduler statistics: current number of runnable
2901 * threads, current number of uninterruptible-sleeping threads, total
2902 * number of context switches performed since bootup.
2903 */
2904unsigned long nr_running(void)
2905{
2906 unsigned long i, sum = 0;
2907
2908 for_each_online_cpu(i)
2909 sum += cpu_rq(i)->nr_running;
2910
2911 return sum;
2912}
2913
2914unsigned long nr_uninterruptible(void)
2915{
2916 unsigned long i, sum = 0;
2917
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002918 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 sum += cpu_rq(i)->nr_uninterruptible;
2920
2921 /*
2922 * Since we read the counters lockless, it might be slightly
2923 * inaccurate. Do not allow it to go below zero though:
2924 */
2925 if (unlikely((long)sum < 0))
2926 sum = 0;
2927
2928 return sum;
2929}
2930
2931unsigned long long nr_context_switches(void)
2932{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002933 int i;
2934 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002936 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 sum += cpu_rq(i)->nr_switches;
2938
2939 return sum;
2940}
2941
2942unsigned long nr_iowait(void)
2943{
2944 unsigned long i, sum = 0;
2945
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002946 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2948
2949 return sum;
2950}
2951
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002952unsigned long nr_active(void)
2953{
2954 unsigned long i, running = 0, uninterruptible = 0;
2955
2956 for_each_online_cpu(i) {
2957 running += cpu_rq(i)->nr_running;
2958 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2959 }
2960
2961 if (unlikely((long)uninterruptible < 0))
2962 uninterruptible = 0;
2963
2964 return running + uninterruptible;
2965}
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002968 * Update rq->cpu_load[] statistics. This function is usually called every
2969 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002970 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002971static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002972{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002973 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002974 int i, scale;
2975
2976 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002977
2978 /* Update our load: */
2979 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2980 unsigned long old_load, new_load;
2981
2982 /* scale is effectively 1 << i now, and >> i divides by scale */
2983
2984 old_load = this_rq->cpu_load[i];
2985 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002986 /*
2987 * Round up the averaging division if load is increasing. This
2988 * prevents us from getting stuck on 9 if the load is 10, for
2989 * example.
2990 */
2991 if (new_load > old_load)
2992 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002993 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2994 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002995}
2996
Ingo Molnardd41f592007-07-09 18:51:59 +02002997#ifdef CONFIG_SMP
2998
Ingo Molnar48f24c42006-07-03 00:25:40 -07002999/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 * double_rq_lock - safely lock two runqueues
3001 *
3002 * Note this does not disable interrupts like task_rq_lock,
3003 * you need to do so manually before calling.
3004 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003005static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 __acquires(rq1->lock)
3007 __acquires(rq2->lock)
3008{
Kirill Korotaev054b9102006-12-10 02:20:11 -08003009 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (rq1 == rq2) {
3011 spin_lock(&rq1->lock);
3012 __acquire(rq2->lock); /* Fake it out ;) */
3013 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003014 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 spin_lock(&rq1->lock);
3016 spin_lock(&rq2->lock);
3017 } else {
3018 spin_lock(&rq2->lock);
3019 spin_lock(&rq1->lock);
3020 }
3021 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003022 update_rq_clock(rq1);
3023 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024}
3025
3026/*
3027 * double_rq_unlock - safely unlock two runqueues
3028 *
3029 * Note this does not restore interrupts like task_rq_unlock,
3030 * you need to do so manually after calling.
3031 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003032static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 __releases(rq1->lock)
3034 __releases(rq2->lock)
3035{
3036 spin_unlock(&rq1->lock);
3037 if (rq1 != rq2)
3038 spin_unlock(&rq2->lock);
3039 else
3040 __release(rq2->lock);
3041}
3042
3043/*
3044 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3045 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01003046static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 __releases(this_rq->lock)
3048 __acquires(busiest->lock)
3049 __acquires(this_rq->lock)
3050{
Steven Rostedte8fa1362008-01-25 21:08:05 +01003051 int ret = 0;
3052
Kirill Korotaev054b9102006-12-10 02:20:11 -08003053 if (unlikely(!irqs_disabled())) {
3054 /* printk() doesn't work good under rq->lock */
3055 spin_unlock(&this_rq->lock);
3056 BUG_ON(1);
3057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003059 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 spin_unlock(&this_rq->lock);
3061 spin_lock(&busiest->lock);
3062 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01003063 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 } else
3065 spin_lock(&busiest->lock);
3066 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01003067 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
3070/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 * If dest_cpu is allowed for this process, migrate the task to it.
3072 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003073 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 * the cpu_allowed mask is restored.
3075 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003076static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003078 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003080 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
3082 rq = task_rq_lock(p, &flags);
3083 if (!cpu_isset(dest_cpu, p->cpus_allowed)
3084 || unlikely(cpu_is_offline(dest_cpu)))
3085 goto out;
3086
3087 /* force the process onto the specified CPU */
3088 if (migrate_task(p, dest_cpu, &req)) {
3089 /* Need to wait for migration thread (might exit: take ref). */
3090 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 get_task_struct(mt);
3093 task_rq_unlock(rq, &flags);
3094 wake_up_process(mt);
3095 put_task_struct(mt);
3096 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return;
3099 }
3100out:
3101 task_rq_unlock(rq, &flags);
3102}
3103
3104/*
Nick Piggin476d1392005-06-25 14:57:29 -07003105 * sched_exec - execve() is a valuable balancing opportunity, because at
3106 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 */
3108void sched_exec(void)
3109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003111 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003113 if (new_cpu != this_cpu)
3114 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
3116
3117/*
3118 * pull_task - move a task from a remote runqueue to the local runqueue.
3119 * Both runqueues must be locked.
3120 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003121static void pull_task(struct rq *src_rq, struct task_struct *p,
3122 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003124 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003126 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 /*
3128 * Note that idle threads have a prio of MAX_PRIO, for this test
3129 * to be always true for them.
3130 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003131 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132}
3133
3134/*
3135 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3136 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003137static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003138int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003139 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003140 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
3142 /*
3143 * We do not migrate tasks that are:
3144 * 1) running (obviously), or
3145 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3146 * 3) are cache-hot on their current CPU.
3147 */
Ingo Molnarcc367732007-10-15 17:00:18 +02003148 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3149 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003151 }
Nick Piggin81026792005-06-25 14:57:07 -07003152 *all_pinned = 0;
3153
Ingo Molnarcc367732007-10-15 17:00:18 +02003154 if (task_running(rq, p)) {
3155 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003156 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Ingo Molnarda84d962007-10-15 17:00:18 +02003159 /*
3160 * Aggressive migration if:
3161 * 1) task is cache cold, or
3162 * 2) too many balance attempts have failed.
3163 */
3164
Ingo Molnar6bc16652007-10-15 17:00:18 +02003165 if (!task_hot(p, rq->clock, sd) ||
3166 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003167#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02003168 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003169 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003170 schedstat_inc(p, se.nr_forced_migrations);
3171 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003172#endif
3173 return 1;
3174 }
3175
Ingo Molnarcc367732007-10-15 17:00:18 +02003176 if (task_hot(p, rq->clock, sd)) {
3177 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003178 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 return 1;
3181}
3182
Peter Williamse1d14842007-10-24 18:23:51 +02003183static unsigned long
3184balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3185 unsigned long max_load_move, struct sched_domain *sd,
3186 enum cpu_idle_type idle, int *all_pinned,
3187 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003188{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003189 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02003190 struct task_struct *p;
3191 long rem_load_move = max_load_move;
3192
Peter Williamse1d14842007-10-24 18:23:51 +02003193 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003194 goto out;
3195
3196 pinned = 1;
3197
3198 /*
3199 * Start the load-balancing iterator:
3200 */
3201 p = iterator->start(iterator->arg);
3202next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003203 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003204 goto out;
3205 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003206 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02003207 * skip a task if it will be the highest priority task (i.e. smallest
3208 * prio value) on its new queue regardless of its load weight
3209 */
3210 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
3211 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003212 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003213 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003214 p = iterator->next(iterator->arg);
3215 goto next;
3216 }
3217
3218 pull_task(busiest, p, this_rq, this_cpu);
3219 pulled++;
3220 rem_load_move -= p->se.load.weight;
3221
3222 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003223 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003224 */
Peter Williamse1d14842007-10-24 18:23:51 +02003225 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003226 if (p->prio < *this_best_prio)
3227 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003228 p = iterator->next(iterator->arg);
3229 goto next;
3230 }
3231out:
3232 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003233 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003234 * so we can safely collect pull_task() stats here rather than
3235 * inside pull_task().
3236 */
3237 schedstat_add(sd, lb_gained[idle], pulled);
3238
3239 if (all_pinned)
3240 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003241
3242 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003243}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245/*
Peter Williams43010652007-08-09 11:16:46 +02003246 * move_tasks tries to move up to max_load_move weighted load from busiest to
3247 * this_rq, as part of a balancing operation within domain "sd".
3248 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 *
3250 * Called with both runqueues locked.
3251 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003252static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003253 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003254 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003255 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003257 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003258 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003259 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Ingo Molnardd41f592007-07-09 18:51:59 +02003261 do {
Peter Williams43010652007-08-09 11:16:46 +02003262 total_load_moved +=
3263 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003264 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003265 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003266 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003267 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Peter Williams43010652007-08-09 11:16:46 +02003269 return total_load_moved > 0;
3270}
3271
Peter Williamse1d14842007-10-24 18:23:51 +02003272static int
3273iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3274 struct sched_domain *sd, enum cpu_idle_type idle,
3275 struct rq_iterator *iterator)
3276{
3277 struct task_struct *p = iterator->start(iterator->arg);
3278 int pinned = 0;
3279
3280 while (p) {
3281 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3282 pull_task(busiest, p, this_rq, this_cpu);
3283 /*
3284 * Right now, this is only the second place pull_task()
3285 * is called, so we can safely collect pull_task()
3286 * stats here rather than inside pull_task().
3287 */
3288 schedstat_inc(sd, lb_gained[idle]);
3289
3290 return 1;
3291 }
3292 p = iterator->next(iterator->arg);
3293 }
3294
3295 return 0;
3296}
3297
Peter Williams43010652007-08-09 11:16:46 +02003298/*
3299 * move_one_task tries to move exactly one task from busiest to this_rq, as
3300 * part of active balancing operations within "domain".
3301 * Returns 1 if successful and 0 otherwise.
3302 *
3303 * Called with both runqueues locked.
3304 */
3305static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3306 struct sched_domain *sd, enum cpu_idle_type idle)
3307{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003308 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003309
3310 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003311 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003312 return 1;
3313
3314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
3317/*
3318 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003319 * domain. It calculates and returns the amount of weighted load which
3320 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 */
3322static struct sched_group *
3323find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003324 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003325 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
3327 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3328 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003329 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003330 unsigned long busiest_load_per_task, busiest_nr_running;
3331 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003332 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003333#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3334 int power_savings_balance = 1;
3335 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3336 unsigned long min_nr_running = ULONG_MAX;
3337 struct sched_group *group_min = NULL, *group_leader = NULL;
3338#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
3340 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003341 busiest_load_per_task = busiest_nr_running = 0;
3342 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003343 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003344 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003345 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003346 load_idx = sd->newidle_idx;
3347 else
3348 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003351 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 int local_group;
3353 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003354 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003355 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003356 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
3358 local_group = cpu_isset(this_cpu, group->cpumask);
3359
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003360 if (local_group)
3361 balance_cpu = first_cpu(group->cpumask);
3362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003364 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02003365 max_cpu_load = 0;
3366 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
3368 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003369 struct rq *rq;
3370
3371 if (!cpu_isset(i, *cpus))
3372 continue;
3373
3374 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003375
Suresh Siddha9439aab2007-07-19 21:28:35 +02003376 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003377 *sd_idle = 0;
3378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003380 if (local_group) {
3381 if (idle_cpu(i) && !first_idle_cpu) {
3382 first_idle_cpu = 1;
3383 balance_cpu = i;
3384 }
3385
Nick Piggina2000572006-02-10 01:51:02 -08003386 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003387 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003388 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003389 if (load > max_cpu_load)
3390 max_cpu_load = load;
3391 if (min_cpu_load > load)
3392 min_cpu_load = load;
3393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
3395 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003396 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003397 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 }
3399
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003400 /*
3401 * First idle cpu or the first cpu(busiest) in this sched group
3402 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003403 * domains. In the newly idle case, we will allow all the cpu's
3404 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003405 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003406 if (idle != CPU_NEWLY_IDLE && local_group &&
3407 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003408 *balance = 0;
3409 goto ret;
3410 }
3411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003413 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
3415 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003416 avg_load = sg_div_cpu_power(group,
3417 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Ken Chen908a7c12007-10-17 16:55:11 +02003419 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3420 __group_imb = 1;
3421
Eric Dumazet5517d862007-05-08 00:32:57 -07003422 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 if (local_group) {
3425 this_load = avg_load;
3426 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003427 this_nr_running = sum_nr_running;
3428 this_load_per_task = sum_weighted_load;
3429 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003430 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 max_load = avg_load;
3432 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003433 busiest_nr_running = sum_nr_running;
3434 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003435 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003437
3438#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3439 /*
3440 * Busy processors will not participate in power savings
3441 * balance.
3442 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003443 if (idle == CPU_NOT_IDLE ||
3444 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3445 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003446
3447 /*
3448 * If the local group is idle or completely loaded
3449 * no need to do power savings balance at this domain
3450 */
3451 if (local_group && (this_nr_running >= group_capacity ||
3452 !this_nr_running))
3453 power_savings_balance = 0;
3454
Ingo Molnardd41f592007-07-09 18:51:59 +02003455 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003456 * If a group is already running at full capacity or idle,
3457 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003458 */
3459 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003460 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003461 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003462
Ingo Molnardd41f592007-07-09 18:51:59 +02003463 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003464 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003465 * This is the group from where we need to pick up the load
3466 * for saving power
3467 */
3468 if ((sum_nr_running < min_nr_running) ||
3469 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003470 first_cpu(group->cpumask) <
3471 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003472 group_min = group;
3473 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003474 min_load_per_task = sum_weighted_load /
3475 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003476 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003477
Ingo Molnardd41f592007-07-09 18:51:59 +02003478 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003479 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003480 * capacity but still has some space to pick up some load
3481 * from other group and save more power
3482 */
3483 if (sum_nr_running <= group_capacity - 1) {
3484 if (sum_nr_running > leader_nr_running ||
3485 (sum_nr_running == leader_nr_running &&
3486 first_cpu(group->cpumask) >
3487 first_cpu(group_leader->cpumask))) {
3488 group_leader = group;
3489 leader_nr_running = sum_nr_running;
3490 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003491 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003492group_next:
3493#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 group = group->next;
3495 } while (group != sd->groups);
3496
Peter Williams2dd73a42006-06-27 02:54:34 -07003497 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 goto out_balanced;
3499
3500 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3501
3502 if (this_load >= avg_load ||
3503 100*max_load <= sd->imbalance_pct*this_load)
3504 goto out_balanced;
3505
Peter Williams2dd73a42006-06-27 02:54:34 -07003506 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003507 if (group_imb)
3508 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3509
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 /*
3511 * We're trying to get all the cpus to the average_load, so we don't
3512 * want to push ourselves above the average load, nor do we wish to
3513 * reduce the max loaded cpu below the average load, as either of these
3514 * actions would just result in more rebalancing later, and ping-pong
3515 * tasks around. Thus we look for the minimum possible imbalance.
3516 * Negative imbalances (*we* are more loaded than anyone else) will
3517 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003518 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 * appear as very large values with unsigned longs.
3520 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003521 if (max_load <= busiest_load_per_task)
3522 goto out_balanced;
3523
3524 /*
3525 * In the presence of smp nice balancing, certain scenarios can have
3526 * max load less than avg load(as we skip the groups at or below
3527 * its cpu_power, while calculating max_load..)
3528 */
3529 if (max_load < avg_load) {
3530 *imbalance = 0;
3531 goto small_imbalance;
3532 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003533
3534 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003535 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003538 *imbalance = min(max_pull * busiest->__cpu_power,
3539 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 / SCHED_LOAD_SCALE;
3541
Peter Williams2dd73a42006-06-27 02:54:34 -07003542 /*
3543 * if *imbalance is less than the average load per runnable task
3544 * there is no gaurantee that any tasks will be moved so we'll have
3545 * a think about bumping its value to force at least one task to be
3546 * moved
3547 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003548 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003549 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003550 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Peter Williams2dd73a42006-06-27 02:54:34 -07003552small_imbalance:
3553 pwr_move = pwr_now = 0;
3554 imbn = 2;
3555 if (this_nr_running) {
3556 this_load_per_task /= this_nr_running;
3557 if (busiest_load_per_task > this_load_per_task)
3558 imbn = 1;
3559 } else
3560 this_load_per_task = SCHED_LOAD_SCALE;
3561
Ingo Molnardd41f592007-07-09 18:51:59 +02003562 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3563 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003564 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 return busiest;
3566 }
3567
3568 /*
3569 * OK, we don't have enough imbalance to justify moving tasks,
3570 * however we may be able to increase total CPU power used by
3571 * moving them.
3572 */
3573
Eric Dumazet5517d862007-05-08 00:32:57 -07003574 pwr_now += busiest->__cpu_power *
3575 min(busiest_load_per_task, max_load);
3576 pwr_now += this->__cpu_power *
3577 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 pwr_now /= SCHED_LOAD_SCALE;
3579
3580 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003581 tmp = sg_div_cpu_power(busiest,
3582 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003584 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003585 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003588 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003589 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003590 tmp = sg_div_cpu_power(this,
3591 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003593 tmp = sg_div_cpu_power(this,
3594 busiest_load_per_task * SCHED_LOAD_SCALE);
3595 pwr_move += this->__cpu_power *
3596 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 pwr_move /= SCHED_LOAD_SCALE;
3598
3599 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003600 if (pwr_move > pwr_now)
3601 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 }
3603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 return busiest;
3605
3606out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003607#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003608 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003609 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003611 if (this == group_leader && group_leader != group_min) {
3612 *imbalance = min_load_per_task;
3613 return group_min;
3614 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003615#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003616ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 *imbalance = 0;
3618 return NULL;
3619}
3620
3621/*
3622 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3623 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003624static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003625find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003626 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003628 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003629 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 int i;
3631
3632 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003633 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003634
3635 if (!cpu_isset(i, *cpus))
3636 continue;
3637
Ingo Molnar48f24c42006-07-03 00:25:40 -07003638 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003639 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Ingo Molnardd41f592007-07-09 18:51:59 +02003641 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003642 continue;
3643
Ingo Molnardd41f592007-07-09 18:51:59 +02003644 if (wl > max_load) {
3645 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003646 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 }
3648 }
3649
3650 return busiest;
3651}
3652
3653/*
Nick Piggin77391d72005-06-25 14:57:30 -07003654 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3655 * so long as it is large enough.
3656 */
3657#define MAX_PINNED_INTERVAL 512
3658
3659/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3661 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003663static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003664 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003665 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666{
Peter Williams43010652007-08-09 11:16:46 +02003667 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003670 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003671 unsigned long flags;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003672 int unlock_aggregate;
Nick Piggin5969fe02005-09-10 00:26:19 -07003673
Mike Travis7c16ec52008-04-04 18:11:11 -07003674 cpus_setall(*cpus);
3675
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003676 unlock_aggregate = get_aggregate(sd);
3677
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003678 /*
3679 * When power savings policy is enabled for the parent domain, idle
3680 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003681 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003682 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003683 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003684 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003685 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003686 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
Ingo Molnar2d723762007-10-15 17:00:12 +02003688 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003690redo:
3691 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003692 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003693
Chen, Kenneth W06066712006-12-10 02:20:35 -08003694 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003695 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 if (!group) {
3698 schedstat_inc(sd, lb_nobusyg[idle]);
3699 goto out_balanced;
3700 }
3701
Mike Travis7c16ec52008-04-04 18:11:11 -07003702 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 if (!busiest) {
3704 schedstat_inc(sd, lb_nobusyq[idle]);
3705 goto out_balanced;
3706 }
3707
Nick Piggindb935db2005-06-25 14:57:11 -07003708 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
3710 schedstat_add(sd, lb_imbalance[idle], imbalance);
3711
Peter Williams43010652007-08-09 11:16:46 +02003712 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 if (busiest->nr_running > 1) {
3714 /*
3715 * Attempt to move tasks. If find_busiest_group has found
3716 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003717 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 * correctly treated as an imbalance.
3719 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003720 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003721 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003722 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003723 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003724 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003725 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003726
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003727 /*
3728 * some other cpu did the load balance for us.
3729 */
Peter Williams43010652007-08-09 11:16:46 +02003730 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003731 resched_cpu(this_cpu);
3732
Nick Piggin81026792005-06-25 14:57:07 -07003733 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003734 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003735 cpu_clear(cpu_of(busiest), *cpus);
3736 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003737 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003738 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
Nick Piggin81026792005-06-25 14:57:07 -07003741
Peter Williams43010652007-08-09 11:16:46 +02003742 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 schedstat_inc(sd, lb_failed[idle]);
3744 sd->nr_balance_failed++;
3745
3746 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003748 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003749
3750 /* don't kick the migration_thread, if the curr
3751 * task on busiest cpu can't be moved to this_cpu
3752 */
3753 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003754 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003755 all_pinned = 1;
3756 goto out_one_pinned;
3757 }
3758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 if (!busiest->active_balance) {
3760 busiest->active_balance = 1;
3761 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003762 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003764 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003765 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 wake_up_process(busiest->migration_thread);
3767
3768 /*
3769 * We've kicked active balancing, reset the failure
3770 * counter.
3771 */
Nick Piggin39507452005-06-25 14:57:09 -07003772 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 }
Nick Piggin81026792005-06-25 14:57:07 -07003774 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 sd->nr_balance_failed = 0;
3776
Nick Piggin81026792005-06-25 14:57:07 -07003777 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 /* We were unbalanced, so reset the balancing interval */
3779 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003780 } else {
3781 /*
3782 * If we've begun active balancing, start to back off. This
3783 * case may not be covered by the all_pinned logic if there
3784 * is only 1 task on the busy runqueue (because we don't call
3785 * move_tasks).
3786 */
3787 if (sd->balance_interval < sd->max_interval)
3788 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 }
3790
Peter Williams43010652007-08-09 11:16:46 +02003791 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003792 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003793 ld_moved = -1;
3794
3795 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
3797out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 schedstat_inc(sd, lb_balanced[idle]);
3799
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003800 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003801
3802out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003804 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3805 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 sd->balance_interval *= 2;
3807
Ingo Molnar48f24c42006-07-03 00:25:40 -07003808 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003809 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003810 ld_moved = -1;
3811 else
3812 ld_moved = 0;
3813out:
3814 if (unlock_aggregate)
3815 put_aggregate(sd);
3816 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817}
3818
3819/*
3820 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3821 * tasks if there is an imbalance.
3822 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003823 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 * this_rq is locked.
3825 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003826static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003827load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3828 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829{
3830 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003831 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003833 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003834 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003835 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003836
3837 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003838
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003839 /*
3840 * When power savings policy is enabled for the parent domain, idle
3841 * sibling can pick up load irrespective of busy siblings. In this case,
3842 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003843 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003844 */
3845 if (sd->flags & SD_SHARE_CPUPOWER &&
3846 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003847 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Ingo Molnar2d723762007-10-15 17:00:12 +02003849 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003850redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003851 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003852 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003854 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003855 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 }
3857
Mike Travis7c16ec52008-04-04 18:11:11 -07003858 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003859 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003860 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003861 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 }
3863
Nick Piggindb935db2005-06-25 14:57:11 -07003864 BUG_ON(busiest == this_rq);
3865
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003866 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003867
Peter Williams43010652007-08-09 11:16:46 +02003868 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003869 if (busiest->nr_running > 1) {
3870 /* Attempt to move tasks */
3871 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003872 /* this_rq->clock is already updated */
3873 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003874 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003875 imbalance, sd, CPU_NEWLY_IDLE,
3876 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003877 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003878
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003879 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003880 cpu_clear(cpu_of(busiest), *cpus);
3881 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003882 goto redo;
3883 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003884 }
3885
Peter Williams43010652007-08-09 11:16:46 +02003886 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003887 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003888 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3889 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003890 return -1;
3891 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003892 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
Peter Williams43010652007-08-09 11:16:46 +02003894 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003895
3896out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003897 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003898 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003899 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003900 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003901 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003902
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904}
3905
3906/*
3907 * idle_balance is called by schedule() if this_cpu is about to become
3908 * idle. Attempts to pull tasks from other CPUs.
3909 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003910static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911{
3912 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003913 int pulled_task = -1;
3914 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003915 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916
3917 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003918 unsigned long interval;
3919
3920 if (!(sd->flags & SD_LOAD_BALANCE))
3921 continue;
3922
3923 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003924 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003925 pulled_task = load_balance_newidle(this_cpu, this_rq,
3926 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003927
3928 interval = msecs_to_jiffies(sd->balance_interval);
3929 if (time_after(next_balance, sd->last_balance + interval))
3930 next_balance = sd->last_balance + interval;
3931 if (pulled_task)
3932 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003934 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003935 /*
3936 * We are going idle. next_balance may be set based on
3937 * a busy processor. So reset next_balance.
3938 */
3939 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941}
3942
3943/*
3944 * active_load_balance is run by migration threads. It pushes running tasks
3945 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3946 * running on each physical CPU where possible, and avoids physical /
3947 * logical imbalances.
3948 *
3949 * Called with busiest_rq locked.
3950 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003951static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952{
Nick Piggin39507452005-06-25 14:57:09 -07003953 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003954 struct sched_domain *sd;
3955 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003956
Ingo Molnar48f24c42006-07-03 00:25:40 -07003957 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003958 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003959 return;
3960
3961 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
3963 /*
Nick Piggin39507452005-06-25 14:57:09 -07003964 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003965 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003966 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 */
Nick Piggin39507452005-06-25 14:57:09 -07003968 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
Nick Piggin39507452005-06-25 14:57:09 -07003970 /* move a task from busiest_rq to target_rq */
3971 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003972 update_rq_clock(busiest_rq);
3973 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
Nick Piggin39507452005-06-25 14:57:09 -07003975 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003976 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003977 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003978 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003979 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Ingo Molnar48f24c42006-07-03 00:25:40 -07003982 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003983 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
Peter Williams43010652007-08-09 11:16:46 +02003985 if (move_one_task(target_rq, target_cpu, busiest_rq,
3986 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003987 schedstat_inc(sd, alb_pushed);
3988 else
3989 schedstat_inc(sd, alb_failed);
3990 }
Nick Piggin39507452005-06-25 14:57:09 -07003991 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992}
3993
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003994#ifdef CONFIG_NO_HZ
3995static struct {
3996 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003997 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003998} nohz ____cacheline_aligned = {
3999 .load_balancer = ATOMIC_INIT(-1),
4000 .cpu_mask = CPU_MASK_NONE,
4001};
4002
Christoph Lameter7835b982006-12-10 02:20:22 -08004003/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004004 * This routine will try to nominate the ilb (idle load balancing)
4005 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
4006 * load balancing on behalf of all those cpus. If all the cpus in the system
4007 * go into this tickless mode, then there will be no ilb owner (as there is
4008 * no need for one) and all the cpus will sleep till the next wakeup event
4009 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08004010 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004011 * For the ilb owner, tick is not stopped. And this tick will be used
4012 * for idle load balancing. ilb owner will still be part of
4013 * nohz.cpu_mask..
4014 *
4015 * While stopping the tick, this cpu will become the ilb owner if there
4016 * is no other owner. And will be the owner till that cpu becomes busy
4017 * or if all cpus in the system stop their ticks at which point
4018 * there is no need for ilb owner.
4019 *
4020 * When the ilb owner becomes busy, it nominates another owner, during the
4021 * next busy scheduler_tick()
4022 */
4023int select_nohz_load_balancer(int stop_tick)
4024{
4025 int cpu = smp_processor_id();
4026
4027 if (stop_tick) {
4028 cpu_set(cpu, nohz.cpu_mask);
4029 cpu_rq(cpu)->in_nohz_recently = 1;
4030
4031 /*
4032 * If we are going offline and still the leader, give up!
4033 */
4034 if (cpu_is_offline(cpu) &&
4035 atomic_read(&nohz.load_balancer) == cpu) {
4036 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4037 BUG();
4038 return 0;
4039 }
4040
4041 /* time for ilb owner also to sleep */
4042 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4043 if (atomic_read(&nohz.load_balancer) == cpu)
4044 atomic_set(&nohz.load_balancer, -1);
4045 return 0;
4046 }
4047
4048 if (atomic_read(&nohz.load_balancer) == -1) {
4049 /* make me the ilb owner */
4050 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4051 return 1;
4052 } else if (atomic_read(&nohz.load_balancer) == cpu)
4053 return 1;
4054 } else {
4055 if (!cpu_isset(cpu, nohz.cpu_mask))
4056 return 0;
4057
4058 cpu_clear(cpu, nohz.cpu_mask);
4059
4060 if (atomic_read(&nohz.load_balancer) == cpu)
4061 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4062 BUG();
4063 }
4064 return 0;
4065}
4066#endif
4067
4068static DEFINE_SPINLOCK(balancing);
4069
4070/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004071 * It checks each scheduling domain to see if it is due to be balanced,
4072 * and initiates a balancing operation if so.
4073 *
4074 * Balancing parameters are set up in arch_init_sched_domains.
4075 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004076static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08004077{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004078 int balance = 1;
4079 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004080 unsigned long interval;
4081 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004082 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004083 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004084 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07004085 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004087 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 if (!(sd->flags & SD_LOAD_BALANCE))
4089 continue;
4090
4091 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004092 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 interval *= sd->busy_factor;
4094
4095 /* scale ms to jiffies */
4096 interval = msecs_to_jiffies(interval);
4097 if (unlikely(!interval))
4098 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004099 if (interval > HZ*NR_CPUS/10)
4100 interval = HZ*NR_CPUS/10;
4101
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Christoph Lameter08c183f2006-12-10 02:20:29 -08004103 if (sd->flags & SD_SERIALIZE) {
4104 if (!spin_trylock(&balancing))
4105 goto out;
4106 }
4107
Christoph Lameterc9819f42006-12-10 02:20:25 -08004108 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07004109 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004110 /*
4111 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004112 * longer idle, or one of our SMT siblings is
4113 * not idle.
4114 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004115 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004117 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08004119 if (sd->flags & SD_SERIALIZE)
4120 spin_unlock(&balancing);
4121out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004122 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004123 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004124 update_next_balance = 1;
4125 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004126
4127 /*
4128 * Stop the load balance at this level. There is another
4129 * CPU in our sched group which is doing load balancing more
4130 * actively.
4131 */
4132 if (!balance)
4133 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004135
4136 /*
4137 * next_balance will be updated only when there is a need.
4138 * When the cpu is attached to null domain for ex, it will not be
4139 * updated.
4140 */
4141 if (likely(update_next_balance))
4142 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004143}
4144
4145/*
4146 * run_rebalance_domains is triggered when needed from the scheduler tick.
4147 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4148 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4149 */
4150static void run_rebalance_domains(struct softirq_action *h)
4151{
Ingo Molnardd41f592007-07-09 18:51:59 +02004152 int this_cpu = smp_processor_id();
4153 struct rq *this_rq = cpu_rq(this_cpu);
4154 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4155 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004156
Ingo Molnardd41f592007-07-09 18:51:59 +02004157 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004158
4159#ifdef CONFIG_NO_HZ
4160 /*
4161 * If this cpu is the owner for idle load balancing, then do the
4162 * balancing on behalf of the other idle cpus whose ticks are
4163 * stopped.
4164 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004165 if (this_rq->idle_at_tick &&
4166 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004167 cpumask_t cpus = nohz.cpu_mask;
4168 struct rq *rq;
4169 int balance_cpu;
4170
Ingo Molnardd41f592007-07-09 18:51:59 +02004171 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004172 for_each_cpu_mask(balance_cpu, cpus) {
4173 /*
4174 * If this cpu gets work to do, stop the load balancing
4175 * work being done for other cpus. Next load
4176 * balancing owner will pick it up.
4177 */
4178 if (need_resched())
4179 break;
4180
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004181 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004182
4183 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004184 if (time_after(this_rq->next_balance, rq->next_balance))
4185 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004186 }
4187 }
4188#endif
4189}
4190
4191/*
4192 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4193 *
4194 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4195 * idle load balancing owner or decide to stop the periodic load balancing,
4196 * if the whole system is idle.
4197 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004198static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004199{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004200#ifdef CONFIG_NO_HZ
4201 /*
4202 * If we were in the nohz mode recently and busy at the current
4203 * scheduler tick, then check if we need to nominate new idle
4204 * load balancer.
4205 */
4206 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4207 rq->in_nohz_recently = 0;
4208
4209 if (atomic_read(&nohz.load_balancer) == cpu) {
4210 cpu_clear(cpu, nohz.cpu_mask);
4211 atomic_set(&nohz.load_balancer, -1);
4212 }
4213
4214 if (atomic_read(&nohz.load_balancer) == -1) {
4215 /*
4216 * simple selection for now: Nominate the
4217 * first cpu in the nohz list to be the next
4218 * ilb owner.
4219 *
4220 * TBD: Traverse the sched domains and nominate
4221 * the nearest cpu in the nohz.cpu_mask.
4222 */
4223 int ilb = first_cpu(nohz.cpu_mask);
4224
Mike Travis434d53b2008-04-04 18:11:04 -07004225 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004226 resched_cpu(ilb);
4227 }
4228 }
4229
4230 /*
4231 * If this cpu is idle and doing idle load balancing for all the
4232 * cpus with ticks stopped, is it time for that to stop?
4233 */
4234 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4235 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4236 resched_cpu(cpu);
4237 return;
4238 }
4239
4240 /*
4241 * If this cpu is idle and the idle load balancing is done by
4242 * someone else, then no need raise the SCHED_SOFTIRQ
4243 */
4244 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4245 cpu_isset(cpu, nohz.cpu_mask))
4246 return;
4247#endif
4248 if (time_after_eq(jiffies, rq->next_balance))
4249 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250}
Ingo Molnardd41f592007-07-09 18:51:59 +02004251
4252#else /* CONFIG_SMP */
4253
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254/*
4255 * on UP we do not need to balance between CPUs:
4256 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004257static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
4259}
Ingo Molnardd41f592007-07-09 18:51:59 +02004260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261#endif
4262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263DEFINE_PER_CPU(struct kernel_stat, kstat);
4264
4265EXPORT_PER_CPU_SYMBOL(kstat);
4266
4267/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004268 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4269 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004271unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004274 u64 ns, delta_exec;
4275 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004276
Ingo Molnar41b86e92007-07-09 18:51:58 +02004277 rq = task_rq_lock(p, &flags);
4278 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004279 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004280 update_rq_clock(rq);
4281 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004282 if ((s64)delta_exec > 0)
4283 ns += delta_exec;
4284 }
4285 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return ns;
4288}
4289
4290/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 * Account user cpu time to a process.
4292 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 * @cputime: the cpu time spent in user space since the last update
4294 */
4295void account_user_time(struct task_struct *p, cputime_t cputime)
4296{
4297 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4298 cputime64_t tmp;
4299
4300 p->utime = cputime_add(p->utime, cputime);
4301
4302 /* Add user time to cpustat. */
4303 tmp = cputime_to_cputime64(cputime);
4304 if (TASK_NICE(p) > 0)
4305 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4306 else
4307 cpustat->user = cputime64_add(cpustat->user, tmp);
4308}
4309
4310/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004311 * Account guest cpu time to a process.
4312 * @p: the process that the cpu time gets accounted to
4313 * @cputime: the cpu time spent in virtual machine since the last update
4314 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004315static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004316{
4317 cputime64_t tmp;
4318 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4319
4320 tmp = cputime_to_cputime64(cputime);
4321
4322 p->utime = cputime_add(p->utime, cputime);
4323 p->gtime = cputime_add(p->gtime, cputime);
4324
4325 cpustat->user = cputime64_add(cpustat->user, tmp);
4326 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4327}
4328
4329/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004330 * Account scaled user cpu time to a process.
4331 * @p: the process that the cpu time gets accounted to
4332 * @cputime: the cpu time spent in user space since the last update
4333 */
4334void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4335{
4336 p->utimescaled = cputime_add(p->utimescaled, cputime);
4337}
4338
4339/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 * Account system cpu time to a process.
4341 * @p: the process that the cpu time gets accounted to
4342 * @hardirq_offset: the offset to subtract from hardirq_count()
4343 * @cputime: the cpu time spent in kernel space since the last update
4344 */
4345void account_system_time(struct task_struct *p, int hardirq_offset,
4346 cputime_t cputime)
4347{
4348 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004349 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 cputime64_t tmp;
4351
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004352 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4353 account_guest_time(p, cputime);
4354 return;
4355 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004356
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 p->stime = cputime_add(p->stime, cputime);
4358
4359 /* Add system time to cpustat. */
4360 tmp = cputime_to_cputime64(cputime);
4361 if (hardirq_count() - hardirq_offset)
4362 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4363 else if (softirq_count())
4364 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004365 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004367 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4369 else
4370 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4371 /* Account for system time used */
4372 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373}
4374
4375/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004376 * Account scaled system cpu time to a process.
4377 * @p: the process that the cpu time gets accounted to
4378 * @hardirq_offset: the offset to subtract from hardirq_count()
4379 * @cputime: the cpu time spent in kernel space since the last update
4380 */
4381void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4382{
4383 p->stimescaled = cputime_add(p->stimescaled, cputime);
4384}
4385
4386/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 * Account for involuntary wait time.
4388 * @p: the process from which the cpu time has been stolen
4389 * @steal: the cpu time spent in involuntary wait
4390 */
4391void account_steal_time(struct task_struct *p, cputime_t steal)
4392{
4393 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4394 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004395 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
4397 if (p == rq->idle) {
4398 p->stime = cputime_add(p->stime, steal);
4399 if (atomic_read(&rq->nr_iowait) > 0)
4400 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4401 else
4402 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004403 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4405}
4406
Christoph Lameter7835b982006-12-10 02:20:22 -08004407/*
4408 * This function gets called by the timer code, with HZ frequency.
4409 * We call it with interrupts disabled.
4410 *
4411 * It also gets called by the fork code, when changing the parent's
4412 * timeslices.
4413 */
4414void scheduler_tick(void)
4415{
Christoph Lameter7835b982006-12-10 02:20:22 -08004416 int cpu = smp_processor_id();
4417 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004418 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004419
4420 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004421
Ingo Molnardd41f592007-07-09 18:51:59 +02004422 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004423 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004424 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004425 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004426 spin_unlock(&rq->lock);
4427
Christoph Lametere418e1c2006-12-10 02:20:23 -08004428#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004429 rq->idle_at_tick = idle_cpu(cpu);
4430 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004431#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432}
4433
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004434#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
4435 defined(CONFIG_PREEMPT_TRACER))
4436
4437static inline unsigned long get_parent_ip(unsigned long addr)
4438{
4439 if (in_lock_functions(addr)) {
4440 addr = CALLER_ADDR2;
4441 if (in_lock_functions(addr))
4442 addr = CALLER_ADDR3;
4443 }
4444 return addr;
4445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Srinivasa Ds43627582008-02-23 15:24:04 -08004447void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004449#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 /*
4451 * Underflow?
4452 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004453 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4454 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004455#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004457#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 /*
4459 * Spinlock count overflowing soon?
4460 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004461 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4462 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004463#endif
4464 if (preempt_count() == val)
4465 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466}
4467EXPORT_SYMBOL(add_preempt_count);
4468
Srinivasa Ds43627582008-02-23 15:24:04 -08004469void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004471#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 /*
4473 * Underflow?
4474 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004475 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4476 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 /*
4478 * Is the spinlock portion underflowing?
4479 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004480 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4481 !(preempt_count() & PREEMPT_MASK)))
4482 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004483#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004484
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004485 if (preempt_count() == val)
4486 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 preempt_count() -= val;
4488}
4489EXPORT_SYMBOL(sub_preempt_count);
4490
4491#endif
4492
4493/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004494 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004496static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497{
Satyam Sharma838225b2007-10-24 18:23:50 +02004498 struct pt_regs *regs = get_irq_regs();
4499
4500 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4501 prev->comm, prev->pid, preempt_count());
4502
Ingo Molnardd41f592007-07-09 18:51:59 +02004503 debug_show_held_locks(prev);
4504 if (irqs_disabled())
4505 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004506
4507 if (regs)
4508 show_regs(regs);
4509 else
4510 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004511}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Ingo Molnardd41f592007-07-09 18:51:59 +02004513/*
4514 * Various schedule()-time debugging checks and statistics:
4515 */
4516static inline void schedule_debug(struct task_struct *prev)
4517{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004519 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 * schedule() atomically, we ignore that path for now.
4521 * Otherwise, whine if we are scheduling when we should not be.
4522 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004523 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4524 __schedule_bug(prev);
4525
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4527
Ingo Molnar2d723762007-10-15 17:00:12 +02004528 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004529#ifdef CONFIG_SCHEDSTATS
4530 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004531 schedstat_inc(this_rq(), bkl_count);
4532 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004533 }
4534#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004535}
4536
4537/*
4538 * Pick up the highest-prio task:
4539 */
4540static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004541pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004542{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004543 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004544 struct task_struct *p;
4545
4546 /*
4547 * Optimization: we know that if all tasks are in
4548 * the fair class we can call that function directly:
4549 */
4550 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004551 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004552 if (likely(p))
4553 return p;
4554 }
4555
4556 class = sched_class_highest;
4557 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004558 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004559 if (p)
4560 return p;
4561 /*
4562 * Will never be NULL as the idle class always
4563 * returns a non-NULL p:
4564 */
4565 class = class->next;
4566 }
4567}
4568
4569/*
4570 * schedule() is the main scheduler function.
4571 */
4572asmlinkage void __sched schedule(void)
4573{
4574 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004575 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004576 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004577 int cpu;
4578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579need_resched:
4580 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004581 cpu = smp_processor_id();
4582 rq = cpu_rq(cpu);
4583 rcu_qsctr_inc(cpu);
4584 prev = rq->curr;
4585 switch_count = &prev->nivcsw;
4586
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 release_kernel_lock(prev);
4588need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Ingo Molnardd41f592007-07-09 18:51:59 +02004590 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004592 hrtick_clear(rq);
4593
Ingo Molnar1e819952007-10-15 17:00:13 +02004594 /*
4595 * Do the rq-clock update outside the rq lock:
4596 */
4597 local_irq_disable();
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004598 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004599 spin_lock(&rq->lock);
4600 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Ingo Molnardd41f592007-07-09 18:51:59 +02004602 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4603 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004604 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004605 prev->state = TASK_RUNNING;
4606 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004607 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004608 }
4609 switch_count = &prev->nvcsw;
4610 }
4611
Steven Rostedt9a897c52008-01-25 21:08:22 +01004612#ifdef CONFIG_SMP
4613 if (prev->sched_class->pre_schedule)
4614 prev->sched_class->pre_schedule(rq, prev);
4615#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004616
Ingo Molnardd41f592007-07-09 18:51:59 +02004617 if (unlikely(!rq->nr_running))
4618 idle_balance(cpu, rq);
4619
Ingo Molnar31ee5292007-08-09 11:16:49 +02004620 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004621 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01004624 sched_info_switch(prev, next);
4625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 rq->nr_switches++;
4627 rq->curr = next;
4628 ++*switch_count;
4629
Ingo Molnardd41f592007-07-09 18:51:59 +02004630 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004631 /*
4632 * the context switch might have flipped the stack from under
4633 * us, hence refresh the local variables.
4634 */
4635 cpu = smp_processor_id();
4636 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 } else
4638 spin_unlock_irq(&rq->lock);
4639
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004640 hrtick_set(rq);
4641
4642 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004644
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 preempt_enable_no_resched();
4646 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4647 goto need_resched;
4648}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649EXPORT_SYMBOL(schedule);
4650
4651#ifdef CONFIG_PREEMPT
4652/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004653 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004654 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 * occur there and call schedule directly.
4656 */
4657asmlinkage void __sched preempt_schedule(void)
4658{
4659 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004660
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 /*
4662 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004663 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004665 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 return;
4667
Andi Kleen3a5c3592007-10-15 17:00:14 +02004668 do {
4669 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004670 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004671 sub_preempt_count(PREEMPT_ACTIVE);
4672
4673 /*
4674 * Check again in case we missed a preemption opportunity
4675 * between schedule and now.
4676 */
4677 barrier();
4678 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680EXPORT_SYMBOL(preempt_schedule);
4681
4682/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004683 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 * off of irq context.
4685 * Note, that this is called and return with irqs disabled. This will
4686 * protect us against recursive calling from irq.
4687 */
4688asmlinkage void __sched preempt_schedule_irq(void)
4689{
4690 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004691
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004692 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 BUG_ON(ti->preempt_count || !irqs_disabled());
4694
Andi Kleen3a5c3592007-10-15 17:00:14 +02004695 do {
4696 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004697 local_irq_enable();
4698 schedule();
4699 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004700 sub_preempt_count(PREEMPT_ACTIVE);
4701
4702 /*
4703 * Check again in case we missed a preemption opportunity
4704 * between schedule and now.
4705 */
4706 barrier();
4707 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708}
4709
4710#endif /* CONFIG_PREEMPT */
4711
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004712int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4713 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004715 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717EXPORT_SYMBOL(default_wake_function);
4718
4719/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004720 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4721 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 * number) then we wake all the non-exclusive tasks and one exclusive task.
4723 *
4724 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004725 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4727 */
4728static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4729 int nr_exclusive, int sync, void *key)
4730{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004731 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004733 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004734 unsigned flags = curr->flags;
4735
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004737 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 break;
4739 }
4740}
4741
4742/**
4743 * __wake_up - wake up threads blocked on a waitqueue.
4744 * @q: the waitqueue
4745 * @mode: which threads
4746 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004747 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004749void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004750 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751{
4752 unsigned long flags;
4753
4754 spin_lock_irqsave(&q->lock, flags);
4755 __wake_up_common(q, mode, nr_exclusive, 0, key);
4756 spin_unlock_irqrestore(&q->lock, flags);
4757}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758EXPORT_SYMBOL(__wake_up);
4759
4760/*
4761 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4762 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004763void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764{
4765 __wake_up_common(q, mode, 1, 0, NULL);
4766}
4767
4768/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004769 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 * @q: the waitqueue
4771 * @mode: which threads
4772 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4773 *
4774 * The sync wakeup differs that the waker knows that it will schedule
4775 * away soon, so while the target thread will be woken up, it will not
4776 * be migrated to another CPU - ie. the two threads are 'synchronized'
4777 * with each other. This can prevent needless bouncing between CPUs.
4778 *
4779 * On UP it can prevent extra preemption.
4780 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004781void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004782__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
4784 unsigned long flags;
4785 int sync = 1;
4786
4787 if (unlikely(!q))
4788 return;
4789
4790 if (unlikely(!nr_exclusive))
4791 sync = 0;
4792
4793 spin_lock_irqsave(&q->lock, flags);
4794 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4795 spin_unlock_irqrestore(&q->lock, flags);
4796}
4797EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4798
Ingo Molnarb15136e2007-10-24 18:23:48 +02004799void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
4801 unsigned long flags;
4802
4803 spin_lock_irqsave(&x->wait.lock, flags);
4804 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004805 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 spin_unlock_irqrestore(&x->wait.lock, flags);
4807}
4808EXPORT_SYMBOL(complete);
4809
Ingo Molnarb15136e2007-10-24 18:23:48 +02004810void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
4812 unsigned long flags;
4813
4814 spin_lock_irqsave(&x->wait.lock, flags);
4815 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004816 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 spin_unlock_irqrestore(&x->wait.lock, flags);
4818}
4819EXPORT_SYMBOL(complete_all);
4820
Andi Kleen8cbbe862007-10-15 17:00:14 +02004821static inline long __sched
4822do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 if (!x->done) {
4825 DECLARE_WAITQUEUE(wait, current);
4826
4827 wait.flags |= WQ_FLAG_EXCLUSIVE;
4828 __add_wait_queue_tail(&x->wait, &wait);
4829 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004830 if ((state == TASK_INTERRUPTIBLE &&
4831 signal_pending(current)) ||
4832 (state == TASK_KILLABLE &&
4833 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004834 __remove_wait_queue(&x->wait, &wait);
4835 return -ERESTARTSYS;
4836 }
4837 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004839 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004841 if (!timeout) {
4842 __remove_wait_queue(&x->wait, &wait);
4843 return timeout;
4844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 } while (!x->done);
4846 __remove_wait_queue(&x->wait, &wait);
4847 }
4848 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004849 return timeout;
4850}
4851
4852static long __sched
4853wait_for_common(struct completion *x, long timeout, int state)
4854{
4855 might_sleep();
4856
4857 spin_lock_irq(&x->wait.lock);
4858 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004860 return timeout;
4861}
4862
Ingo Molnarb15136e2007-10-24 18:23:48 +02004863void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004864{
4865 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866}
4867EXPORT_SYMBOL(wait_for_completion);
4868
Ingo Molnarb15136e2007-10-24 18:23:48 +02004869unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4871{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004872 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873}
4874EXPORT_SYMBOL(wait_for_completion_timeout);
4875
Andi Kleen8cbbe862007-10-15 17:00:14 +02004876int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
Andi Kleen51e97992007-10-18 21:32:55 +02004878 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4879 if (t == -ERESTARTSYS)
4880 return t;
4881 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882}
4883EXPORT_SYMBOL(wait_for_completion_interruptible);
4884
Ingo Molnarb15136e2007-10-24 18:23:48 +02004885unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886wait_for_completion_interruptible_timeout(struct completion *x,
4887 unsigned long timeout)
4888{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004889 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890}
4891EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4892
Matthew Wilcox009e5772007-12-06 12:29:54 -05004893int __sched wait_for_completion_killable(struct completion *x)
4894{
4895 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4896 if (t == -ERESTARTSYS)
4897 return t;
4898 return 0;
4899}
4900EXPORT_SYMBOL(wait_for_completion_killable);
4901
Andi Kleen8cbbe862007-10-15 17:00:14 +02004902static long __sched
4903sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004904{
4905 unsigned long flags;
4906 wait_queue_t wait;
4907
4908 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
Andi Kleen8cbbe862007-10-15 17:00:14 +02004910 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
Andi Kleen8cbbe862007-10-15 17:00:14 +02004912 spin_lock_irqsave(&q->lock, flags);
4913 __add_wait_queue(q, &wait);
4914 spin_unlock(&q->lock);
4915 timeout = schedule_timeout(timeout);
4916 spin_lock_irq(&q->lock);
4917 __remove_wait_queue(q, &wait);
4918 spin_unlock_irqrestore(&q->lock, flags);
4919
4920 return timeout;
4921}
4922
4923void __sched interruptible_sleep_on(wait_queue_head_t *q)
4924{
4925 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927EXPORT_SYMBOL(interruptible_sleep_on);
4928
Ingo Molnar0fec1712007-07-09 18:52:01 +02004929long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004930interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004932 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4935
Ingo Molnar0fec1712007-07-09 18:52:01 +02004936void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004938 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940EXPORT_SYMBOL(sleep_on);
4941
Ingo Molnar0fec1712007-07-09 18:52:01 +02004942long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004944 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946EXPORT_SYMBOL(sleep_on_timeout);
4947
Ingo Molnarb29739f2006-06-27 02:54:51 -07004948#ifdef CONFIG_RT_MUTEXES
4949
4950/*
4951 * rt_mutex_setprio - set the current priority of a task
4952 * @p: task
4953 * @prio: prio value (kernel-internal form)
4954 *
4955 * This function changes the 'effective' priority of a task. It does
4956 * not touch ->normal_prio like __setscheduler().
4957 *
4958 * Used by the rt_mutex code to implement priority inheritance logic.
4959 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004960void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004961{
4962 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004963 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004964 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004965 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004966
4967 BUG_ON(prio < 0 || prio > MAX_PRIO);
4968
4969 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004970 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004971
Andrew Mortond5f9f942007-05-08 20:27:06 -07004972 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004973 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004974 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004975 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004976 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004977 if (running)
4978 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004979
4980 if (rt_prio(prio))
4981 p->sched_class = &rt_sched_class;
4982 else
4983 p->sched_class = &fair_sched_class;
4984
Ingo Molnarb29739f2006-06-27 02:54:51 -07004985 p->prio = prio;
4986
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004987 if (running)
4988 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004989 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004990 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004991
4992 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004993 }
4994 task_rq_unlock(rq, &flags);
4995}
4996
4997#endif
4998
Ingo Molnar36c8b582006-07-03 00:25:41 -07004999void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000{
Ingo Molnardd41f592007-07-09 18:51:59 +02005001 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005003 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
5005 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5006 return;
5007 /*
5008 * We have to be careful, if called from sys_setpriority(),
5009 * the task might be in the middle of scheduling on another CPU.
5010 */
5011 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005012 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 /*
5014 * The RT priorities are set via sched_setscheduler(), but we still
5015 * allow the 'normal' nice value to be set - but as expected
5016 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02005017 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 */
Ingo Molnare05606d2007-07-09 18:51:59 +02005019 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 p->static_prio = NICE_TO_PRIO(nice);
5021 goto out_unlock;
5022 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005023 on_rq = p->se.on_rq;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02005024 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005025 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07005028 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005029 old_prio = p->prio;
5030 p->prio = effective_prio(p);
5031 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
Ingo Molnardd41f592007-07-09 18:51:59 +02005033 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005034 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07005036 * If the task increased its priority or is running and
5037 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005039 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 resched_task(rq->curr);
5041 }
5042out_unlock:
5043 task_rq_unlock(rq, &flags);
5044}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045EXPORT_SYMBOL(set_user_nice);
5046
Matt Mackalle43379f2005-05-01 08:59:00 -07005047/*
5048 * can_nice - check if a task can reduce its nice value
5049 * @p: task
5050 * @nice: nice value
5051 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005052int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005053{
Matt Mackall024f4742005-08-18 11:24:19 -07005054 /* convert nice value [19,-20] to rlimit style value [1,40] */
5055 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005056
Matt Mackalle43379f2005-05-01 08:59:00 -07005057 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5058 capable(CAP_SYS_NICE));
5059}
5060
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061#ifdef __ARCH_WANT_SYS_NICE
5062
5063/*
5064 * sys_nice - change the priority of the current process.
5065 * @increment: priority increment
5066 *
5067 * sys_setpriority is a more generic, but much slower function that
5068 * does similar things.
5069 */
5070asmlinkage long sys_nice(int increment)
5071{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005072 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
5074 /*
5075 * Setpriority might change our priority at the same moment.
5076 * We don't have to worry. Conceptually one call occurs first
5077 * and we have a single winner.
5078 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005079 if (increment < -40)
5080 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 if (increment > 40)
5082 increment = 40;
5083
5084 nice = PRIO_TO_NICE(current->static_prio) + increment;
5085 if (nice < -20)
5086 nice = -20;
5087 if (nice > 19)
5088 nice = 19;
5089
Matt Mackalle43379f2005-05-01 08:59:00 -07005090 if (increment < 0 && !can_nice(current, nice))
5091 return -EPERM;
5092
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 retval = security_task_setnice(current, nice);
5094 if (retval)
5095 return retval;
5096
5097 set_user_nice(current, nice);
5098 return 0;
5099}
5100
5101#endif
5102
5103/**
5104 * task_prio - return the priority value of a given task.
5105 * @p: the task in question.
5106 *
5107 * This is the priority value as seen by users in /proc.
5108 * RT tasks are offset by -200. Normal tasks are centered
5109 * around 0, value goes from -16 to +15.
5110 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005111int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112{
5113 return p->prio - MAX_RT_PRIO;
5114}
5115
5116/**
5117 * task_nice - return the nice value of a given task.
5118 * @p: the task in question.
5119 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005120int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121{
5122 return TASK_NICE(p);
5123}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005124EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
5126/**
5127 * idle_cpu - is a given cpu idle currently?
5128 * @cpu: the processor in question.
5129 */
5130int idle_cpu(int cpu)
5131{
5132 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5133}
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135/**
5136 * idle_task - return the idle task for a given cpu.
5137 * @cpu: the processor in question.
5138 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005139struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140{
5141 return cpu_rq(cpu)->idle;
5142}
5143
5144/**
5145 * find_process_by_pid - find a process with a matching PID value.
5146 * @pid: the pid in question.
5147 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005148static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005150 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151}
5152
5153/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005154static void
5155__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156{
Ingo Molnardd41f592007-07-09 18:51:59 +02005157 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005158
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005160 switch (p->policy) {
5161 case SCHED_NORMAL:
5162 case SCHED_BATCH:
5163 case SCHED_IDLE:
5164 p->sched_class = &fair_sched_class;
5165 break;
5166 case SCHED_FIFO:
5167 case SCHED_RR:
5168 p->sched_class = &rt_sched_class;
5169 break;
5170 }
5171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005173 p->normal_prio = normal_prio(p);
5174 /* we are holding p->pi_lock already */
5175 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005176 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177}
5178
5179/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005180 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 * @p: the task in question.
5182 * @policy: new policy.
5183 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005184 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005185 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005187int sched_setscheduler(struct task_struct *p, int policy,
5188 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005190 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005192 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005193 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
Steven Rostedt66e53932006-06-27 02:54:44 -07005195 /* may grab non-irq protected spin_locks */
5196 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197recheck:
5198 /* double check policy once rq lock held */
5199 if (policy < 0)
5200 policy = oldpolicy = p->policy;
5201 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005202 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5203 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005204 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 /*
5206 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005207 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5208 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 */
5210 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005211 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005212 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005214 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 return -EINVAL;
5216
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005217 /*
5218 * Allow unprivileged RT tasks to decrease priority:
5219 */
5220 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005221 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005222 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005223
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005224 if (!lock_task_sighand(p, &flags))
5225 return -ESRCH;
5226 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5227 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005228
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005229 /* can't set/change the rt policy */
5230 if (policy != p->policy && !rlim_rtprio)
5231 return -EPERM;
5232
5233 /* can't increase priority */
5234 if (param->sched_priority > p->rt_priority &&
5235 param->sched_priority > rlim_rtprio)
5236 return -EPERM;
5237 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005238 /*
5239 * Like positive nice levels, dont allow tasks to
5240 * move out of SCHED_IDLE either:
5241 */
5242 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5243 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005244
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005245 /* can't change other user's priorities */
5246 if ((current->euid != p->euid) &&
5247 (current->euid != p->uid))
5248 return -EPERM;
5249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005251#ifdef CONFIG_RT_GROUP_SCHED
5252 /*
5253 * Do not allow realtime tasks into groups that have no runtime
5254 * assigned.
5255 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02005256 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005257 return -EPERM;
5258#endif
5259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 retval = security_task_setscheduler(p, policy, param);
5261 if (retval)
5262 return retval;
5263 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005264 * make sure no PI-waiters arrive (or leave) while we are
5265 * changing the priority of the task:
5266 */
5267 spin_lock_irqsave(&p->pi_lock, flags);
5268 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 * To be able to change p->policy safely, the apropriate
5270 * runqueue lock must be held.
5271 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005272 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 /* recheck policy now with rq lock held */
5274 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5275 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005276 __task_rq_unlock(rq);
5277 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 goto recheck;
5279 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005280 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005281 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005282 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005283 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005284 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005285 if (running)
5286 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005287
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005289 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005290
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005291 if (running)
5292 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005293 if (on_rq) {
5294 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005295
5296 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005298 __task_rq_unlock(rq);
5299 spin_unlock_irqrestore(&p->pi_lock, flags);
5300
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005301 rt_mutex_adjust_pi(p);
5302
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 return 0;
5304}
5305EXPORT_SYMBOL_GPL(sched_setscheduler);
5306
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005307static int
5308do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 struct sched_param lparam;
5311 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005312 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313
5314 if (!param || pid < 0)
5315 return -EINVAL;
5316 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5317 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005318
5319 rcu_read_lock();
5320 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005322 if (p != NULL)
5323 retval = sched_setscheduler(p, policy, &lparam);
5324 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005325
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 return retval;
5327}
5328
5329/**
5330 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5331 * @pid: the pid in question.
5332 * @policy: new policy.
5333 * @param: structure containing the new RT priority.
5334 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005335asmlinkage long
5336sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337{
Jason Baronc21761f2006-01-18 17:43:03 -08005338 /* negative values for policy are not valid */
5339 if (policy < 0)
5340 return -EINVAL;
5341
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 return do_sched_setscheduler(pid, policy, param);
5343}
5344
5345/**
5346 * sys_sched_setparam - set/change the RT priority of a thread
5347 * @pid: the pid in question.
5348 * @param: structure containing the new RT priority.
5349 */
5350asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5351{
5352 return do_sched_setscheduler(pid, -1, param);
5353}
5354
5355/**
5356 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5357 * @pid: the pid in question.
5358 */
5359asmlinkage long sys_sched_getscheduler(pid_t pid)
5360{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005361 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005362 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
5364 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005365 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
5367 retval = -ESRCH;
5368 read_lock(&tasklist_lock);
5369 p = find_process_by_pid(pid);
5370 if (p) {
5371 retval = security_task_getscheduler(p);
5372 if (!retval)
5373 retval = p->policy;
5374 }
5375 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 return retval;
5377}
5378
5379/**
5380 * sys_sched_getscheduler - get the RT priority of a thread
5381 * @pid: the pid in question.
5382 * @param: structure containing the RT priority.
5383 */
5384asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5385{
5386 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005387 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005388 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
5390 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005391 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
5393 read_lock(&tasklist_lock);
5394 p = find_process_by_pid(pid);
5395 retval = -ESRCH;
5396 if (!p)
5397 goto out_unlock;
5398
5399 retval = security_task_getscheduler(p);
5400 if (retval)
5401 goto out_unlock;
5402
5403 lp.sched_priority = p->rt_priority;
5404 read_unlock(&tasklist_lock);
5405
5406 /*
5407 * This one might sleep, we cannot do it with a spinlock held ...
5408 */
5409 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 return retval;
5412
5413out_unlock:
5414 read_unlock(&tasklist_lock);
5415 return retval;
5416}
5417
Mike Travisb53e9212008-04-04 18:11:08 -07005418long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005421 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005422 struct task_struct *p;
5423 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005425 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 read_lock(&tasklist_lock);
5427
5428 p = find_process_by_pid(pid);
5429 if (!p) {
5430 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005431 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 return -ESRCH;
5433 }
5434
5435 /*
5436 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005437 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 * usage count and then drop tasklist_lock.
5439 */
5440 get_task_struct(p);
5441 read_unlock(&tasklist_lock);
5442
5443 retval = -EPERM;
5444 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5445 !capable(CAP_SYS_NICE))
5446 goto out_unlock;
5447
David Quigleye7834f82006-06-23 02:03:59 -07005448 retval = security_task_setscheduler(p, 0, NULL);
5449 if (retval)
5450 goto out_unlock;
5451
Mike Travisf9a86fc2008-04-04 18:11:07 -07005452 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005454 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005455 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456
Paul Menage8707d8b2007-10-18 23:40:22 -07005457 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005458 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005459 if (!cpus_subset(new_mask, cpus_allowed)) {
5460 /*
5461 * We must have raced with a concurrent cpuset
5462 * update. Just reset the cpus_allowed to the
5463 * cpuset's cpus_allowed
5464 */
5465 new_mask = cpus_allowed;
5466 goto again;
5467 }
5468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469out_unlock:
5470 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005471 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 return retval;
5473}
5474
5475static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5476 cpumask_t *new_mask)
5477{
5478 if (len < sizeof(cpumask_t)) {
5479 memset(new_mask, 0, sizeof(cpumask_t));
5480 } else if (len > sizeof(cpumask_t)) {
5481 len = sizeof(cpumask_t);
5482 }
5483 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5484}
5485
5486/**
5487 * sys_sched_setaffinity - set the cpu affinity of a process
5488 * @pid: pid of the process
5489 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5490 * @user_mask_ptr: user-space pointer to the new cpu mask
5491 */
5492asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5493 unsigned long __user *user_mask_ptr)
5494{
5495 cpumask_t new_mask;
5496 int retval;
5497
5498 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5499 if (retval)
5500 return retval;
5501
Mike Travisb53e9212008-04-04 18:11:08 -07005502 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503}
5504
5505/*
5506 * Represents all cpu's present in the system
5507 * In systems capable of hotplug, this map could dynamically grow
5508 * as new cpu's are detected in the system via any platform specific
5509 * method, such as ACPI for e.g.
5510 */
5511
Andi Kleen4cef0c62006-01-11 22:44:57 +01005512cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513EXPORT_SYMBOL(cpu_present_map);
5514
5515#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005516cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005517EXPORT_SYMBOL(cpu_online_map);
5518
Andi Kleen4cef0c62006-01-11 22:44:57 +01005519cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005520EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521#endif
5522
5523long sched_getaffinity(pid_t pid, cpumask_t *mask)
5524{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005525 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005528 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 read_lock(&tasklist_lock);
5530
5531 retval = -ESRCH;
5532 p = find_process_by_pid(pid);
5533 if (!p)
5534 goto out_unlock;
5535
David Quigleye7834f82006-06-23 02:03:59 -07005536 retval = security_task_getscheduler(p);
5537 if (retval)
5538 goto out_unlock;
5539
Jack Steiner2f7016d2006-02-01 03:05:18 -08005540 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541
5542out_unlock:
5543 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005544 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545
Ulrich Drepper9531b622007-08-09 11:16:46 +02005546 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547}
5548
5549/**
5550 * sys_sched_getaffinity - get the cpu affinity of a process
5551 * @pid: pid of the process
5552 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5553 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5554 */
5555asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5556 unsigned long __user *user_mask_ptr)
5557{
5558 int ret;
5559 cpumask_t mask;
5560
5561 if (len < sizeof(cpumask_t))
5562 return -EINVAL;
5563
5564 ret = sched_getaffinity(pid, &mask);
5565 if (ret < 0)
5566 return ret;
5567
5568 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5569 return -EFAULT;
5570
5571 return sizeof(cpumask_t);
5572}
5573
5574/**
5575 * sys_sched_yield - yield the current processor to other threads.
5576 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005577 * This function yields the current CPU to other tasks. If there are no
5578 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 */
5580asmlinkage long sys_sched_yield(void)
5581{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005582 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583
Ingo Molnar2d723762007-10-15 17:00:12 +02005584 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005585 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
5587 /*
5588 * Since we are going to call schedule() anyway, there's
5589 * no need to preempt or enable interrupts:
5590 */
5591 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005592 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 _raw_spin_unlock(&rq->lock);
5594 preempt_enable_no_resched();
5595
5596 schedule();
5597
5598 return 0;
5599}
5600
Andrew Mortone7b38402006-06-30 01:56:00 -07005601static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005603#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5604 __might_sleep(__FILE__, __LINE__);
5605#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005606 /*
5607 * The BKS might be reacquired before we have dropped
5608 * PREEMPT_ACTIVE, which could trigger a second
5609 * cond_resched() call.
5610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 do {
5612 add_preempt_count(PREEMPT_ACTIVE);
5613 schedule();
5614 sub_preempt_count(PREEMPT_ACTIVE);
5615 } while (need_resched());
5616}
5617
Herbert Xu02b67cc32008-01-25 21:08:28 +01005618int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619{
Ingo Molnar94142322006-12-29 16:48:13 -08005620 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5621 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 __cond_resched();
5623 return 1;
5624 }
5625 return 0;
5626}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005627EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
5629/*
5630 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5631 * call schedule, and on return reacquire the lock.
5632 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005633 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 * operations here to prevent schedule() from being called twice (once via
5635 * spin_unlock(), once by hand).
5636 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005637int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638{
Nick Piggin95c354f2008-01-30 13:31:20 +01005639 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005640 int ret = 0;
5641
Nick Piggin95c354f2008-01-30 13:31:20 +01005642 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005644 if (resched && need_resched())
5645 __cond_resched();
5646 else
5647 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005648 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005651 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653EXPORT_SYMBOL(cond_resched_lock);
5654
5655int __sched cond_resched_softirq(void)
5656{
5657 BUG_ON(!in_softirq());
5658
Ingo Molnar94142322006-12-29 16:48:13 -08005659 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005660 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 __cond_resched();
5662 local_bh_disable();
5663 return 1;
5664 }
5665 return 0;
5666}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667EXPORT_SYMBOL(cond_resched_softirq);
5668
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669/**
5670 * yield - yield the current processor to other threads.
5671 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005672 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 * thread runnable and calls sys_sched_yield().
5674 */
5675void __sched yield(void)
5676{
5677 set_current_state(TASK_RUNNING);
5678 sys_sched_yield();
5679}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680EXPORT_SYMBOL(yield);
5681
5682/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005683 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 * that process accounting knows that this is a task in IO wait state.
5685 *
5686 * But don't do that if it is a deliberate, throttling IO wait (this task
5687 * has set its backing_dev_info: the queue against which it should throttle)
5688 */
5689void __sched io_schedule(void)
5690{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005691 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005693 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 atomic_inc(&rq->nr_iowait);
5695 schedule();
5696 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005697 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699EXPORT_SYMBOL(io_schedule);
5700
5701long __sched io_schedule_timeout(long timeout)
5702{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005703 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 long ret;
5705
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005706 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 atomic_inc(&rq->nr_iowait);
5708 ret = schedule_timeout(timeout);
5709 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005710 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 return ret;
5712}
5713
5714/**
5715 * sys_sched_get_priority_max - return maximum RT priority.
5716 * @policy: scheduling class.
5717 *
5718 * this syscall returns the maximum rt_priority that can be used
5719 * by a given scheduling class.
5720 */
5721asmlinkage long sys_sched_get_priority_max(int policy)
5722{
5723 int ret = -EINVAL;
5724
5725 switch (policy) {
5726 case SCHED_FIFO:
5727 case SCHED_RR:
5728 ret = MAX_USER_RT_PRIO-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 break;
5735 }
5736 return ret;
5737}
5738
5739/**
5740 * sys_sched_get_priority_min - return minimum RT priority.
5741 * @policy: scheduling class.
5742 *
5743 * this syscall returns the minimum rt_priority that can be used
5744 * by a given scheduling class.
5745 */
5746asmlinkage long sys_sched_get_priority_min(int policy)
5747{
5748 int ret = -EINVAL;
5749
5750 switch (policy) {
5751 case SCHED_FIFO:
5752 case SCHED_RR:
5753 ret = 1;
5754 break;
5755 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005756 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005757 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 ret = 0;
5759 }
5760 return ret;
5761}
5762
5763/**
5764 * sys_sched_rr_get_interval - return the default timeslice of a process.
5765 * @pid: pid of the process.
5766 * @interval: userspace pointer to the timeslice value.
5767 *
5768 * this syscall writes the default timeslice value of a given process
5769 * into the user-space timespec buffer. A value of '0' means infinity.
5770 */
5771asmlinkage
5772long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5773{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005774 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005775 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005776 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
5779 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005780 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
5782 retval = -ESRCH;
5783 read_lock(&tasklist_lock);
5784 p = find_process_by_pid(pid);
5785 if (!p)
5786 goto out_unlock;
5787
5788 retval = security_task_getscheduler(p);
5789 if (retval)
5790 goto out_unlock;
5791
Ingo Molnar77034932007-12-04 17:04:39 +01005792 /*
5793 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5794 * tasks that are on an otherwise idle runqueue:
5795 */
5796 time_slice = 0;
5797 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005798 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005799 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005800 struct sched_entity *se = &p->se;
5801 unsigned long flags;
5802 struct rq *rq;
5803
5804 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005805 if (rq->cfs.load.weight)
5806 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005807 task_rq_unlock(rq, &flags);
5808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005810 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005813
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814out_unlock:
5815 read_unlock(&tasklist_lock);
5816 return retval;
5817}
5818
Steven Rostedt7c731e02008-05-12 21:20:41 +02005819static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005820
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005821void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005824 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005827 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005828 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005829#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005831 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005833 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834#else
5835 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005836 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005838 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839#endif
5840#ifdef CONFIG_DEBUG_STACK_USAGE
5841 {
Al Viro10ebffd2005-11-13 16:06:56 -08005842 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 while (!*n)
5844 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005845 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 }
5847#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005848 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005849 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005851 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852}
5853
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005854void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005856 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
Ingo Molnar4bd77322007-07-11 21:21:47 +02005858#if BITS_PER_LONG == 32
5859 printk(KERN_INFO
5860 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005862 printk(KERN_INFO
5863 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864#endif
5865 read_lock(&tasklist_lock);
5866 do_each_thread(g, p) {
5867 /*
5868 * reset the NMI-timeout, listing all files on a slow
5869 * console might take alot of time:
5870 */
5871 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005872 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005873 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 } while_each_thread(g, p);
5875
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005876 touch_all_softlockup_watchdogs();
5877
Ingo Molnardd41f592007-07-09 18:51:59 +02005878#ifdef CONFIG_SCHED_DEBUG
5879 sysrq_sched_debug_show();
5880#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005882 /*
5883 * Only show locks if all tasks are dumped:
5884 */
5885 if (state_filter == -1)
5886 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887}
5888
Ingo Molnar1df21052007-07-09 18:51:58 +02005889void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5890{
Ingo Molnardd41f592007-07-09 18:51:59 +02005891 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005892}
5893
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005894/**
5895 * init_idle - set up an idle thread for a given CPU
5896 * @idle: task in question
5897 * @cpu: cpu the idle task belongs to
5898 *
5899 * NOTE: this function does not set the idle thread's NEED_RESCHED
5900 * flag, to make booting more robust.
5901 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005902void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005904 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 unsigned long flags;
5906
Ingo Molnardd41f592007-07-09 18:51:59 +02005907 __sched_fork(idle);
5908 idle->se.exec_start = sched_clock();
5909
Ingo Molnarb29739f2006-06-27 02:54:51 -07005910 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005912 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913
5914 spin_lock_irqsave(&rq->lock, flags);
5915 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005916#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5917 idle->oncpu = 1;
5918#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 spin_unlock_irqrestore(&rq->lock, flags);
5920
5921 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005922#if defined(CONFIG_PREEMPT)
5923 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
5924#else
Al Viroa1261f542005-11-13 16:06:55 -08005925 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005926#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005927 /*
5928 * The idle tasks have their own, simple scheduling class:
5929 */
5930 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931}
5932
5933/*
5934 * In a system that switches off the HZ timer nohz_cpu_mask
5935 * indicates which cpus entered this state. This is used
5936 * in the rcu update to wait only for active cpus. For system
5937 * which do not switch off the HZ timer nohz_cpu_mask should
5938 * always be CPU_MASK_NONE.
5939 */
5940cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5941
Ingo Molnar19978ca2007-11-09 22:39:38 +01005942/*
5943 * Increase the granularity value when there are more CPUs,
5944 * because with more CPUs the 'effective latency' as visible
5945 * to users decreases. But the relationship is not linear,
5946 * so pick a second-best guess by going with the log2 of the
5947 * number of CPUs.
5948 *
5949 * This idea comes from the SD scheduler of Con Kolivas:
5950 */
5951static inline void sched_init_granularity(void)
5952{
5953 unsigned int factor = 1 + ilog2(num_online_cpus());
5954 const unsigned long limit = 200000000;
5955
5956 sysctl_sched_min_granularity *= factor;
5957 if (sysctl_sched_min_granularity > limit)
5958 sysctl_sched_min_granularity = limit;
5959
5960 sysctl_sched_latency *= factor;
5961 if (sysctl_sched_latency > limit)
5962 sysctl_sched_latency = limit;
5963
5964 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005965}
5966
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967#ifdef CONFIG_SMP
5968/*
5969 * This is how migration works:
5970 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005971 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 * runqueue and wake up that CPU's migration thread.
5973 * 2) we down() the locked semaphore => thread blocks.
5974 * 3) migration thread wakes up (implicitly it forces the migrated
5975 * thread off the CPU)
5976 * 4) it gets the migration request and checks whether the migrated
5977 * task is still in the wrong runqueue.
5978 * 5) if it's in the wrong runqueue then the migration thread removes
5979 * it and puts it into the right queue.
5980 * 6) migration thread up()s the semaphore.
5981 * 7) we wake up and the migration is done.
5982 */
5983
5984/*
5985 * Change a given task's CPU affinity. Migrate the thread to a
5986 * proper CPU and schedule it away if the CPU it's executing on
5987 * is removed from the allowed bitmask.
5988 *
5989 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005990 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 * call is not atomic; no spinlocks may be held.
5992 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005993int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005995 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005997 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005998 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
6000 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006001 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 ret = -EINVAL;
6003 goto out;
6004 }
6005
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01006006 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006007 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01006008 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006009 p->cpus_allowed = *new_mask;
6010 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01006011 }
6012
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006014 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 goto out;
6016
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006017 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 /* Need help from migration thread: drop lock and wait. */
6019 task_rq_unlock(rq, &flags);
6020 wake_up_process(rq->migration_thread);
6021 wait_for_completion(&req.done);
6022 tlb_migrate_finish(p->mm);
6023 return 0;
6024 }
6025out:
6026 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006027
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 return ret;
6029}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006030EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
6032/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006033 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 * this because either it can't run here any more (set_cpus_allowed()
6035 * away from this CPU, or CPU going down), or because we're
6036 * attempting to rebalance this task on exec (sched_exec).
6037 *
6038 * So we race with normal scheduler movements, but that's OK, as long
6039 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006040 *
6041 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006043static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006045 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006046 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
6048 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006049 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
6051 rq_src = cpu_rq(src_cpu);
6052 rq_dest = cpu_rq(dest_cpu);
6053
6054 double_rq_lock(rq_src, rq_dest);
6055 /* Already moved. */
6056 if (task_cpu(p) != src_cpu)
6057 goto out;
6058 /* Affinity changed (again). */
6059 if (!cpu_isset(dest_cpu, p->cpus_allowed))
6060 goto out;
6061
Ingo Molnardd41f592007-07-09 18:51:59 +02006062 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006063 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006064 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006065
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006067 if (on_rq) {
6068 activate_task(rq_dest, p, 0);
6069 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07006071 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072out:
6073 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006074 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075}
6076
6077/*
6078 * migration_thread - this is a highprio system thread that performs
6079 * thread migration by bumping thread off CPU then 'pushing' onto
6080 * another runqueue.
6081 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006082static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006085 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
6087 rq = cpu_rq(cpu);
6088 BUG_ON(rq->migration_thread != current);
6089
6090 set_current_state(TASK_INTERRUPTIBLE);
6091 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006092 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 spin_lock_irq(&rq->lock);
6096
6097 if (cpu_is_offline(cpu)) {
6098 spin_unlock_irq(&rq->lock);
6099 goto wait_to_die;
6100 }
6101
6102 if (rq->active_balance) {
6103 active_load_balance(rq, cpu);
6104 rq->active_balance = 0;
6105 }
6106
6107 head = &rq->migration_queue;
6108
6109 if (list_empty(head)) {
6110 spin_unlock_irq(&rq->lock);
6111 schedule();
6112 set_current_state(TASK_INTERRUPTIBLE);
6113 continue;
6114 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006115 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 list_del_init(head->next);
6117
Nick Piggin674311d2005-06-25 14:57:27 -07006118 spin_unlock(&rq->lock);
6119 __migrate_task(req->task, cpu, req->dest_cpu);
6120 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121
6122 complete(&req->done);
6123 }
6124 __set_current_state(TASK_RUNNING);
6125 return 0;
6126
6127wait_to_die:
6128 /* Wait for kthread_stop */
6129 set_current_state(TASK_INTERRUPTIBLE);
6130 while (!kthread_should_stop()) {
6131 schedule();
6132 set_current_state(TASK_INTERRUPTIBLE);
6133 }
6134 __set_current_state(TASK_RUNNING);
6135 return 0;
6136}
6137
6138#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006139
6140static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6141{
6142 int ret;
6143
6144 local_irq_disable();
6145 ret = __migrate_task(p, src_cpu, dest_cpu);
6146 local_irq_enable();
6147 return ret;
6148}
6149
Kirill Korotaev054b9102006-12-10 02:20:11 -08006150/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006151 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006152 * NOTE: interrupts should be disabled by the caller
6153 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006154static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006156 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006158 struct rq *rq;
6159 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
Andi Kleen3a5c3592007-10-15 17:00:14 +02006161 do {
6162 /* On same node? */
6163 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6164 cpus_and(mask, mask, p->cpus_allowed);
6165 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
Andi Kleen3a5c3592007-10-15 17:00:14 +02006167 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006168 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006169 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170
Andi Kleen3a5c3592007-10-15 17:00:14 +02006171 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006172 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006173 cpumask_t cpus_allowed;
6174
6175 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006176 /*
6177 * Try to stay on the same cpuset, where the
6178 * current cpuset may be a subset of all cpus.
6179 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006180 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006181 * called within calls to cpuset_lock/cpuset_unlock.
6182 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006183 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006184 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006185 dest_cpu = any_online_cpu(p->cpus_allowed);
6186 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187
Andi Kleen3a5c3592007-10-15 17:00:14 +02006188 /*
6189 * Don't tell them about moving exiting tasks or
6190 * kernel threads (both mm NULL), since they never
6191 * leave kernel.
6192 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006193 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006194 printk(KERN_INFO "process %d (%s) no "
6195 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006196 task_pid_nr(p), p->comm, dead_cpu);
6197 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006198 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006199 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200}
6201
6202/*
6203 * While a dead CPU has no uninterruptible tasks queued at this point,
6204 * it might still have a nonzero ->nr_uninterruptible counter, because
6205 * for performance reasons the counter is not stricly tracking tasks to
6206 * their home CPUs. So we just add the counter to another CPU's counter,
6207 * to keep the global sum constant after CPU-down:
6208 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006209static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210{
Mike Travis7c16ec52008-04-04 18:11:11 -07006211 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 unsigned long flags;
6213
6214 local_irq_save(flags);
6215 double_rq_lock(rq_src, rq_dest);
6216 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6217 rq_src->nr_uninterruptible = 0;
6218 double_rq_unlock(rq_src, rq_dest);
6219 local_irq_restore(flags);
6220}
6221
6222/* Run through task list and migrate tasks from the dead cpu. */
6223static void migrate_live_tasks(int src_cpu)
6224{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006225 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006227 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
Ingo Molnar48f24c42006-07-03 00:25:40 -07006229 do_each_thread(t, p) {
6230 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 continue;
6232
Ingo Molnar48f24c42006-07-03 00:25:40 -07006233 if (task_cpu(p) == src_cpu)
6234 move_task_off_dead_cpu(src_cpu, p);
6235 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006237 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238}
6239
Ingo Molnardd41f592007-07-09 18:51:59 +02006240/*
6241 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006242 * It does so by boosting its priority to highest possible.
6243 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 */
6245void sched_idle_next(void)
6246{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006247 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006248 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 struct task_struct *p = rq->idle;
6250 unsigned long flags;
6251
6252 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006253 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
Ingo Molnar48f24c42006-07-03 00:25:40 -07006255 /*
6256 * Strictly not necessary since rest of the CPUs are stopped by now
6257 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 */
6259 spin_lock_irqsave(&rq->lock, flags);
6260
Ingo Molnardd41f592007-07-09 18:51:59 +02006261 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006262
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006263 update_rq_clock(rq);
6264 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
6266 spin_unlock_irqrestore(&rq->lock, flags);
6267}
6268
Ingo Molnar48f24c42006-07-03 00:25:40 -07006269/*
6270 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 * offline.
6272 */
6273void idle_task_exit(void)
6274{
6275 struct mm_struct *mm = current->active_mm;
6276
6277 BUG_ON(cpu_online(smp_processor_id()));
6278
6279 if (mm != &init_mm)
6280 switch_mm(mm, &init_mm, current);
6281 mmdrop(mm);
6282}
6283
Kirill Korotaev054b9102006-12-10 02:20:11 -08006284/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006285static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006287 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288
6289 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006290 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
6292 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006293 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Ingo Molnar48f24c42006-07-03 00:25:40 -07006295 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
6297 /*
6298 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006299 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 * fine.
6301 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006302 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006303 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006304 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305
Ingo Molnar48f24c42006-07-03 00:25:40 -07006306 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307}
6308
6309/* release_task() removes task from tasklist, so we won't find dead tasks. */
6310static void migrate_dead_tasks(unsigned int dead_cpu)
6311{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006312 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006313 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
Ingo Molnardd41f592007-07-09 18:51:59 +02006315 for ( ; ; ) {
6316 if (!rq->nr_running)
6317 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006318 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006319 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006320 if (!next)
6321 break;
6322 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006323
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 }
6325}
6326#endif /* CONFIG_HOTPLUG_CPU */
6327
Nick Piggine692ab52007-07-26 13:40:43 +02006328#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6329
6330static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006331 {
6332 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006333 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006334 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006335 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006336};
6337
6338static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006339 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006340 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006341 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006342 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006343 .child = sd_ctl_dir,
6344 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006345 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006346};
6347
6348static struct ctl_table *sd_alloc_ctl_entry(int n)
6349{
6350 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006351 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006352
Nick Piggine692ab52007-07-26 13:40:43 +02006353 return entry;
6354}
6355
Milton Miller6382bc92007-10-15 17:00:19 +02006356static void sd_free_ctl_entry(struct ctl_table **tablep)
6357{
Milton Millercd7900762007-10-17 16:55:11 +02006358 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006359
Milton Millercd7900762007-10-17 16:55:11 +02006360 /*
6361 * In the intermediate directories, both the child directory and
6362 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006363 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006364 * static strings and all have proc handlers.
6365 */
6366 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006367 if (entry->child)
6368 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006369 if (entry->proc_handler == NULL)
6370 kfree(entry->procname);
6371 }
Milton Miller6382bc92007-10-15 17:00:19 +02006372
6373 kfree(*tablep);
6374 *tablep = NULL;
6375}
6376
Nick Piggine692ab52007-07-26 13:40:43 +02006377static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006378set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006379 const char *procname, void *data, int maxlen,
6380 mode_t mode, proc_handler *proc_handler)
6381{
Nick Piggine692ab52007-07-26 13:40:43 +02006382 entry->procname = procname;
6383 entry->data = data;
6384 entry->maxlen = maxlen;
6385 entry->mode = mode;
6386 entry->proc_handler = proc_handler;
6387}
6388
6389static struct ctl_table *
6390sd_alloc_ctl_domain_table(struct sched_domain *sd)
6391{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006392 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006393
Milton Millerad1cdc12007-10-15 17:00:19 +02006394 if (table == NULL)
6395 return NULL;
6396
Alexey Dobriyane0361852007-08-09 11:16:46 +02006397 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006398 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006399 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006400 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006401 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006402 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006403 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006404 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006405 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006406 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006407 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006408 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006409 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006410 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006411 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006412 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006413 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006414 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006415 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006416 &sd->cache_nice_tries,
6417 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006418 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006419 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006420 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006421
6422 return table;
6423}
6424
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006425static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006426{
6427 struct ctl_table *entry, *table;
6428 struct sched_domain *sd;
6429 int domain_num = 0, i;
6430 char buf[32];
6431
6432 for_each_domain(cpu, sd)
6433 domain_num++;
6434 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006435 if (table == NULL)
6436 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006437
6438 i = 0;
6439 for_each_domain(cpu, sd) {
6440 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006441 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006442 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006443 entry->child = sd_alloc_ctl_domain_table(sd);
6444 entry++;
6445 i++;
6446 }
6447 return table;
6448}
6449
6450static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006451static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006452{
6453 int i, cpu_num = num_online_cpus();
6454 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6455 char buf[32];
6456
Milton Miller73785472007-10-24 18:23:48 +02006457 WARN_ON(sd_ctl_dir[0].child);
6458 sd_ctl_dir[0].child = entry;
6459
Milton Millerad1cdc12007-10-15 17:00:19 +02006460 if (entry == NULL)
6461 return;
6462
Milton Miller97b6ea72007-10-15 17:00:19 +02006463 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006464 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006465 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006466 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006467 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006468 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006469 }
Milton Miller73785472007-10-24 18:23:48 +02006470
6471 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006472 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6473}
Milton Miller6382bc92007-10-15 17:00:19 +02006474
Milton Miller73785472007-10-24 18:23:48 +02006475/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006476static void unregister_sched_domain_sysctl(void)
6477{
Milton Miller73785472007-10-24 18:23:48 +02006478 if (sd_sysctl_header)
6479 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006480 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006481 if (sd_ctl_dir[0].child)
6482 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006483}
Nick Piggine692ab52007-07-26 13:40:43 +02006484#else
Milton Miller6382bc92007-10-15 17:00:19 +02006485static void register_sched_domain_sysctl(void)
6486{
6487}
6488static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006489{
6490}
6491#endif
6492
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493/*
6494 * migration_call - callback that gets triggered when a CPU is added.
6495 * Here we can start up the necessary migration thread for the new CPU.
6496 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006497static int __cpuinit
6498migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006501 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006503 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504
6505 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006506
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006508 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006509 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 if (IS_ERR(p))
6511 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 kthread_bind(p, cpu);
6513 /* Must be high prio: stop_machine expects to yield to it. */
6514 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006515 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 task_rq_unlock(rq, &flags);
6517 cpu_rq(cpu)->migration_thread = p;
6518 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006519
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006521 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006522 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006524
6525 /* Update our root-domain */
6526 rq = cpu_rq(cpu);
6527 spin_lock_irqsave(&rq->lock, flags);
6528 if (rq->rd) {
6529 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6530 cpu_set(cpu, rq->rd->online);
6531 }
6532 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006534
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535#ifdef CONFIG_HOTPLUG_CPU
6536 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006537 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006538 if (!cpu_rq(cpu)->migration_thread)
6539 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006540 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006541 kthread_bind(cpu_rq(cpu)->migration_thread,
6542 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 kthread_stop(cpu_rq(cpu)->migration_thread);
6544 cpu_rq(cpu)->migration_thread = NULL;
6545 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006546
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006548 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006549 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 migrate_live_tasks(cpu);
6551 rq = cpu_rq(cpu);
6552 kthread_stop(rq->migration_thread);
6553 rq->migration_thread = NULL;
6554 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006555 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006556 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006557 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006559 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6560 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006562 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006563 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 migrate_nr_uninterruptible(rq);
6565 BUG_ON(rq->nr_running != 0);
6566
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006567 /*
6568 * No need to migrate the tasks: it was best-effort if
6569 * they didn't take sched_hotcpu_mutex. Just wake up
6570 * the requestors.
6571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 spin_lock_irq(&rq->lock);
6573 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006574 struct migration_req *req;
6575
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006577 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 list_del_init(&req->list);
6579 complete(&req->done);
6580 }
6581 spin_unlock_irq(&rq->lock);
6582 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006583
Gregory Haskins08f503b2008-03-10 17:59:11 -04006584 case CPU_DYING:
6585 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006586 /* Update our root-domain */
6587 rq = cpu_rq(cpu);
6588 spin_lock_irqsave(&rq->lock, flags);
6589 if (rq->rd) {
6590 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6591 cpu_clear(cpu, rq->rd->online);
6592 }
6593 spin_unlock_irqrestore(&rq->lock, flags);
6594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595#endif
6596 }
6597 return NOTIFY_OK;
6598}
6599
6600/* Register at highest priority so that task migration (migrate_all_tasks)
6601 * happens before everything else.
6602 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006603static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 .notifier_call = migration_call,
6605 .priority = 10
6606};
6607
Adrian Bunke6fe6642007-11-09 22:39:39 +01006608void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609{
6610 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006611 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006612
6613 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006614 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6615 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6617 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618}
6619#endif
6620
6621#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006622
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006623#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006624
Mike Travis7c16ec52008-04-04 18:11:11 -07006625static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6626 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006627{
6628 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006629 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006630
Mike Travis434d53b2008-04-04 18:11:04 -07006631 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006632 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006633
6634 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6635
6636 if (!(sd->flags & SD_LOAD_BALANCE)) {
6637 printk("does not load-balance\n");
6638 if (sd->parent)
6639 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6640 " has parent");
6641 return -1;
6642 }
6643
6644 printk(KERN_CONT "span %s\n", str);
6645
6646 if (!cpu_isset(cpu, sd->span)) {
6647 printk(KERN_ERR "ERROR: domain->span does not contain "
6648 "CPU%d\n", cpu);
6649 }
6650 if (!cpu_isset(cpu, group->cpumask)) {
6651 printk(KERN_ERR "ERROR: domain->groups does not contain"
6652 " CPU%d\n", cpu);
6653 }
6654
6655 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6656 do {
6657 if (!group) {
6658 printk("\n");
6659 printk(KERN_ERR "ERROR: group is NULL\n");
6660 break;
6661 }
6662
6663 if (!group->__cpu_power) {
6664 printk(KERN_CONT "\n");
6665 printk(KERN_ERR "ERROR: domain->cpu_power not "
6666 "set\n");
6667 break;
6668 }
6669
6670 if (!cpus_weight(group->cpumask)) {
6671 printk(KERN_CONT "\n");
6672 printk(KERN_ERR "ERROR: empty group\n");
6673 break;
6674 }
6675
Mike Travis7c16ec52008-04-04 18:11:11 -07006676 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006677 printk(KERN_CONT "\n");
6678 printk(KERN_ERR "ERROR: repeated CPUs\n");
6679 break;
6680 }
6681
Mike Travis7c16ec52008-04-04 18:11:11 -07006682 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006683
Mike Travis434d53b2008-04-04 18:11:04 -07006684 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006685 printk(KERN_CONT " %s", str);
6686
6687 group = group->next;
6688 } while (group != sd->groups);
6689 printk(KERN_CONT "\n");
6690
Mike Travis7c16ec52008-04-04 18:11:11 -07006691 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006692 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6693
Mike Travis7c16ec52008-04-04 18:11:11 -07006694 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006695 printk(KERN_ERR "ERROR: parent span is not a superset "
6696 "of domain->span\n");
6697 return 0;
6698}
6699
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700static void sched_domain_debug(struct sched_domain *sd, int cpu)
6701{
Mike Travis7c16ec52008-04-04 18:11:11 -07006702 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 int level = 0;
6704
Nick Piggin41c7ce92005-06-25 14:57:24 -07006705 if (!sd) {
6706 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6707 return;
6708 }
6709
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6711
Mike Travis7c16ec52008-04-04 18:11:11 -07006712 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6713 if (!groupmask) {
6714 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6715 return;
6716 }
6717
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006718 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006719 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 level++;
6722 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006723 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006724 break;
6725 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006726 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727}
6728#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006729# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730#endif
6731
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006732static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006733{
6734 if (cpus_weight(sd->span) == 1)
6735 return 1;
6736
6737 /* Following flags need at least 2 groups */
6738 if (sd->flags & (SD_LOAD_BALANCE |
6739 SD_BALANCE_NEWIDLE |
6740 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006741 SD_BALANCE_EXEC |
6742 SD_SHARE_CPUPOWER |
6743 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006744 if (sd->groups != sd->groups->next)
6745 return 0;
6746 }
6747
6748 /* Following flags don't use groups */
6749 if (sd->flags & (SD_WAKE_IDLE |
6750 SD_WAKE_AFFINE |
6751 SD_WAKE_BALANCE))
6752 return 0;
6753
6754 return 1;
6755}
6756
Ingo Molnar48f24c42006-07-03 00:25:40 -07006757static int
6758sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006759{
6760 unsigned long cflags = sd->flags, pflags = parent->flags;
6761
6762 if (sd_degenerate(parent))
6763 return 1;
6764
6765 if (!cpus_equal(sd->span, parent->span))
6766 return 0;
6767
6768 /* Does parent contain flags not in child? */
6769 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6770 if (cflags & SD_WAKE_AFFINE)
6771 pflags &= ~SD_WAKE_BALANCE;
6772 /* Flags needing groups don't count if only 1 group in parent */
6773 if (parent->groups == parent->groups->next) {
6774 pflags &= ~(SD_LOAD_BALANCE |
6775 SD_BALANCE_NEWIDLE |
6776 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006777 SD_BALANCE_EXEC |
6778 SD_SHARE_CPUPOWER |
6779 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006780 }
6781 if (~cflags & pflags)
6782 return 0;
6783
6784 return 1;
6785}
6786
Gregory Haskins57d885f2008-01-25 21:08:18 +01006787static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6788{
6789 unsigned long flags;
6790 const struct sched_class *class;
6791
6792 spin_lock_irqsave(&rq->lock, flags);
6793
6794 if (rq->rd) {
6795 struct root_domain *old_rd = rq->rd;
6796
Ingo Molnar0eab9142008-01-25 21:08:19 +01006797 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006798 if (class->leave_domain)
6799 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006800 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006801
Gregory Haskinsdc938522008-01-25 21:08:26 +01006802 cpu_clear(rq->cpu, old_rd->span);
6803 cpu_clear(rq->cpu, old_rd->online);
6804
Gregory Haskins57d885f2008-01-25 21:08:18 +01006805 if (atomic_dec_and_test(&old_rd->refcount))
6806 kfree(old_rd);
6807 }
6808
6809 atomic_inc(&rd->refcount);
6810 rq->rd = rd;
6811
Gregory Haskinsdc938522008-01-25 21:08:26 +01006812 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006813 if (cpu_isset(rq->cpu, cpu_online_map))
6814 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006815
Ingo Molnar0eab9142008-01-25 21:08:19 +01006816 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006817 if (class->join_domain)
6818 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006819 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006820
6821 spin_unlock_irqrestore(&rq->lock, flags);
6822}
6823
Gregory Haskinsdc938522008-01-25 21:08:26 +01006824static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006825{
6826 memset(rd, 0, sizeof(*rd));
6827
Gregory Haskinsdc938522008-01-25 21:08:26 +01006828 cpus_clear(rd->span);
6829 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006830}
6831
6832static void init_defrootdomain(void)
6833{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006834 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006835 atomic_set(&def_root_domain.refcount, 1);
6836}
6837
Gregory Haskinsdc938522008-01-25 21:08:26 +01006838static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006839{
6840 struct root_domain *rd;
6841
6842 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6843 if (!rd)
6844 return NULL;
6845
Gregory Haskinsdc938522008-01-25 21:08:26 +01006846 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006847
6848 return rd;
6849}
6850
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006852 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 * hold the hotplug lock.
6854 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006855static void
6856cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006858 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006859 struct sched_domain *tmp;
6860
6861 /* Remove the sched domains which do not contribute to scheduling. */
6862 for (tmp = sd; tmp; tmp = tmp->parent) {
6863 struct sched_domain *parent = tmp->parent;
6864 if (!parent)
6865 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006866 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006867 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006868 if (parent->parent)
6869 parent->parent->child = tmp;
6870 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006871 }
6872
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006873 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006874 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006875 if (sd)
6876 sd->child = NULL;
6877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878
6879 sched_domain_debug(sd, cpu);
6880
Gregory Haskins57d885f2008-01-25 21:08:18 +01006881 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006882 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883}
6884
6885/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006886static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887
6888/* Setup the mask of cpus configured for isolated domains */
6889static int __init isolated_cpu_setup(char *str)
6890{
6891 int ints[NR_CPUS], i;
6892
6893 str = get_options(str, ARRAY_SIZE(ints), ints);
6894 cpus_clear(cpu_isolated_map);
6895 for (i = 1; i <= ints[0]; i++)
6896 if (ints[i] < NR_CPUS)
6897 cpu_set(ints[i], cpu_isolated_map);
6898 return 1;
6899}
6900
Ingo Molnar8927f492007-10-15 17:00:13 +02006901__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
6903/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006904 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6905 * to a function which identifies what group(along with sched group) a CPU
6906 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6907 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 *
6909 * init_sched_build_groups will build a circular linked list of the groups
6910 * covered by the given span, and will set each group's ->cpumask correctly,
6911 * and ->cpu_power to 0.
6912 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006913static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006914init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006915 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006916 struct sched_group **sg,
6917 cpumask_t *tmpmask),
6918 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919{
6920 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 int i;
6922
Mike Travis7c16ec52008-04-04 18:11:11 -07006923 cpus_clear(*covered);
6924
6925 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006926 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006927 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 int j;
6929
Mike Travis7c16ec52008-04-04 18:11:11 -07006930 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 continue;
6932
Mike Travis7c16ec52008-04-04 18:11:11 -07006933 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006934 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935
Mike Travis7c16ec52008-04-04 18:11:11 -07006936 for_each_cpu_mask(j, *span) {
6937 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 continue;
6939
Mike Travis7c16ec52008-04-04 18:11:11 -07006940 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 cpu_set(j, sg->cpumask);
6942 }
6943 if (!first)
6944 first = sg;
6945 if (last)
6946 last->next = sg;
6947 last = sg;
6948 }
6949 last->next = first;
6950}
6951
John Hawkes9c1cfda2005-09-06 15:18:14 -07006952#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953
John Hawkes9c1cfda2005-09-06 15:18:14 -07006954#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006955
John Hawkes9c1cfda2005-09-06 15:18:14 -07006956/**
6957 * find_next_best_node - find the next node to include in a sched_domain
6958 * @node: node whose sched_domain we're building
6959 * @used_nodes: nodes already in the sched_domain
6960 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006961 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006962 * finds the closest node not already in the @used_nodes map.
6963 *
6964 * Should use nodemask_t.
6965 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006966static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006967{
6968 int i, n, val, min_val, best_node = 0;
6969
6970 min_val = INT_MAX;
6971
6972 for (i = 0; i < MAX_NUMNODES; i++) {
6973 /* Start at @node */
6974 n = (node + i) % MAX_NUMNODES;
6975
6976 if (!nr_cpus_node(n))
6977 continue;
6978
6979 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006980 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006981 continue;
6982
6983 /* Simple min distance search */
6984 val = node_distance(node, n);
6985
6986 if (val < min_val) {
6987 min_val = val;
6988 best_node = n;
6989 }
6990 }
6991
Mike Travisc5f59f02008-04-04 18:11:10 -07006992 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006993 return best_node;
6994}
6995
6996/**
6997 * sched_domain_node_span - get a cpumask for a node's sched_domain
6998 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07006999 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007000 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007001 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007002 * should be one that prevents unnecessary balancing, but also spreads tasks
7003 * out optimally.
7004 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07007005static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007006{
Mike Travisc5f59f02008-04-04 18:11:10 -07007007 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07007008 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007009 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007010
Mike Travis4bdbaad32008-04-15 16:35:52 -07007011 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007012 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007013
Mike Travis4bdbaad32008-04-15 16:35:52 -07007014 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07007015 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007016
7017 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007018 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007019
Mike Travisc5f59f02008-04-04 18:11:10 -07007020 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007021 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007022 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007023}
7024#endif
7025
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007026int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007027
John Hawkes9c1cfda2005-09-06 15:18:14 -07007028/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07007029 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07007030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031#ifdef CONFIG_SCHED_SMT
7032static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007033static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007034
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007035static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007036cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7037 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007039 if (sg)
7040 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 return cpu;
7042}
7043#endif
7044
Ingo Molnar48f24c42006-07-03 00:25:40 -07007045/*
7046 * multi-core sched-domains:
7047 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007048#ifdef CONFIG_SCHED_MC
7049static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007050static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007051#endif
7052
7053#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007054static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007055cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7056 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007057{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007058 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007059
7060 *mask = per_cpu(cpu_sibling_map, cpu);
7061 cpus_and(*mask, *mask, *cpu_map);
7062 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007063 if (sg)
7064 *sg = &per_cpu(sched_group_core, group);
7065 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007066}
7067#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007068static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007069cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7070 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007071{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007072 if (sg)
7073 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007074 return cpu;
7075}
7076#endif
7077
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007079static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007080
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007081static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007082cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7083 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007085 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007086#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007087 *mask = cpu_coregroup_map(cpu);
7088 cpus_and(*mask, *mask, *cpu_map);
7089 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007090#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007091 *mask = per_cpu(cpu_sibling_map, cpu);
7092 cpus_and(*mask, *mask, *cpu_map);
7093 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007095 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007097 if (sg)
7098 *sg = &per_cpu(sched_group_phys, group);
7099 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100}
7101
7102#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007103/*
7104 * The init_sched_build_groups can't handle what we want to do with node
7105 * groups, so roll our own. Now each node has its own list of groups which
7106 * gets dynamically allocated.
7107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007109static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007110
7111static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007112static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007113
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007114static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007115 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007117 int group;
7118
Mike Travis7c16ec52008-04-04 18:11:11 -07007119 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7120 cpus_and(*nodemask, *nodemask, *cpu_map);
7121 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007122
7123 if (sg)
7124 *sg = &per_cpu(sched_group_allnodes, group);
7125 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007127
Siddha, Suresh B08069032006-03-27 01:15:23 -08007128static void init_numa_sched_groups_power(struct sched_group *group_head)
7129{
7130 struct sched_group *sg = group_head;
7131 int j;
7132
7133 if (!sg)
7134 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007135 do {
7136 for_each_cpu_mask(j, sg->cpumask) {
7137 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007138
Andi Kleen3a5c3592007-10-15 17:00:14 +02007139 sd = &per_cpu(phys_domains, j);
7140 if (j != first_cpu(sd->groups->cpumask)) {
7141 /*
7142 * Only add "power" once for each
7143 * physical package.
7144 */
7145 continue;
7146 }
7147
7148 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007149 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007150 sg = sg->next;
7151 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007152}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153#endif
7154
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007155#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007156/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007157static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007158{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007159 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007160
7161 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007162 struct sched_group **sched_group_nodes
7163 = sched_group_nodes_bycpu[cpu];
7164
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007165 if (!sched_group_nodes)
7166 continue;
7167
7168 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007169 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7170
Mike Travis7c16ec52008-04-04 18:11:11 -07007171 *nodemask = node_to_cpumask(i);
7172 cpus_and(*nodemask, *nodemask, *cpu_map);
7173 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007174 continue;
7175
7176 if (sg == NULL)
7177 continue;
7178 sg = sg->next;
7179next_sg:
7180 oldsg = sg;
7181 sg = sg->next;
7182 kfree(oldsg);
7183 if (oldsg != sched_group_nodes[i])
7184 goto next_sg;
7185 }
7186 kfree(sched_group_nodes);
7187 sched_group_nodes_bycpu[cpu] = NULL;
7188 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007189}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007190#else
Mike Travis7c16ec52008-04-04 18:11:11 -07007191static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007192{
7193}
7194#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007195
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007197 * Initialize sched groups cpu_power.
7198 *
7199 * cpu_power indicates the capacity of sched group, which is used while
7200 * distributing the load between different sched groups in a sched domain.
7201 * Typically cpu_power for all the groups in a sched domain will be same unless
7202 * there are asymmetries in the topology. If there are asymmetries, group
7203 * having more cpu_power will pickup more load compared to the group having
7204 * less cpu_power.
7205 *
7206 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7207 * the maximum number of tasks a group can handle in the presence of other idle
7208 * or lightly loaded groups in the same sched domain.
7209 */
7210static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7211{
7212 struct sched_domain *child;
7213 struct sched_group *group;
7214
7215 WARN_ON(!sd || !sd->groups);
7216
7217 if (cpu != first_cpu(sd->groups->cpumask))
7218 return;
7219
7220 child = sd->child;
7221
Eric Dumazet5517d862007-05-08 00:32:57 -07007222 sd->groups->__cpu_power = 0;
7223
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007224 /*
7225 * For perf policy, if the groups in child domain share resources
7226 * (for example cores sharing some portions of the cache hierarchy
7227 * or SMT), then set this domain groups cpu_power such that each group
7228 * can handle only one task, when there are other idle groups in the
7229 * same sched domain.
7230 */
7231 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7232 (child->flags &
7233 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007234 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007235 return;
7236 }
7237
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007238 /*
7239 * add cpu_power of each child group to this groups cpu_power
7240 */
7241 group = child->groups;
7242 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007243 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007244 group = group->next;
7245 } while (group != child->groups);
7246}
7247
7248/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007249 * Initializers for schedule domains
7250 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7251 */
7252
7253#define SD_INIT(sd, type) sd_init_##type(sd)
7254#define SD_INIT_FUNC(type) \
7255static noinline void sd_init_##type(struct sched_domain *sd) \
7256{ \
7257 memset(sd, 0, sizeof(*sd)); \
7258 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007259 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007260}
7261
7262SD_INIT_FUNC(CPU)
7263#ifdef CONFIG_NUMA
7264 SD_INIT_FUNC(ALLNODES)
7265 SD_INIT_FUNC(NODE)
7266#endif
7267#ifdef CONFIG_SCHED_SMT
7268 SD_INIT_FUNC(SIBLING)
7269#endif
7270#ifdef CONFIG_SCHED_MC
7271 SD_INIT_FUNC(MC)
7272#endif
7273
7274/*
7275 * To minimize stack usage kmalloc room for cpumasks and share the
7276 * space as the usage in build_sched_domains() dictates. Used only
7277 * if the amount of space is significant.
7278 */
7279struct allmasks {
7280 cpumask_t tmpmask; /* make this one first */
7281 union {
7282 cpumask_t nodemask;
7283 cpumask_t this_sibling_map;
7284 cpumask_t this_core_map;
7285 };
7286 cpumask_t send_covered;
7287
7288#ifdef CONFIG_NUMA
7289 cpumask_t domainspan;
7290 cpumask_t covered;
7291 cpumask_t notcovered;
7292#endif
7293};
7294
7295#if NR_CPUS > 128
7296#define SCHED_CPUMASK_ALLOC 1
7297#define SCHED_CPUMASK_FREE(v) kfree(v)
7298#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7299#else
7300#define SCHED_CPUMASK_ALLOC 0
7301#define SCHED_CPUMASK_FREE(v)
7302#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7303#endif
7304
7305#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7306 ((unsigned long)(a) + offsetof(struct allmasks, v))
7307
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007308static int default_relax_domain_level = -1;
7309
7310static int __init setup_relax_domain_level(char *str)
7311{
7312 default_relax_domain_level = simple_strtoul(str, NULL, 0);
7313 return 1;
7314}
7315__setup("relax_domain_level=", setup_relax_domain_level);
7316
7317static void set_domain_attribute(struct sched_domain *sd,
7318 struct sched_domain_attr *attr)
7319{
7320 int request;
7321
7322 if (!attr || attr->relax_domain_level < 0) {
7323 if (default_relax_domain_level < 0)
7324 return;
7325 else
7326 request = default_relax_domain_level;
7327 } else
7328 request = attr->relax_domain_level;
7329 if (request < sd->level) {
7330 /* turn off idle balance on this domain */
7331 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7332 } else {
7333 /* turn on idle balance on this domain */
7334 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7335 }
7336}
7337
Mike Travis7c16ec52008-04-04 18:11:11 -07007338/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007339 * Build sched domains for a given set of cpus and attach the sched domains
7340 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007342static int __build_sched_domains(const cpumask_t *cpu_map,
7343 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344{
7345 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007346 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007347 SCHED_CPUMASK_DECLARE(allmasks);
7348 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007349#ifdef CONFIG_NUMA
7350 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007351 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007352
7353 /*
7354 * Allocate the per-node list of sched groups
7355 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007356 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007357 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007358 if (!sched_group_nodes) {
7359 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007360 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007361 }
John Hawkesd1b55132005-09-06 15:18:14 -07007362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363
Gregory Haskinsdc938522008-01-25 21:08:26 +01007364 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007365 if (!rd) {
7366 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007367#ifdef CONFIG_NUMA
7368 kfree(sched_group_nodes);
7369#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007370 return -ENOMEM;
7371 }
7372
Mike Travis7c16ec52008-04-04 18:11:11 -07007373#if SCHED_CPUMASK_ALLOC
7374 /* get space for all scratch cpumask variables */
7375 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7376 if (!allmasks) {
7377 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7378 kfree(rd);
7379#ifdef CONFIG_NUMA
7380 kfree(sched_group_nodes);
7381#endif
7382 return -ENOMEM;
7383 }
7384#endif
7385 tmpmask = (cpumask_t *)allmasks;
7386
7387
7388#ifdef CONFIG_NUMA
7389 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7390#endif
7391
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007393 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007395 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007397 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398
Mike Travis7c16ec52008-04-04 18:11:11 -07007399 *nodemask = node_to_cpumask(cpu_to_node(i));
7400 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401
7402#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007403 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007404 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007405 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007406 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007407 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007408 sd->span = *cpu_map;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007409 sd->first_cpu = first_cpu(sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07007410 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007411 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007412 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007413 } else
7414 p = NULL;
7415
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007417 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007418 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007419 sched_domain_node_span(cpu_to_node(i), &sd->span);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007420 sd->first_cpu = first_cpu(sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007421 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007422 if (p)
7423 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007424 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425#endif
7426
7427 p = sd;
7428 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007429 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007430 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007431 sd->span = *nodemask;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007432 sd->first_cpu = first_cpu(sd->span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007434 if (p)
7435 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007436 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007438#ifdef CONFIG_SCHED_MC
7439 p = sd;
7440 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007441 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007442 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007443 sd->span = cpu_coregroup_map(i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007444 sd->first_cpu = first_cpu(sd->span);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007445 cpus_and(sd->span, sd->span, *cpu_map);
7446 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007447 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007448 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007449#endif
7450
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451#ifdef CONFIG_SCHED_SMT
7452 p = sd;
7453 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007454 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007455 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007456 sd->span = per_cpu(cpu_sibling_map, i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007457 sd->first_cpu = first_cpu(sd->span);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007458 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007460 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007461 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462#endif
7463 }
7464
7465#ifdef CONFIG_SCHED_SMT
7466 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007467 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007468 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7469 SCHED_CPUMASK_VAR(send_covered, allmasks);
7470
7471 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7472 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7473 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 continue;
7475
Ingo Molnardd41f592007-07-09 18:51:59 +02007476 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007477 &cpu_to_cpu_group,
7478 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 }
7480#endif
7481
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007482#ifdef CONFIG_SCHED_MC
7483 /* Set up multi-core groups */
7484 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007485 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7486 SCHED_CPUMASK_VAR(send_covered, allmasks);
7487
7488 *this_core_map = cpu_coregroup_map(i);
7489 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7490 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007491 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007492
Ingo Molnardd41f592007-07-09 18:51:59 +02007493 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007494 &cpu_to_core_group,
7495 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007496 }
7497#endif
7498
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 /* Set up physical groups */
7500 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007501 SCHED_CPUMASK_VAR(nodemask, allmasks);
7502 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503
Mike Travis7c16ec52008-04-04 18:11:11 -07007504 *nodemask = node_to_cpumask(i);
7505 cpus_and(*nodemask, *nodemask, *cpu_map);
7506 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507 continue;
7508
Mike Travis7c16ec52008-04-04 18:11:11 -07007509 init_sched_build_groups(nodemask, cpu_map,
7510 &cpu_to_phys_group,
7511 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 }
7513
7514#ifdef CONFIG_NUMA
7515 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007516 if (sd_allnodes) {
7517 SCHED_CPUMASK_VAR(send_covered, allmasks);
7518
7519 init_sched_build_groups(cpu_map, cpu_map,
7520 &cpu_to_allnodes_group,
7521 send_covered, tmpmask);
7522 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007523
7524 for (i = 0; i < MAX_NUMNODES; i++) {
7525 /* Set up node groups */
7526 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007527 SCHED_CPUMASK_VAR(nodemask, allmasks);
7528 SCHED_CPUMASK_VAR(domainspan, allmasks);
7529 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007530 int j;
7531
Mike Travis7c16ec52008-04-04 18:11:11 -07007532 *nodemask = node_to_cpumask(i);
7533 cpus_clear(*covered);
7534
7535 cpus_and(*nodemask, *nodemask, *cpu_map);
7536 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007537 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007538 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007539 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007540
Mike Travis4bdbaad32008-04-15 16:35:52 -07007541 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007542 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007543
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007544 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007545 if (!sg) {
7546 printk(KERN_WARNING "Can not alloc domain group for "
7547 "node %d\n", i);
7548 goto error;
7549 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007550 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007551 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007552 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007553
John Hawkes9c1cfda2005-09-06 15:18:14 -07007554 sd = &per_cpu(node_domains, j);
7555 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007556 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007557 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007558 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007559 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007560 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007561 prev = sg;
7562
7563 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007564 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007565 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007566 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007567
Mike Travis7c16ec52008-04-04 18:11:11 -07007568 cpus_complement(*notcovered, *covered);
7569 cpus_and(*tmpmask, *notcovered, *cpu_map);
7570 cpus_and(*tmpmask, *tmpmask, *domainspan);
7571 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007572 break;
7573
Mike Travis7c16ec52008-04-04 18:11:11 -07007574 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7575 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007576 continue;
7577
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007578 sg = kmalloc_node(sizeof(struct sched_group),
7579 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007580 if (!sg) {
7581 printk(KERN_WARNING
7582 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007583 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007584 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007585 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007586 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007587 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007588 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007589 prev->next = sg;
7590 prev = sg;
7591 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593#endif
7594
7595 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007596#ifdef CONFIG_SCHED_SMT
7597 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007598 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7599
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007600 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007601 }
7602#endif
7603#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007604 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007605 struct sched_domain *sd = &per_cpu(core_domains, i);
7606
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007607 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007608 }
7609#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007611 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007612 struct sched_domain *sd = &per_cpu(phys_domains, i);
7613
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007614 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 }
7616
John Hawkes9c1cfda2005-09-06 15:18:14 -07007617#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007618 for (i = 0; i < MAX_NUMNODES; i++)
7619 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007620
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007621 if (sd_allnodes) {
7622 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007623
Mike Travis7c16ec52008-04-04 18:11:11 -07007624 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7625 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007626 init_numa_sched_groups_power(sg);
7627 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007628#endif
7629
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007631 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 struct sched_domain *sd;
7633#ifdef CONFIG_SCHED_SMT
7634 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007635#elif defined(CONFIG_SCHED_MC)
7636 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637#else
7638 sd = &per_cpu(phys_domains, i);
7639#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007640 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007642
Mike Travis7c16ec52008-04-04 18:11:11 -07007643 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007644 return 0;
7645
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007646#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007647error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007648 free_sched_groups(cpu_map, tmpmask);
7649 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007650 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007651#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652}
Paul Jackson029190c2007-10-18 23:40:20 -07007653
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007654static int build_sched_domains(const cpumask_t *cpu_map)
7655{
7656 return __build_sched_domains(cpu_map, NULL);
7657}
7658
Paul Jackson029190c2007-10-18 23:40:20 -07007659static cpumask_t *doms_cur; /* current sched domains */
7660static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007661static struct sched_domain_attr *dattr_cur; /* attribues of custom domains
7662 in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007663
7664/*
7665 * Special case: If a kmalloc of a doms_cur partition (array of
7666 * cpumask_t) fails, then fallback to a single sched domain,
7667 * as determined by the single cpumask_t fallback_doms.
7668 */
7669static cpumask_t fallback_doms;
7670
Heiko Carstens22e52b02008-03-12 18:31:59 +01007671void __attribute__((weak)) arch_update_cpu_topology(void)
7672{
7673}
7674
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007675/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007676 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007677 * For now this just excludes isolated cpus, but could be used to
7678 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007679 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007680static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007681{
Milton Miller73785472007-10-24 18:23:48 +02007682 int err;
7683
Heiko Carstens22e52b02008-03-12 18:31:59 +01007684 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007685 ndoms_cur = 1;
7686 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7687 if (!doms_cur)
7688 doms_cur = &fallback_doms;
7689 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007690 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007691 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007692 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007693
7694 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007695}
7696
Mike Travis7c16ec52008-04-04 18:11:11 -07007697static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7698 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699{
Mike Travis7c16ec52008-04-04 18:11:11 -07007700 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007703/*
7704 * Detach sched domains from a group of cpus specified in cpu_map
7705 * These cpus will now be attached to the NULL domain
7706 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007707static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007708{
Mike Travis7c16ec52008-04-04 18:11:11 -07007709 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007710 int i;
7711
Milton Miller6382bc92007-10-15 17:00:19 +02007712 unregister_sched_domain_sysctl();
7713
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007714 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007715 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007716 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007717 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007718}
7719
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007720/* handle null as "default" */
7721static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7722 struct sched_domain_attr *new, int idx_new)
7723{
7724 struct sched_domain_attr tmp;
7725
7726 /* fast path */
7727 if (!new && !cur)
7728 return 1;
7729
7730 tmp = SD_ATTR_INIT;
7731 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7732 new ? (new + idx_new) : &tmp,
7733 sizeof(struct sched_domain_attr));
7734}
7735
Paul Jackson029190c2007-10-18 23:40:20 -07007736/*
7737 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007738 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007739 * doms_new[] to the current sched domain partitioning, doms_cur[].
7740 * It destroys each deleted domain and builds each new domain.
7741 *
7742 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007743 * The masks don't intersect (don't overlap.) We should setup one
7744 * sched domain for each mask. CPUs not in any of the cpumasks will
7745 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007746 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7747 * it as it is.
7748 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007749 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7750 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007751 * failed the kmalloc call, then it can pass in doms_new == NULL,
7752 * and partition_sched_domains() will fallback to the single partition
7753 * 'fallback_doms'.
7754 *
7755 * Call with hotplug lock held
7756 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007757void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7758 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007759{
7760 int i, j;
7761
Heiko Carstens712555e2008-04-28 11:33:07 +02007762 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007763
Milton Miller73785472007-10-24 18:23:48 +02007764 /* always unregister in case we don't destroy any domains */
7765 unregister_sched_domain_sysctl();
7766
Paul Jackson029190c2007-10-18 23:40:20 -07007767 if (doms_new == NULL) {
7768 ndoms_new = 1;
7769 doms_new = &fallback_doms;
7770 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007771 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007772 }
7773
7774 /* Destroy deleted domains */
7775 for (i = 0; i < ndoms_cur; i++) {
7776 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007777 if (cpus_equal(doms_cur[i], doms_new[j])
7778 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007779 goto match1;
7780 }
7781 /* no match - a current sched domain not in new doms_new[] */
7782 detach_destroy_domains(doms_cur + i);
7783match1:
7784 ;
7785 }
7786
7787 /* Build new domains */
7788 for (i = 0; i < ndoms_new; i++) {
7789 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007790 if (cpus_equal(doms_new[i], doms_cur[j])
7791 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007792 goto match2;
7793 }
7794 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007795 __build_sched_domains(doms_new + i,
7796 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007797match2:
7798 ;
7799 }
7800
7801 /* Remember the new sched domains */
7802 if (doms_cur != &fallback_doms)
7803 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007804 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007805 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007806 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007807 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007808
7809 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007810
Heiko Carstens712555e2008-04-28 11:33:07 +02007811 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007812}
7813
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007814#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007815int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007816{
7817 int err;
7818
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007819 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007820 mutex_lock(&sched_domains_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007821 detach_destroy_domains(&cpu_online_map);
7822 err = arch_init_sched_domains(&cpu_online_map);
Heiko Carstens712555e2008-04-28 11:33:07 +02007823 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007824 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007825
7826 return err;
7827}
7828
7829static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7830{
7831 int ret;
7832
7833 if (buf[0] != '0' && buf[0] != '1')
7834 return -EINVAL;
7835
7836 if (smt)
7837 sched_smt_power_savings = (buf[0] == '1');
7838 else
7839 sched_mc_power_savings = (buf[0] == '1');
7840
7841 ret = arch_reinit_sched_domains();
7842
7843 return ret ? ret : count;
7844}
7845
Adrian Bunk6707de002007-08-12 18:08:19 +02007846#ifdef CONFIG_SCHED_MC
7847static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7848{
7849 return sprintf(page, "%u\n", sched_mc_power_savings);
7850}
7851static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7852 const char *buf, size_t count)
7853{
7854 return sched_power_savings_store(buf, count, 0);
7855}
7856static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7857 sched_mc_power_savings_store);
7858#endif
7859
7860#ifdef CONFIG_SCHED_SMT
7861static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7862{
7863 return sprintf(page, "%u\n", sched_smt_power_savings);
7864}
7865static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7866 const char *buf, size_t count)
7867{
7868 return sched_power_savings_store(buf, count, 1);
7869}
7870static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7871 sched_smt_power_savings_store);
7872#endif
7873
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007874int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7875{
7876 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007877
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007878#ifdef CONFIG_SCHED_SMT
7879 if (smt_capable())
7880 err = sysfs_create_file(&cls->kset.kobj,
7881 &attr_sched_smt_power_savings.attr);
7882#endif
7883#ifdef CONFIG_SCHED_MC
7884 if (!err && mc_capable())
7885 err = sysfs_create_file(&cls->kset.kobj,
7886 &attr_sched_mc_power_savings.attr);
7887#endif
7888 return err;
7889}
7890#endif
7891
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007893 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007895 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896 * which will prevent rebalancing while the sched domains are recalculated.
7897 */
7898static int update_sched_domains(struct notifier_block *nfb,
7899 unsigned long action, void *hcpu)
7900{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 switch (action) {
7902 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007903 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007905 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007906 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907 return NOTIFY_OK;
7908
7909 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007910 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007912 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007914 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007916 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917 /*
7918 * Fall through and re-initialise the domains.
7919 */
7920 break;
7921 default:
7922 return NOTIFY_DONE;
7923 }
7924
7925 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007926 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927
7928 return NOTIFY_OK;
7929}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930
7931void __init sched_init_smp(void)
7932{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007933 cpumask_t non_isolated_cpus;
7934
Mike Travis434d53b2008-04-04 18:11:04 -07007935#if defined(CONFIG_NUMA)
7936 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7937 GFP_KERNEL);
7938 BUG_ON(sched_group_nodes_bycpu == NULL);
7939#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007940 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007941 mutex_lock(&sched_domains_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007942 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007943 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007944 if (cpus_empty(non_isolated_cpus))
7945 cpu_set(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007946 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007947 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948 /* XXX: Theoretical race here - CPU may be hotplugged now */
7949 hotcpu_notifier(update_sched_domains, 0);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007950 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007951
7952 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07007953 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007954 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007955 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956}
7957#else
7958void __init sched_init_smp(void)
7959{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007960 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961}
7962#endif /* CONFIG_SMP */
7963
7964int in_sched_functions(unsigned long addr)
7965{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966 return in_lock_functions(addr) ||
7967 (addr >= (unsigned long)__sched_text_start
7968 && addr < (unsigned long)__sched_text_end);
7969}
7970
Alexey Dobriyana9957442007-10-15 17:00:13 +02007971static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007972{
7973 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007974 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02007975#ifdef CONFIG_FAIR_GROUP_SCHED
7976 cfs_rq->rq = rq;
7977#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007978 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007979}
7980
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007981static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7982{
7983 struct rt_prio_array *array;
7984 int i;
7985
7986 array = &rt_rq->active;
7987 for (i = 0; i < MAX_RT_PRIO; i++) {
7988 INIT_LIST_HEAD(array->queue + i);
7989 __clear_bit(i, array->bitmap);
7990 }
7991 /* delimiter for bitsearch: */
7992 __set_bit(MAX_RT_PRIO, array->bitmap);
7993
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007994#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007995 rt_rq->highest_prio = MAX_RT_PRIO;
7996#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007997#ifdef CONFIG_SMP
7998 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007999 rt_rq->overloaded = 0;
8000#endif
8001
8002 rt_rq->rt_time = 0;
8003 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008004 rt_rq->rt_runtime = 0;
8005 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008006
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008007#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01008008 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008009 rt_rq->rq = rq;
8010#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008011}
8012
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008013#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008014static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
8015 struct sched_entity *se, int cpu, int add,
8016 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008017{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008018 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008019 tg->cfs_rq[cpu] = cfs_rq;
8020 init_cfs_rq(cfs_rq, rq);
8021 cfs_rq->tg = tg;
8022 if (add)
8023 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
8024
8025 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008026 /* se could be NULL for init_task_group */
8027 if (!se)
8028 return;
8029
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008030 if (!parent)
8031 se->cfs_rq = &rq->cfs;
8032 else
8033 se->cfs_rq = parent->my_q;
8034
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008035 se->my_q = cfs_rq;
8036 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008037 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008038 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008039}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008040#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008041
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008042#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008043static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8044 struct sched_rt_entity *rt_se, int cpu, int add,
8045 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008046{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008047 struct rq *rq = cpu_rq(cpu);
8048
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008049 tg->rt_rq[cpu] = rt_rq;
8050 init_rt_rq(rt_rq, rq);
8051 rt_rq->tg = tg;
8052 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008053 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008054 if (add)
8055 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8056
8057 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008058 if (!rt_se)
8059 return;
8060
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008061 if (!parent)
8062 rt_se->rt_rq = &rq->rt;
8063 else
8064 rt_se->rt_rq = parent->my_q;
8065
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008066 rt_se->rt_rq = &rq->rt;
8067 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008068 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008069 INIT_LIST_HEAD(&rt_se->run_list);
8070}
8071#endif
8072
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073void __init sched_init(void)
8074{
Ingo Molnardd41f592007-07-09 18:51:59 +02008075 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008076 unsigned long alloc_size = 0, ptr;
8077
8078#ifdef CONFIG_FAIR_GROUP_SCHED
8079 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8080#endif
8081#ifdef CONFIG_RT_GROUP_SCHED
8082 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8083#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008084#ifdef CONFIG_USER_SCHED
8085 alloc_size *= 2;
8086#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008087 /*
8088 * As sched_init() is called before page_alloc is setup,
8089 * we use alloc_bootmem().
8090 */
8091 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07008092 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07008093
8094#ifdef CONFIG_FAIR_GROUP_SCHED
8095 init_task_group.se = (struct sched_entity **)ptr;
8096 ptr += nr_cpu_ids * sizeof(void **);
8097
8098 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8099 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008100
8101#ifdef CONFIG_USER_SCHED
8102 root_task_group.se = (struct sched_entity **)ptr;
8103 ptr += nr_cpu_ids * sizeof(void **);
8104
8105 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8106 ptr += nr_cpu_ids * sizeof(void **);
8107#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008108#endif
8109#ifdef CONFIG_RT_GROUP_SCHED
8110 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8111 ptr += nr_cpu_ids * sizeof(void **);
8112
8113 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008114 ptr += nr_cpu_ids * sizeof(void **);
8115
8116#ifdef CONFIG_USER_SCHED
8117 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8118 ptr += nr_cpu_ids * sizeof(void **);
8119
8120 root_task_group.rt_rq = (struct rt_rq **)ptr;
8121 ptr += nr_cpu_ids * sizeof(void **);
8122#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008123#endif
8124 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008125
Gregory Haskins57d885f2008-01-25 21:08:18 +01008126#ifdef CONFIG_SMP
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008127 init_aggregate();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008128 init_defrootdomain();
8129#endif
8130
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008131 init_rt_bandwidth(&def_rt_bandwidth,
8132 global_rt_period(), global_rt_runtime());
8133
8134#ifdef CONFIG_RT_GROUP_SCHED
8135 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8136 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008137#ifdef CONFIG_USER_SCHED
8138 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8139 global_rt_period(), RUNTIME_INF);
8140#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008141#endif
8142
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008143#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008144 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008145 INIT_LIST_HEAD(&init_task_group.children);
8146
8147#ifdef CONFIG_USER_SCHED
8148 INIT_LIST_HEAD(&root_task_group.children);
8149 init_task_group.parent = &root_task_group;
8150 list_add(&init_task_group.siblings, &root_task_group.children);
8151#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008152#endif
8153
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008154 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008155 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156
8157 rq = cpu_rq(i);
8158 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07008159 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07008160 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008161 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008162 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008163#ifdef CONFIG_FAIR_GROUP_SCHED
8164 init_task_group.shares = init_task_group_load;
8165 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008166#ifdef CONFIG_CGROUP_SCHED
8167 /*
8168 * How much cpu bandwidth does init_task_group get?
8169 *
8170 * In case of task-groups formed thr' the cgroup filesystem, it
8171 * gets 100% of the cpu resources in the system. This overall
8172 * system cpu resource is divided among the tasks of
8173 * init_task_group and its child task-groups in a fair manner,
8174 * based on each entity's (task or task-group's) weight
8175 * (se->load.weight).
8176 *
8177 * In other words, if init_task_group has 10 tasks of weight
8178 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8179 * then A0's share of the cpu resource is:
8180 *
8181 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8182 *
8183 * We achieve this by letting init_task_group's tasks sit
8184 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8185 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008186 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008187#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008188 root_task_group.shares = NICE_0_LOAD;
8189 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008190 /*
8191 * In case of task-groups formed thr' the user id of tasks,
8192 * init_task_group represents tasks belonging to root user.
8193 * Hence it forms a sibling of all subsequent groups formed.
8194 * In this case, init_task_group gets only a fraction of overall
8195 * system cpu resource, based on the weight assigned to root
8196 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8197 * by letting tasks of init_task_group sit in a separate cfs_rq
8198 * (init_cfs_rq) and having one entity represent this group of
8199 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8200 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008201 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008202 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008203 &per_cpu(init_sched_entity, i), i, 1,
8204 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008205
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008206#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008207#endif /* CONFIG_FAIR_GROUP_SCHED */
8208
8209 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008210#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008211 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008212#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008213 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008214#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008215 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008216 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008217 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008218 &per_cpu(init_sched_rt_entity, i), i, 1,
8219 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008220#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008221#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222
Ingo Molnardd41f592007-07-09 18:51:59 +02008223 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8224 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008225#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008226 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008227 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008228 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008229 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008230 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008231 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008232 rq->migration_thread = NULL;
8233 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008234 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008235#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008236 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238 }
8239
Peter Williams2dd73a42006-06-27 02:54:34 -07008240 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008241
Avi Kivitye107be32007-07-26 13:40:43 +02008242#ifdef CONFIG_PREEMPT_NOTIFIERS
8243 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8244#endif
8245
Christoph Lameterc9819f42006-12-10 02:20:25 -08008246#ifdef CONFIG_SMP
8247 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8248#endif
8249
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008250#ifdef CONFIG_RT_MUTEXES
8251 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8252#endif
8253
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254 /*
8255 * The boot idle thread does lazy MMU switching as well:
8256 */
8257 atomic_inc(&init_mm.mm_count);
8258 enter_lazy_tlb(&init_mm, current);
8259
8260 /*
8261 * Make us the idle thread. Technically, schedule() should not be
8262 * called from this thread, however somewhere below it might be,
8263 * but because we are the idle thread, we just pick up running again
8264 * when this runqueue becomes "idle".
8265 */
8266 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008267 /*
8268 * During early bootup we pretend to be a normal task:
8269 */
8270 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008271
8272 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273}
8274
8275#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8276void __might_sleep(char *file, int line)
8277{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008278#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279 static unsigned long prev_jiffy; /* ratelimiting */
8280
8281 if ((in_atomic() || irqs_disabled()) &&
8282 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8283 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8284 return;
8285 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008286 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008287 " context at %s:%d\n", file, line);
8288 printk("in_atomic():%d, irqs_disabled():%d\n",
8289 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008290 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008291 if (irqs_disabled())
8292 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293 dump_stack();
8294 }
8295#endif
8296}
8297EXPORT_SYMBOL(__might_sleep);
8298#endif
8299
8300#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008301static void normalize_task(struct rq *rq, struct task_struct *p)
8302{
8303 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02008304
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008305 update_rq_clock(rq);
8306 on_rq = p->se.on_rq;
8307 if (on_rq)
8308 deactivate_task(rq, p, 0);
8309 __setscheduler(rq, p, SCHED_NORMAL, 0);
8310 if (on_rq) {
8311 activate_task(rq, p, 0);
8312 resched_task(rq->curr);
8313 }
8314}
8315
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316void normalize_rt_tasks(void)
8317{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008318 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008320 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008322 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008323 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008324 /*
8325 * Only normalize user tasks:
8326 */
8327 if (!p->mm)
8328 continue;
8329
Ingo Molnardd41f592007-07-09 18:51:59 +02008330 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008331#ifdef CONFIG_SCHEDSTATS
8332 p->se.wait_start = 0;
8333 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008334 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008335#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008336
8337 if (!rt_task(p)) {
8338 /*
8339 * Renice negative nice level userspace
8340 * tasks back to 0:
8341 */
8342 if (TASK_NICE(p) < 0 && p->mm)
8343 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008347 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008348 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008349
Ingo Molnar178be792007-10-15 17:00:18 +02008350 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008351
Ingo Molnarb29739f2006-06-27 02:54:51 -07008352 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008353 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008354 } while_each_thread(g, p);
8355
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008356 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357}
8358
8359#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008360
8361#ifdef CONFIG_IA64
8362/*
8363 * These functions are only useful for the IA64 MCA handling.
8364 *
8365 * They can only be called when the whole system has been
8366 * stopped - every CPU needs to be quiescent, and no scheduling
8367 * activity can take place. Using them for anything else would
8368 * be a serious bug, and as a result, they aren't even visible
8369 * under any other configuration.
8370 */
8371
8372/**
8373 * curr_task - return the current task for a given cpu.
8374 * @cpu: the processor in question.
8375 *
8376 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8377 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008378struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008379{
8380 return cpu_curr(cpu);
8381}
8382
8383/**
8384 * set_curr_task - set the current task for a given cpu.
8385 * @cpu: the processor in question.
8386 * @p: the task pointer to set.
8387 *
8388 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008389 * are serviced on a separate stack. It allows the architecture to switch the
8390 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008391 * must be called with all CPU's synchronized, and interrupts disabled, the
8392 * and caller must save the original value of the current task (see
8393 * curr_task() above) and restore that value before reenabling interrupts and
8394 * re-starting the system.
8395 *
8396 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8397 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008398void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008399{
8400 cpu_curr(cpu) = p;
8401}
8402
8403#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008404
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008405#ifdef CONFIG_FAIR_GROUP_SCHED
8406static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008407{
8408 int i;
8409
8410 for_each_possible_cpu(i) {
8411 if (tg->cfs_rq)
8412 kfree(tg->cfs_rq[i]);
8413 if (tg->se)
8414 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008415 }
8416
8417 kfree(tg->cfs_rq);
8418 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008419}
8420
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008421static
8422int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008423{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008424 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008425 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008426 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008427 int i;
8428
Mike Travis434d53b2008-04-04 18:11:04 -07008429 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008430 if (!tg->cfs_rq)
8431 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008432 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008433 if (!tg->se)
8434 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008435
8436 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008437
8438 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008439 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008440
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008441 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8442 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008443 if (!cfs_rq)
8444 goto err;
8445
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008446 se = kmalloc_node(sizeof(struct sched_entity),
8447 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008448 if (!se)
8449 goto err;
8450
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008451 parent_se = parent ? parent->se[i] : NULL;
8452 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008453 }
8454
8455 return 1;
8456
8457 err:
8458 return 0;
8459}
8460
8461static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8462{
8463 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8464 &cpu_rq(cpu)->leaf_cfs_rq_list);
8465}
8466
8467static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8468{
8469 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8470}
8471#else
8472static inline void free_fair_sched_group(struct task_group *tg)
8473{
8474}
8475
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008476static inline
8477int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008478{
8479 return 1;
8480}
8481
8482static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8483{
8484}
8485
8486static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8487{
8488}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008489#endif
8490
8491#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008492static void free_rt_sched_group(struct task_group *tg)
8493{
8494 int i;
8495
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008496 destroy_rt_bandwidth(&tg->rt_bandwidth);
8497
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008498 for_each_possible_cpu(i) {
8499 if (tg->rt_rq)
8500 kfree(tg->rt_rq[i]);
8501 if (tg->rt_se)
8502 kfree(tg->rt_se[i]);
8503 }
8504
8505 kfree(tg->rt_rq);
8506 kfree(tg->rt_se);
8507}
8508
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008509static
8510int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008511{
8512 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008513 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008514 struct rq *rq;
8515 int i;
8516
Mike Travis434d53b2008-04-04 18:11:04 -07008517 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008518 if (!tg->rt_rq)
8519 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008520 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008521 if (!tg->rt_se)
8522 goto err;
8523
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008524 init_rt_bandwidth(&tg->rt_bandwidth,
8525 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008526
8527 for_each_possible_cpu(i) {
8528 rq = cpu_rq(i);
8529
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008530 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8531 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8532 if (!rt_rq)
8533 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008534
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008535 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8536 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8537 if (!rt_se)
8538 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008539
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008540 parent_se = parent ? parent->rt_se[i] : NULL;
8541 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008542 }
8543
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008544 return 1;
8545
8546 err:
8547 return 0;
8548}
8549
8550static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8551{
8552 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8553 &cpu_rq(cpu)->leaf_rt_rq_list);
8554}
8555
8556static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8557{
8558 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8559}
8560#else
8561static inline void free_rt_sched_group(struct task_group *tg)
8562{
8563}
8564
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008565static inline
8566int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008567{
8568 return 1;
8569}
8570
8571static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8572{
8573}
8574
8575static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8576{
8577}
8578#endif
8579
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008580#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008581static void free_sched_group(struct task_group *tg)
8582{
8583 free_fair_sched_group(tg);
8584 free_rt_sched_group(tg);
8585 kfree(tg);
8586}
8587
8588/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008589struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008590{
8591 struct task_group *tg;
8592 unsigned long flags;
8593 int i;
8594
8595 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8596 if (!tg)
8597 return ERR_PTR(-ENOMEM);
8598
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008599 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008600 goto err;
8601
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008602 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008603 goto err;
8604
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008605 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008606 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008607 register_fair_sched_group(tg, i);
8608 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008609 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008610 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008611
8612 WARN_ON(!parent); /* root should already exist */
8613
8614 tg->parent = parent;
8615 list_add_rcu(&tg->siblings, &parent->children);
8616 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008617 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008618
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008619 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008620
8621err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008622 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008623 return ERR_PTR(-ENOMEM);
8624}
8625
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008626/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008627static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008628{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008629 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008630 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008631}
8632
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008633/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008634void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008635{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008636 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008637 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008638
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008639 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008640 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008641 unregister_fair_sched_group(tg, i);
8642 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008643 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008644 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008645 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008646 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008647
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008648 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008649 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008650}
8651
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008652/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008653 * The caller of this function should have put the task in its new group
8654 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8655 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008656 */
8657void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008658{
8659 int on_rq, running;
8660 unsigned long flags;
8661 struct rq *rq;
8662
8663 rq = task_rq_lock(tsk, &flags);
8664
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008665 update_rq_clock(rq);
8666
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008667 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008668 on_rq = tsk->se.on_rq;
8669
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008670 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008671 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008672 if (unlikely(running))
8673 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008674
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008675 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008676
Peter Zijlstra810b3812008-02-29 15:21:01 -05008677#ifdef CONFIG_FAIR_GROUP_SCHED
8678 if (tsk->sched_class->moved_group)
8679 tsk->sched_class->moved_group(tsk);
8680#endif
8681
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008682 if (unlikely(running))
8683 tsk->sched_class->set_curr_task(rq);
8684 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008685 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008686
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008687 task_rq_unlock(rq, &flags);
8688}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008689#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008690
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008691#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008692static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008693{
8694 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008695 int on_rq;
8696
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008697 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008698 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008699 dequeue_entity(cfs_rq, se, 0);
8700
8701 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008702 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008703
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008704 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008705 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008706}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008707
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008708static void set_se_shares(struct sched_entity *se, unsigned long shares)
8709{
8710 struct cfs_rq *cfs_rq = se->cfs_rq;
8711 struct rq *rq = cfs_rq->rq;
8712 unsigned long flags;
8713
8714 spin_lock_irqsave(&rq->lock, flags);
8715 __set_se_shares(se, shares);
8716 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008717}
8718
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008719static DEFINE_MUTEX(shares_mutex);
8720
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008721int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008722{
8723 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008724 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008725
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008726 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008727 * We can't change the weight of the root cgroup.
8728 */
8729 if (!tg->se[0])
8730 return -EINVAL;
8731
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008732 if (shares < MIN_SHARES)
8733 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008734 else if (shares > MAX_SHARES)
8735 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008736
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008737 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008738 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008739 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008740
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008741 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008742 for_each_possible_cpu(i)
8743 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008744 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008745 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008746
8747 /* wait for any ongoing reference to this group to finish */
8748 synchronize_sched();
8749
8750 /*
8751 * Now we are free to modify the group's share on each cpu
8752 * w/o tripping rebalance_share or load_balance_fair.
8753 */
8754 tg->shares = shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008755 for_each_possible_cpu(i) {
8756 /*
8757 * force a rebalance
8758 */
8759 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008760 set_se_shares(tg->se[i], shares);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008761 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008762
8763 /*
8764 * Enable load balance activity on this group, by inserting it back on
8765 * each cpu's rq->leaf_cfs_rq_list.
8766 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008767 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008768 for_each_possible_cpu(i)
8769 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008770 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008771 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008772done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008773 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008774 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008775}
8776
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008777unsigned long sched_group_shares(struct task_group *tg)
8778{
8779 return tg->shares;
8780}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008781#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008782
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008783#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008784/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008785 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008786 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008787static DEFINE_MUTEX(rt_constraints_mutex);
8788
8789static unsigned long to_ratio(u64 period, u64 runtime)
8790{
8791 if (runtime == RUNTIME_INF)
8792 return 1ULL << 16;
8793
Roman Zippel6f6d6a12008-05-01 04:34:28 -07008794 return div64_u64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008795}
8796
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008797#ifdef CONFIG_CGROUP_SCHED
8798static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8799{
8800 struct task_group *tgi, *parent = tg->parent;
8801 unsigned long total = 0;
8802
8803 if (!parent) {
8804 if (global_rt_period() < period)
8805 return 0;
8806
8807 return to_ratio(period, runtime) <
8808 to_ratio(global_rt_period(), global_rt_runtime());
8809 }
8810
8811 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8812 return 0;
8813
8814 rcu_read_lock();
8815 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8816 if (tgi == tg)
8817 continue;
8818
8819 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8820 tgi->rt_bandwidth.rt_runtime);
8821 }
8822 rcu_read_unlock();
8823
8824 return total + to_ratio(period, runtime) <
8825 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8826 parent->rt_bandwidth.rt_runtime);
8827}
8828#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008829static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008830{
8831 struct task_group *tgi;
8832 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008833 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008834 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008835
8836 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008837 list_for_each_entry_rcu(tgi, &task_groups, list) {
8838 if (tgi == tg)
8839 continue;
8840
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008841 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8842 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008843 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008844 rcu_read_unlock();
8845
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008846 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008847}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008848#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008849
Dhaval Giani521f1a242008-02-28 15:21:56 +05308850/* Must be called with tasklist_lock held */
8851static inline int tg_has_rt_tasks(struct task_group *tg)
8852{
8853 struct task_struct *g, *p;
8854 do_each_thread(g, p) {
8855 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8856 return 1;
8857 } while_each_thread(g, p);
8858 return 0;
8859}
8860
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008861static int tg_set_bandwidth(struct task_group *tg,
8862 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008863{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008864 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008865
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008866 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308867 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008868 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308869 err = -EBUSY;
8870 goto unlock;
8871 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008872 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8873 err = -EINVAL;
8874 goto unlock;
8875 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008876
8877 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008878 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8879 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008880
8881 for_each_possible_cpu(i) {
8882 struct rt_rq *rt_rq = tg->rt_rq[i];
8883
8884 spin_lock(&rt_rq->rt_runtime_lock);
8885 rt_rq->rt_runtime = rt_runtime;
8886 spin_unlock(&rt_rq->rt_runtime_lock);
8887 }
8888 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008889 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308890 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008891 mutex_unlock(&rt_constraints_mutex);
8892
8893 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008894}
8895
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008896int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8897{
8898 u64 rt_runtime, rt_period;
8899
8900 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8901 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8902 if (rt_runtime_us < 0)
8903 rt_runtime = RUNTIME_INF;
8904
8905 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8906}
8907
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008908long sched_group_rt_runtime(struct task_group *tg)
8909{
8910 u64 rt_runtime_us;
8911
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008912 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008913 return -1;
8914
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008915 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008916 do_div(rt_runtime_us, NSEC_PER_USEC);
8917 return rt_runtime_us;
8918}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008919
8920int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8921{
8922 u64 rt_runtime, rt_period;
8923
8924 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8925 rt_runtime = tg->rt_bandwidth.rt_runtime;
8926
8927 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8928}
8929
8930long sched_group_rt_period(struct task_group *tg)
8931{
8932 u64 rt_period_us;
8933
8934 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8935 do_div(rt_period_us, NSEC_PER_USEC);
8936 return rt_period_us;
8937}
8938
8939static int sched_rt_global_constraints(void)
8940{
8941 int ret = 0;
8942
8943 mutex_lock(&rt_constraints_mutex);
8944 if (!__rt_schedulable(NULL, 1, 0))
8945 ret = -EINVAL;
8946 mutex_unlock(&rt_constraints_mutex);
8947
8948 return ret;
8949}
8950#else
8951static int sched_rt_global_constraints(void)
8952{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008953 unsigned long flags;
8954 int i;
8955
8956 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
8957 for_each_possible_cpu(i) {
8958 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8959
8960 spin_lock(&rt_rq->rt_runtime_lock);
8961 rt_rq->rt_runtime = global_rt_runtime();
8962 spin_unlock(&rt_rq->rt_runtime_lock);
8963 }
8964 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
8965
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008966 return 0;
8967}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008968#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008969
8970int sched_rt_handler(struct ctl_table *table, int write,
8971 struct file *filp, void __user *buffer, size_t *lenp,
8972 loff_t *ppos)
8973{
8974 int ret;
8975 int old_period, old_runtime;
8976 static DEFINE_MUTEX(mutex);
8977
8978 mutex_lock(&mutex);
8979 old_period = sysctl_sched_rt_period;
8980 old_runtime = sysctl_sched_rt_runtime;
8981
8982 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8983
8984 if (!ret && write) {
8985 ret = sched_rt_global_constraints();
8986 if (ret) {
8987 sysctl_sched_rt_period = old_period;
8988 sysctl_sched_rt_runtime = old_runtime;
8989 } else {
8990 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8991 def_rt_bandwidth.rt_period =
8992 ns_to_ktime(global_rt_period());
8993 }
8994 }
8995 mutex_unlock(&mutex);
8996
8997 return ret;
8998}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008999
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009000#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009001
9002/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009003static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009004{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009005 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
9006 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009007}
9008
9009static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02009010cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009011{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009012 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009013
Paul Menage2b01dfe2007-10-24 18:23:50 +02009014 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009015 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009016 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009017 return &init_task_group.css;
9018 }
9019
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009020 parent = cgroup_tg(cgrp->parent);
9021 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009022 if (IS_ERR(tg))
9023 return ERR_PTR(-ENOMEM);
9024
9025 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009026 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009027
9028 return &tg->css;
9029}
9030
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009031static void
9032cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009033{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009034 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009035
9036 sched_destroy_group(tg);
9037}
9038
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009039static int
9040cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9041 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009042{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009043#ifdef CONFIG_RT_GROUP_SCHED
9044 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009045 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009046 return -EINVAL;
9047#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009048 /* We don't support RT-tasks being in separate groups */
9049 if (tsk->sched_class != &fair_sched_class)
9050 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009051#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009052
9053 return 0;
9054}
9055
9056static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009057cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009058 struct cgroup *old_cont, struct task_struct *tsk)
9059{
9060 sched_move_task(tsk);
9061}
9062
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009063#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009064static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009065 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009066{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009067 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009068}
9069
Paul Menagef4c753b2008-04-29 00:59:56 -07009070static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009071{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009072 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009073
9074 return (u64) tg->shares;
9075}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009076#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009077
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009078#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07009079static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009080 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009081{
Paul Menage06ecb272008-04-29 01:00:06 -07009082 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009083}
9084
Paul Menage06ecb272008-04-29 01:00:06 -07009085static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009086{
Paul Menage06ecb272008-04-29 01:00:06 -07009087 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009088}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009089
9090static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9091 u64 rt_period_us)
9092{
9093 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9094}
9095
9096static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9097{
9098 return sched_group_rt_period(cgroup_tg(cgrp));
9099}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009100#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009101
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009102static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009103#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009104 {
9105 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009106 .read_u64 = cpu_shares_read_u64,
9107 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009108 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009109#endif
9110#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009111 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009112 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009113 .read_s64 = cpu_rt_runtime_read,
9114 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009115 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009116 {
9117 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009118 .read_u64 = cpu_rt_period_read_uint,
9119 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009120 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009121#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009122};
9123
9124static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9125{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009126 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009127}
9128
9129struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009130 .name = "cpu",
9131 .create = cpu_cgroup_create,
9132 .destroy = cpu_cgroup_destroy,
9133 .can_attach = cpu_cgroup_can_attach,
9134 .attach = cpu_cgroup_attach,
9135 .populate = cpu_cgroup_populate,
9136 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009137 .early_init = 1,
9138};
9139
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009140#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009141
9142#ifdef CONFIG_CGROUP_CPUACCT
9143
9144/*
9145 * CPU accounting code for task groups.
9146 *
9147 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9148 * (balbir@in.ibm.com).
9149 */
9150
9151/* track cpu usage of a group of tasks */
9152struct cpuacct {
9153 struct cgroup_subsys_state css;
9154 /* cpuusage holds pointer to a u64-type object on every cpu */
9155 u64 *cpuusage;
9156};
9157
9158struct cgroup_subsys cpuacct_subsys;
9159
9160/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309161static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009162{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309163 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009164 struct cpuacct, css);
9165}
9166
9167/* return cpu accounting group to which this task belongs */
9168static inline struct cpuacct *task_ca(struct task_struct *tsk)
9169{
9170 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9171 struct cpuacct, css);
9172}
9173
9174/* create a new cpu accounting group */
9175static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309176 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009177{
9178 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9179
9180 if (!ca)
9181 return ERR_PTR(-ENOMEM);
9182
9183 ca->cpuusage = alloc_percpu(u64);
9184 if (!ca->cpuusage) {
9185 kfree(ca);
9186 return ERR_PTR(-ENOMEM);
9187 }
9188
9189 return &ca->css;
9190}
9191
9192/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009193static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309194cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009195{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309196 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009197
9198 free_percpu(ca->cpuusage);
9199 kfree(ca);
9200}
9201
9202/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309203static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009204{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309205 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009206 u64 totalcpuusage = 0;
9207 int i;
9208
9209 for_each_possible_cpu(i) {
9210 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9211
9212 /*
9213 * Take rq->lock to make 64-bit addition safe on 32-bit
9214 * platforms.
9215 */
9216 spin_lock_irq(&cpu_rq(i)->lock);
9217 totalcpuusage += *cpuusage;
9218 spin_unlock_irq(&cpu_rq(i)->lock);
9219 }
9220
9221 return totalcpuusage;
9222}
9223
Dhaval Giani0297b802008-02-29 10:02:44 +05309224static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9225 u64 reset)
9226{
9227 struct cpuacct *ca = cgroup_ca(cgrp);
9228 int err = 0;
9229 int i;
9230
9231 if (reset) {
9232 err = -EINVAL;
9233 goto out;
9234 }
9235
9236 for_each_possible_cpu(i) {
9237 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9238
9239 spin_lock_irq(&cpu_rq(i)->lock);
9240 *cpuusage = 0;
9241 spin_unlock_irq(&cpu_rq(i)->lock);
9242 }
9243out:
9244 return err;
9245}
9246
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009247static struct cftype files[] = {
9248 {
9249 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009250 .read_u64 = cpuusage_read,
9251 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009252 },
9253};
9254
Dhaval Giani32cd7562008-02-29 10:02:43 +05309255static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009256{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309257 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009258}
9259
9260/*
9261 * charge this task's execution time to its accounting group.
9262 *
9263 * called with rq->lock held.
9264 */
9265static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9266{
9267 struct cpuacct *ca;
9268
9269 if (!cpuacct_subsys.active)
9270 return;
9271
9272 ca = task_ca(tsk);
9273 if (ca) {
9274 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9275
9276 *cpuusage += cputime;
9277 }
9278}
9279
9280struct cgroup_subsys cpuacct_subsys = {
9281 .name = "cpuacct",
9282 .create = cpuacct_create,
9283 .destroy = cpuacct_destroy,
9284 .populate = cpuacct_populate,
9285 .subsys_id = cpuacct_subsys_id,
9286};
9287#endif /* CONFIG_CGROUP_CPUACCT */