blob: e2f85c7a7476291fb2f3228ecf13897dad359449 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Eric Dumazet5517d862007-05-08 00:32:57 -070071#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020072#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080075 * Scheduler clock - returns current time in nanosec units.
76 * This is default implementation.
77 * Architectures and sub-architectures can override this.
78 */
79unsigned long long __attribute__((weak)) sched_clock(void)
80{
Eric Dumazetd6322fa2007-11-09 22:39:38 +010081 return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080082}
83
84/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * Convert user-nice values [ -20 ... 0 ... 19 ]
86 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
87 * and back.
88 */
89#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
90#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
91#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
92
93/*
94 * 'User priority' is the nice value converted to something we
95 * can work with better when scaling various scheduler parameters,
96 * it's a [ 0 ... 39 ] range.
97 */
98#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
99#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
100#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
101
102/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100103 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100105#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200107#define NICE_0_LOAD SCHED_LOAD_SCALE
108#define NICE_0_SHIFT SCHED_LOAD_SHIFT
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * These are the 'tuning knobs' of the scheduler:
112 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200113 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * Timeslices get refilled after they expire.
115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700117
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200118/*
119 * single value that denotes runtime == period, ie unlimited time.
120 */
121#define RUNTIME_INF ((u64)~0ULL)
122
Eric Dumazet5517d862007-05-08 00:32:57 -0700123#ifdef CONFIG_SMP
124/*
125 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
126 * Since cpu_power is a 'constant', we can use a reciprocal divide.
127 */
128static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
129{
130 return reciprocal_divide(load, sg->reciprocal_cpu_power);
131}
132
133/*
134 * Each time a sched group cpu_power is changed,
135 * we must compute its reciprocal value
136 */
137static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
138{
139 sg->__cpu_power += val;
140 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
141}
142#endif
143
Ingo Molnare05606d2007-07-09 18:51:59 +0200144static inline int rt_policy(int policy)
145{
146 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
147 return 1;
148 return 0;
149}
150
151static inline int task_has_rt_policy(struct task_struct *p)
152{
153 return rt_policy(p->policy);
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200157 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200159struct rt_prio_array {
160 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
161 struct list_head queue[MAX_RT_PRIO];
162};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200164struct rt_bandwidth {
165 ktime_t rt_period;
166 u64 rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200167 spinlock_t rt_runtime_lock;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200168 struct hrtimer rt_period_timer;
169};
170
171static struct rt_bandwidth def_rt_bandwidth;
172
173static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
174
175static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
176{
177 struct rt_bandwidth *rt_b =
178 container_of(timer, struct rt_bandwidth, rt_period_timer);
179 ktime_t now;
180 int overrun;
181 int idle = 0;
182
183 for (;;) {
184 now = hrtimer_cb_get_time(timer);
185 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
186
187 if (!overrun)
188 break;
189
190 idle = do_sched_rt_period_timer(rt_b, overrun);
191 }
192
193 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
194}
195
196static
197void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
198{
199 rt_b->rt_period = ns_to_ktime(period);
200 rt_b->rt_runtime = runtime;
201
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200202 spin_lock_init(&rt_b->rt_runtime_lock);
203
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200204 hrtimer_init(&rt_b->rt_period_timer,
205 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
206 rt_b->rt_period_timer.function = sched_rt_period_timer;
207 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
208}
209
210static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
211{
212 ktime_t now;
213
214 if (rt_b->rt_runtime == RUNTIME_INF)
215 return;
216
217 if (hrtimer_active(&rt_b->rt_period_timer))
218 return;
219
220 spin_lock(&rt_b->rt_runtime_lock);
221 for (;;) {
222 if (hrtimer_active(&rt_b->rt_period_timer))
223 break;
224
225 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
226 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
227 hrtimer_start(&rt_b->rt_period_timer,
228 rt_b->rt_period_timer.expires,
229 HRTIMER_MODE_ABS);
230 }
231 spin_unlock(&rt_b->rt_runtime_lock);
232}
233
234#ifdef CONFIG_RT_GROUP_SCHED
235static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
236{
237 hrtimer_cancel(&rt_b->rt_period_timer);
238}
239#endif
240
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100241#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200242
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700243#include <linux/cgroup.h>
244
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200245struct cfs_rq;
246
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100247static LIST_HEAD(task_groups);
248
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200249/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200250struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100251#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700252 struct cgroup_subsys_state css;
253#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100254
255#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200256 /* schedulable entities of this group on each cpu */
257 struct sched_entity **se;
258 /* runqueue "owned" by this group on each cpu */
259 struct cfs_rq **cfs_rq;
260 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100261#endif
262
263#ifdef CONFIG_RT_GROUP_SCHED
264 struct sched_rt_entity **rt_se;
265 struct rt_rq **rt_rq;
266
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200267 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100268#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100269
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100270 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100271 struct list_head list;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200272};
273
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100274#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200275/* Default task group's sched entity on each cpu */
276static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
277/* Default task group's cfs_rq on each cpu */
278static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
279
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100280static struct sched_entity *init_sched_entity_p[NR_CPUS];
281static struct cfs_rq *init_cfs_rq_p[NR_CPUS];
282#endif
283
284#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100285static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
286static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
287
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100288static struct sched_rt_entity *init_sched_rt_entity_p[NR_CPUS];
289static struct rt_rq *init_rt_rq_p[NR_CPUS];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100290#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100291
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100292/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100293 * a task group's cpu shares.
294 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100295static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100296
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100297/* doms_cur_mutex serializes access to doms_cur[] array */
298static DEFINE_MUTEX(doms_cur_mutex);
299
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100300#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100301#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100302# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200303#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100304# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200305#endif
306
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100307static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100308#endif
309
310/* Default task group.
311 * Every task in system belong to this group at bootup.
312 */
313struct task_group init_task_group = {
314#ifdef CONFIG_FAIR_GROUP_SCHED
315 .se = init_sched_entity_p,
316 .cfs_rq = init_cfs_rq_p,
317#endif
318
319#ifdef CONFIG_RT_GROUP_SCHED
320 .rt_se = init_sched_rt_entity_p,
321 .rt_rq = init_rt_rq_p,
322#endif
323};
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200324
325/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200326static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200327{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200328 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200329
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100330#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200331 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100332#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700333 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
334 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200335#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100336 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200337#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200338 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200339}
340
341/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100342static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200343{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100344#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100345 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
346 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100347#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100348
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100349#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100350 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
351 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100352#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200353}
354
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100355static inline void lock_doms_cur(void)
356{
357 mutex_lock(&doms_cur_mutex);
358}
359
360static inline void unlock_doms_cur(void)
361{
362 mutex_unlock(&doms_cur_mutex);
363}
364
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200365#else
366
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100367static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100368static inline void lock_doms_cur(void) { }
369static inline void unlock_doms_cur(void) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200370
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100371#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200372
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200373/* CFS-related fields in a runqueue */
374struct cfs_rq {
375 struct load_weight load;
376 unsigned long nr_running;
377
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200378 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200379 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200380
381 struct rb_root tasks_timeline;
382 struct rb_node *rb_leftmost;
383 struct rb_node *rb_load_balance_curr;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200384 /* 'curr' points to currently running entity on this cfs_rq.
385 * It is set to NULL otherwise (i.e when none are currently running).
386 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100387 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200388
389 unsigned long nr_spread_over;
390
Ingo Molnar62160e32007-10-15 17:00:03 +0200391#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200392 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
393
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100394 /*
395 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200396 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
397 * (like users, containers etc.)
398 *
399 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
400 * list is used during load balance.
401 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100402 struct list_head leaf_cfs_rq_list;
403 struct task_group *tg; /* group that "owns" this runqueue */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200404#endif
405};
406
407/* Real-Time classes' related field in a runqueue: */
408struct rt_rq {
409 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100410 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100411#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100412 int highest_prio; /* highest queued rt task prio */
413#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100414#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100415 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100416 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100417#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100418 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100419 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200420 u64 rt_runtime;
421 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100422
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100423#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100424 unsigned long rt_nr_boosted;
425
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100426 struct rq *rq;
427 struct list_head leaf_rt_rq_list;
428 struct task_group *tg;
429 struct sched_rt_entity *rt_se;
430#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200431};
432
Gregory Haskins57d885f2008-01-25 21:08:18 +0100433#ifdef CONFIG_SMP
434
435/*
436 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100437 * variables. Each exclusive cpuset essentially defines an island domain by
438 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100439 * exclusive cpuset is created, we also create and attach a new root-domain
440 * object.
441 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100442 */
443struct root_domain {
444 atomic_t refcount;
445 cpumask_t span;
446 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100447
Ingo Molnar0eab9142008-01-25 21:08:19 +0100448 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100449 * The "RT overload" flag: it gets set if a CPU has more than
450 * one runnable RT task.
451 */
452 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100453 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100454};
455
Gregory Haskinsdc938522008-01-25 21:08:26 +0100456/*
457 * By default the system creates a single root-domain with all cpus as
458 * members (mimicking the global state we have today).
459 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100460static struct root_domain def_root_domain;
461
462#endif
463
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200464/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * This is the main, per-CPU runqueue data structure.
466 *
467 * Locking rule: those places that want to lock multiple runqueues
468 * (such as the load balancing or the thread migration code), lock
469 * acquire operations must be ordered by ascending &runqueue.
470 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700471struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200472 /* runqueue lock: */
473 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /*
476 * nr_running and cpu_load should be in the same cacheline because
477 * remote CPUs use both these fields when doing load calculation.
478 */
479 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200480 #define CPU_LOAD_IDX_MAX 5
481 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700482 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700483#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200484 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700485 unsigned char in_nohz_recently;
486#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200487 /* capture load from *all* tasks on this cpu: */
488 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200489 unsigned long nr_load_updates;
490 u64 nr_switches;
491
492 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100493 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100494
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200495#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200496 /* list of leaf cfs_rq on this cpu: */
497 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100498#endif
499#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100500 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 /*
504 * This is part of a global counter where only the total sum
505 * over all CPUs matters. A task can increase this counter on
506 * one CPU and if it got migrated afterwards it may decrease
507 * it on another CPU. Always updated under the runqueue lock:
508 */
509 unsigned long nr_uninterruptible;
510
Ingo Molnar36c8b582006-07-03 00:25:41 -0700511 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800512 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200514
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200515 u64 clock, prev_clock_raw;
516 s64 clock_max_delta;
517
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100518 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200519 u64 idle_clock;
520 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200521 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 atomic_t nr_iowait;
524
525#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100526 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct sched_domain *sd;
528
529 /* For active balancing */
530 int active_balance;
531 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200532 /* cpu of this runqueue: */
533 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Ingo Molnar36c8b582006-07-03 00:25:41 -0700535 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 struct list_head migration_queue;
537#endif
538
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100539#ifdef CONFIG_SCHED_HRTICK
540 unsigned long hrtick_flags;
541 ktime_t hrtick_expire;
542 struct hrtimer hrtick_timer;
543#endif
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545#ifdef CONFIG_SCHEDSTATS
546 /* latency stats */
547 struct sched_info rq_sched_info;
548
549 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200550 unsigned int yld_exp_empty;
551 unsigned int yld_act_empty;
552 unsigned int yld_both_empty;
553 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200556 unsigned int sched_switch;
557 unsigned int sched_count;
558 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200561 unsigned int ttwu_count;
562 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200563
564 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200565 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700567 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568};
569
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700570static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Ingo Molnardd41f592007-07-09 18:51:59 +0200572static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
573{
574 rq->curr->sched_class->check_preempt_curr(rq, p);
575}
576
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700577static inline int cpu_of(struct rq *rq)
578{
579#ifdef CONFIG_SMP
580 return rq->cpu;
581#else
582 return 0;
583#endif
584}
585
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200586#ifdef CONFIG_NO_HZ
587static inline bool nohz_on(int cpu)
588{
589 return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE;
590}
591
592static inline u64 max_skipped_ticks(struct rq *rq)
593{
594 return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1;
595}
596
597static inline void update_last_tick_seen(struct rq *rq)
598{
599 rq->last_tick_seen = jiffies;
600}
601#else
602static inline u64 max_skipped_ticks(struct rq *rq)
603{
604 return 1;
605}
606
607static inline void update_last_tick_seen(struct rq *rq)
608{
609}
610#endif
611
Nick Piggin674311d2005-06-25 14:57:27 -0700612/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200613 * Update the per-runqueue clock, as finegrained as the platform can give
614 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200615 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200616static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200617{
618 u64 prev_raw = rq->prev_clock_raw;
619 u64 now = sched_clock();
620 s64 delta = now - prev_raw;
621 u64 clock = rq->clock;
622
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200623#ifdef CONFIG_SCHED_DEBUG
624 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
625#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200626 /*
627 * Protect against sched_clock() occasionally going backwards:
628 */
629 if (unlikely(delta < 0)) {
630 clock++;
631 rq->clock_warps++;
632 } else {
633 /*
634 * Catch too large forward jumps too:
635 */
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200636 u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC;
637 u64 max_time = rq->tick_timestamp + max_jump;
638
639 if (unlikely(clock + delta > max_time)) {
640 if (clock < max_time)
641 clock = max_time;
Ingo Molnar529c7722007-08-10 23:05:11 +0200642 else
643 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200644 rq->clock_overflows++;
645 } else {
646 if (unlikely(delta > rq->clock_max_delta))
647 rq->clock_max_delta = delta;
648 clock += delta;
649 }
650 }
651
652 rq->prev_clock_raw = now;
653 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200654}
655
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200656static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200657{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200658 if (likely(smp_processor_id() == cpu_of(rq)))
659 __update_rq_clock(rq);
660}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200661
Ingo Molnar20d315d2007-07-09 18:51:58 +0200662/*
Nick Piggin674311d2005-06-25 14:57:27 -0700663 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700664 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700665 *
666 * The domain tree of any CPU may only be accessed from within
667 * preempt-disabled sections.
668 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700669#define for_each_domain(cpu, __sd) \
670 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
673#define this_rq() (&__get_cpu_var(runqueues))
674#define task_rq(p) cpu_rq(task_cpu(p))
675#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
676
Ingo Molnare436d802007-07-19 21:28:35 +0200677/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200678 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
679 */
680#ifdef CONFIG_SCHED_DEBUG
681# define const_debug __read_mostly
682#else
683# define const_debug static const
684#endif
685
686/*
687 * Debugging: various feature bits
688 */
689enum {
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200690 SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
Ingo Molnar96126332007-11-15 20:57:40 +0100691 SCHED_FEAT_WAKEUP_PREEMPT = 2,
692 SCHED_FEAT_START_DEBIT = 4,
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100693 SCHED_FEAT_AFFINE_WAKEUPS = 8,
694 SCHED_FEAT_CACHE_HOT_BUDDY = 16,
Ingo Molnar02e2b832008-03-19 01:37:10 +0100695 SCHED_FEAT_SYNC_WAKEUPS = 32,
696 SCHED_FEAT_HRTICK = 64,
697 SCHED_FEAT_DOUBLE_TICK = 128,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200698};
699
700const_debug unsigned int sysctl_sched_features =
Ingo Molnar8401f772007-10-18 21:32:55 +0200701 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
Ingo Molnar96126332007-11-15 20:57:40 +0100702 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
Ingo Molnar8401f772007-10-18 21:32:55 +0200703 SCHED_FEAT_START_DEBIT * 1 |
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100704 SCHED_FEAT_AFFINE_WAKEUPS * 1 |
705 SCHED_FEAT_CACHE_HOT_BUDDY * 1 |
Ingo Molnar02e2b832008-03-19 01:37:10 +0100706 SCHED_FEAT_SYNC_WAKEUPS * 1 |
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100707 SCHED_FEAT_HRTICK * 1 |
Ingo Molnar02e2b832008-03-19 01:37:10 +0100708 SCHED_FEAT_DOUBLE_TICK * 0;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200709
710#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
711
712/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100713 * Number of tasks to iterate in a single balance run.
714 * Limited because this is done with IRQs disabled.
715 */
716const_debug unsigned int sysctl_sched_nr_migrate = 32;
717
718/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100719 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100720 * default: 1s
721 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100722unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100723
Ingo Molnar6892b752008-02-13 14:02:36 +0100724static __read_mostly int scheduler_running;
725
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100726/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100727 * part of the period that we allow rt tasks to run in us.
728 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100729 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100730int sysctl_sched_rt_runtime = 950000;
731
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200732static inline u64 global_rt_period(void)
733{
734 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
735}
736
737static inline u64 global_rt_runtime(void)
738{
739 if (sysctl_sched_rt_period < 0)
740 return RUNTIME_INF;
741
742 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
743}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100744
Ingo Molnar27ec4402008-02-28 21:00:21 +0100745static const unsigned long long time_sync_thresh = 100000;
746
747static DEFINE_PER_CPU(unsigned long long, time_offset);
748static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
749
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100750/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100751 * Global lock which we take every now and then to synchronize
752 * the CPUs time. This method is not warp-safe, but it's good
753 * enough to synchronize slowly diverging time sources and thus
754 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200755 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100756static DEFINE_SPINLOCK(time_sync_lock);
757static unsigned long long prev_global_time;
758
759static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
760{
761 unsigned long flags;
762
763 spin_lock_irqsave(&time_sync_lock, flags);
764
765 if (time < prev_global_time) {
766 per_cpu(time_offset, cpu) += prev_global_time - time;
767 time = prev_global_time;
768 } else {
769 prev_global_time = time;
770 }
771
772 spin_unlock_irqrestore(&time_sync_lock, flags);
773
774 return time;
775}
776
777static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200778{
Ingo Molnare436d802007-07-19 21:28:35 +0200779 unsigned long long now;
780 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200781 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200782
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100783 /*
784 * Only call sched_clock() if the scheduler has already been
785 * initialized (some code might call cpu_clock() very early):
786 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100787 if (unlikely(!scheduler_running))
788 return 0;
789
790 local_irq_save(flags);
791 rq = cpu_rq(cpu);
792 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200793 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200794 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200795
796 return now;
797}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100798
799/*
800 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
801 * clock constructed from sched_clock():
802 */
803unsigned long long cpu_clock(int cpu)
804{
805 unsigned long long prev_cpu_time, time, delta_time;
806
807 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
808 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
809 delta_time = time-prev_cpu_time;
810
811 if (unlikely(delta_time > time_sync_thresh))
812 time = __sync_cpu_clock(time, cpu);
813
814 return time;
815}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200816EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700819# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700821#ifndef finish_arch_switch
822# define finish_arch_switch(prev) do { } while (0)
823#endif
824
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100825static inline int task_current(struct rq *rq, struct task_struct *p)
826{
827 return rq->curr == p;
828}
829
Nick Piggin4866cde2005-06-25 14:57:23 -0700830#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700831static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700832{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100833 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700834}
835
Ingo Molnar70b97a72006-07-03 00:25:42 -0700836static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700837{
838}
839
Ingo Molnar70b97a72006-07-03 00:25:42 -0700840static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700841{
Ingo Molnarda04c032005-09-13 11:17:59 +0200842#ifdef CONFIG_DEBUG_SPINLOCK
843 /* this is a valid case when another task releases the spinlock */
844 rq->lock.owner = current;
845#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700846 /*
847 * If we are tracking spinlock dependencies then we have to
848 * fix up the runqueue lock - which gets 'carried over' from
849 * prev into current:
850 */
851 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
852
Nick Piggin4866cde2005-06-25 14:57:23 -0700853 spin_unlock_irq(&rq->lock);
854}
855
856#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700857static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700858{
859#ifdef CONFIG_SMP
860 return p->oncpu;
861#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100862 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700863#endif
864}
865
Ingo Molnar70b97a72006-07-03 00:25:42 -0700866static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700867{
868#ifdef CONFIG_SMP
869 /*
870 * We can optimise this out completely for !SMP, because the
871 * SMP rebalancing from interrupt is the only thing that cares
872 * here.
873 */
874 next->oncpu = 1;
875#endif
876#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
877 spin_unlock_irq(&rq->lock);
878#else
879 spin_unlock(&rq->lock);
880#endif
881}
882
Ingo Molnar70b97a72006-07-03 00:25:42 -0700883static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700884{
885#ifdef CONFIG_SMP
886 /*
887 * After ->oncpu is cleared, the task can be moved to a different CPU.
888 * We must ensure this doesn't happen until the switch is completely
889 * finished.
890 */
891 smp_wmb();
892 prev->oncpu = 0;
893#endif
894#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
895 local_irq_enable();
896#endif
897}
898#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700901 * __task_rq_lock - lock the runqueue a given task resides on.
902 * Must be called interrupts disabled.
903 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700904static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700905 __acquires(rq->lock)
906{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200907 for (;;) {
908 struct rq *rq = task_rq(p);
909 spin_lock(&rq->lock);
910 if (likely(rq == task_rq(p)))
911 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700912 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700913 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700914}
915
916/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100918 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * explicitly disabling preemption.
920 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700921static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 __acquires(rq->lock)
923{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700924 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Andi Kleen3a5c3592007-10-15 17:00:14 +0200926 for (;;) {
927 local_irq_save(*flags);
928 rq = task_rq(p);
929 spin_lock(&rq->lock);
930 if (likely(rq == task_rq(p)))
931 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Alexey Dobriyana9957442007-10-15 17:00:13 +0200936static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700937 __releases(rq->lock)
938{
939 spin_unlock(&rq->lock);
940}
941
Ingo Molnar70b97a72006-07-03 00:25:42 -0700942static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 __releases(rq->lock)
944{
945 spin_unlock_irqrestore(&rq->lock, *flags);
946}
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800949 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200951static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 __acquires(rq->lock)
953{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700954 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 local_irq_disable();
957 rq = this_rq();
958 spin_lock(&rq->lock);
959
960 return rq;
961}
962
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200963/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200964 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200965 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200966void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200967{
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200968 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200969
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200970 spin_lock(&rq->lock);
971 __update_rq_clock(rq);
972 spin_unlock(&rq->lock);
973 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200974}
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200975EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
976
977/*
978 * We just idled delta nanoseconds (called with irqs disabled):
979 */
980void sched_clock_idle_wakeup_event(u64 delta_ns)
981{
982 struct rq *rq = cpu_rq(smp_processor_id());
983 u64 now = sched_clock();
984
985 rq->idle_clock += delta_ns;
986 /*
987 * Override the previous timestamp and ignore all
988 * sched_clock() deltas that occured while we idled,
989 * and use the PM-provided delta_ns to advance the
990 * rq clock:
991 */
992 spin_lock(&rq->lock);
993 rq->prev_clock_raw = now;
994 rq->clock += delta_ns;
995 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +0100996 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200997}
998EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200999
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001000static void __resched_task(struct task_struct *p, int tif_bit);
1001
1002static inline void resched_task(struct task_struct *p)
1003{
1004 __resched_task(p, TIF_NEED_RESCHED);
1005}
1006
1007#ifdef CONFIG_SCHED_HRTICK
1008/*
1009 * Use HR-timers to deliver accurate preemption points.
1010 *
1011 * Its all a bit involved since we cannot program an hrt while holding the
1012 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1013 * reschedule event.
1014 *
1015 * When we get rescheduled we reprogram the hrtick_timer outside of the
1016 * rq->lock.
1017 */
1018static inline void resched_hrt(struct task_struct *p)
1019{
1020 __resched_task(p, TIF_HRTICK_RESCHED);
1021}
1022
1023static inline void resched_rq(struct rq *rq)
1024{
1025 unsigned long flags;
1026
1027 spin_lock_irqsave(&rq->lock, flags);
1028 resched_task(rq->curr);
1029 spin_unlock_irqrestore(&rq->lock, flags);
1030}
1031
1032enum {
1033 HRTICK_SET, /* re-programm hrtick_timer */
1034 HRTICK_RESET, /* not a new slice */
1035};
1036
1037/*
1038 * Use hrtick when:
1039 * - enabled by features
1040 * - hrtimer is actually high res
1041 */
1042static inline int hrtick_enabled(struct rq *rq)
1043{
1044 if (!sched_feat(HRTICK))
1045 return 0;
1046 return hrtimer_is_hres_active(&rq->hrtick_timer);
1047}
1048
1049/*
1050 * Called to set the hrtick timer state.
1051 *
1052 * called with rq->lock held and irqs disabled
1053 */
1054static void hrtick_start(struct rq *rq, u64 delay, int reset)
1055{
1056 assert_spin_locked(&rq->lock);
1057
1058 /*
1059 * preempt at: now + delay
1060 */
1061 rq->hrtick_expire =
1062 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1063 /*
1064 * indicate we need to program the timer
1065 */
1066 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1067 if (reset)
1068 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1069
1070 /*
1071 * New slices are called from the schedule path and don't need a
1072 * forced reschedule.
1073 */
1074 if (reset)
1075 resched_hrt(rq->curr);
1076}
1077
1078static void hrtick_clear(struct rq *rq)
1079{
1080 if (hrtimer_active(&rq->hrtick_timer))
1081 hrtimer_cancel(&rq->hrtick_timer);
1082}
1083
1084/*
1085 * Update the timer from the possible pending state.
1086 */
1087static void hrtick_set(struct rq *rq)
1088{
1089 ktime_t time;
1090 int set, reset;
1091 unsigned long flags;
1092
1093 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1094
1095 spin_lock_irqsave(&rq->lock, flags);
1096 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1097 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1098 time = rq->hrtick_expire;
1099 clear_thread_flag(TIF_HRTICK_RESCHED);
1100 spin_unlock_irqrestore(&rq->lock, flags);
1101
1102 if (set) {
1103 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1104 if (reset && !hrtimer_active(&rq->hrtick_timer))
1105 resched_rq(rq);
1106 } else
1107 hrtick_clear(rq);
1108}
1109
1110/*
1111 * High-resolution timer tick.
1112 * Runs from hardirq context with interrupts disabled.
1113 */
1114static enum hrtimer_restart hrtick(struct hrtimer *timer)
1115{
1116 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1117
1118 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1119
1120 spin_lock(&rq->lock);
1121 __update_rq_clock(rq);
1122 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1123 spin_unlock(&rq->lock);
1124
1125 return HRTIMER_NORESTART;
1126}
1127
1128static inline void init_rq_hrtick(struct rq *rq)
1129{
1130 rq->hrtick_flags = 0;
1131 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1132 rq->hrtick_timer.function = hrtick;
1133 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1134}
1135
1136void hrtick_resched(void)
1137{
1138 struct rq *rq;
1139 unsigned long flags;
1140
1141 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1142 return;
1143
1144 local_irq_save(flags);
1145 rq = cpu_rq(smp_processor_id());
1146 hrtick_set(rq);
1147 local_irq_restore(flags);
1148}
1149#else
1150static inline void hrtick_clear(struct rq *rq)
1151{
1152}
1153
1154static inline void hrtick_set(struct rq *rq)
1155{
1156}
1157
1158static inline void init_rq_hrtick(struct rq *rq)
1159{
1160}
1161
1162void hrtick_resched(void)
1163{
1164}
1165#endif
1166
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001167/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001168 * resched_task - mark a task 'to be rescheduled now'.
1169 *
1170 * On UP this means the setting of the need_resched flag, on SMP it
1171 * might also involve a cross-CPU call to trigger the scheduler on
1172 * the target CPU.
1173 */
1174#ifdef CONFIG_SMP
1175
1176#ifndef tsk_is_polling
1177#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1178#endif
1179
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001180static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001181{
1182 int cpu;
1183
1184 assert_spin_locked(&task_rq(p)->lock);
1185
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001186 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001187 return;
1188
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001189 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001190
1191 cpu = task_cpu(p);
1192 if (cpu == smp_processor_id())
1193 return;
1194
1195 /* NEED_RESCHED must be visible before we test polling */
1196 smp_mb();
1197 if (!tsk_is_polling(p))
1198 smp_send_reschedule(cpu);
1199}
1200
1201static void resched_cpu(int cpu)
1202{
1203 struct rq *rq = cpu_rq(cpu);
1204 unsigned long flags;
1205
1206 if (!spin_trylock_irqsave(&rq->lock, flags))
1207 return;
1208 resched_task(cpu_curr(cpu));
1209 spin_unlock_irqrestore(&rq->lock, flags);
1210}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001211
1212#ifdef CONFIG_NO_HZ
1213/*
1214 * When add_timer_on() enqueues a timer into the timer wheel of an
1215 * idle CPU then this timer might expire before the next timer event
1216 * which is scheduled to wake up that CPU. In case of a completely
1217 * idle system the next event might even be infinite time into the
1218 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1219 * leaves the inner idle loop so the newly added timer is taken into
1220 * account when the CPU goes back to idle and evaluates the timer
1221 * wheel for the next timer event.
1222 */
1223void wake_up_idle_cpu(int cpu)
1224{
1225 struct rq *rq = cpu_rq(cpu);
1226
1227 if (cpu == smp_processor_id())
1228 return;
1229
1230 /*
1231 * This is safe, as this function is called with the timer
1232 * wheel base lock of (cpu) held. When the CPU is on the way
1233 * to idle and has not yet set rq->curr to idle then it will
1234 * be serialized on the timer wheel base lock and take the new
1235 * timer into account automatically.
1236 */
1237 if (rq->curr != rq->idle)
1238 return;
1239
1240 /*
1241 * We can set TIF_RESCHED on the idle task of the other CPU
1242 * lockless. The worst case is that the other CPU runs the
1243 * idle task through an additional NOOP schedule()
1244 */
1245 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1246
1247 /* NEED_RESCHED must be visible before we test polling */
1248 smp_mb();
1249 if (!tsk_is_polling(rq->idle))
1250 smp_send_reschedule(cpu);
1251}
1252#endif
1253
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001254#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001255static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001256{
1257 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001258 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001259}
1260#endif
1261
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001262#if BITS_PER_LONG == 32
1263# define WMULT_CONST (~0UL)
1264#else
1265# define WMULT_CONST (1UL << 32)
1266#endif
1267
1268#define WMULT_SHIFT 32
1269
Ingo Molnar194081e2007-08-09 11:16:51 +02001270/*
1271 * Shift right and round:
1272 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001273#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001274
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001275static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001276calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1277 struct load_weight *lw)
1278{
1279 u64 tmp;
1280
1281 if (unlikely(!lw->inv_weight))
Ingo Molnar27d11722008-03-14 22:20:01 +01001282 lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001283
1284 tmp = (u64)delta_exec * weight;
1285 /*
1286 * Check whether we'd overflow the 64-bit multiplication:
1287 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001288 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001289 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001290 WMULT_SHIFT/2);
1291 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001292 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001293
Ingo Molnarecf691d2007-08-02 17:41:40 +02001294 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001295}
1296
1297static inline unsigned long
1298calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1299{
1300 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1301}
1302
Ingo Molnar10919852007-10-15 17:00:04 +02001303static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001304{
1305 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001306 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001307}
1308
Ingo Molnar10919852007-10-15 17:00:04 +02001309static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001310{
1311 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001312 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001313}
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001316 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1317 * of tasks with abnormal "nice" values across CPUs the contribution that
1318 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001319 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001320 * scaled version of the new time slice allocation that they receive on time
1321 * slice expiry etc.
1322 */
1323
Ingo Molnardd41f592007-07-09 18:51:59 +02001324#define WEIGHT_IDLEPRIO 2
1325#define WMULT_IDLEPRIO (1 << 31)
1326
1327/*
1328 * Nice levels are multiplicative, with a gentle 10% change for every
1329 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1330 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1331 * that remained on nice 0.
1332 *
1333 * The "10% effect" is relative and cumulative: from _any_ nice level,
1334 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001335 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1336 * If a task goes up by ~10% and another task goes down by ~10% then
1337 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001338 */
1339static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001340 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1341 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1342 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1343 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1344 /* 0 */ 1024, 820, 655, 526, 423,
1345 /* 5 */ 335, 272, 215, 172, 137,
1346 /* 10 */ 110, 87, 70, 56, 45,
1347 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001348};
1349
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001350/*
1351 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1352 *
1353 * In cases where the weight does not change often, we can use the
1354 * precalculated inverse to speed up arithmetics by turning divisions
1355 * into multiplications:
1356 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001357static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001358 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1359 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1360 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1361 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1362 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1363 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1364 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1365 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001366};
Peter Williams2dd73a42006-06-27 02:54:34 -07001367
Ingo Molnardd41f592007-07-09 18:51:59 +02001368static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1369
1370/*
1371 * runqueue iterator, to support SMP load-balancing between different
1372 * scheduling classes, without having to expose their internal data
1373 * structures to the load-balancing proper:
1374 */
1375struct rq_iterator {
1376 void *arg;
1377 struct task_struct *(*start)(void *);
1378 struct task_struct *(*next)(void *);
1379};
1380
Peter Williamse1d14842007-10-24 18:23:51 +02001381#ifdef CONFIG_SMP
1382static unsigned long
1383balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1384 unsigned long max_load_move, struct sched_domain *sd,
1385 enum cpu_idle_type idle, int *all_pinned,
1386 int *this_best_prio, struct rq_iterator *iterator);
1387
1388static int
1389iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1390 struct sched_domain *sd, enum cpu_idle_type idle,
1391 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001392#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001393
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001394#ifdef CONFIG_CGROUP_CPUACCT
1395static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1396#else
1397static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1398#endif
1399
Gregory Haskinse7693a32008-01-25 21:08:09 +01001400#ifdef CONFIG_SMP
1401static unsigned long source_load(int cpu, int type);
1402static unsigned long target_load(int cpu, int type);
1403static unsigned long cpu_avg_load_per_task(int cpu);
1404static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1405#endif /* CONFIG_SMP */
1406
Ingo Molnardd41f592007-07-09 18:51:59 +02001407#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001408#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001409#include "sched_fair.c"
1410#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001411#ifdef CONFIG_SCHED_DEBUG
1412# include "sched_debug.c"
1413#endif
1414
1415#define sched_class_highest (&rt_sched_class)
1416
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001417static inline void inc_load(struct rq *rq, const struct task_struct *p)
Ingo Molnar9c217242007-08-02 17:41:40 +02001418{
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001419 update_load_add(&rq->load, p->se.load.weight);
Ingo Molnar9c217242007-08-02 17:41:40 +02001420}
1421
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001422static inline void dec_load(struct rq *rq, const struct task_struct *p)
1423{
1424 update_load_sub(&rq->load, p->se.load.weight);
1425}
1426
1427static void inc_nr_running(struct task_struct *p, struct rq *rq)
1428{
1429 rq->nr_running++;
1430 inc_load(rq, p);
1431}
1432
1433static void dec_nr_running(struct task_struct *p, struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001434{
1435 rq->nr_running--;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001436 dec_load(rq, p);
Ingo Molnar9c217242007-08-02 17:41:40 +02001437}
1438
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001439static void set_load_weight(struct task_struct *p)
1440{
1441 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001442 p->se.load.weight = prio_to_weight[0] * 2;
1443 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1444 return;
1445 }
1446
1447 /*
1448 * SCHED_IDLE tasks get minimal weight:
1449 */
1450 if (p->policy == SCHED_IDLE) {
1451 p->se.load.weight = WEIGHT_IDLEPRIO;
1452 p->se.load.inv_weight = WMULT_IDLEPRIO;
1453 return;
1454 }
1455
1456 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1457 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001458}
1459
Ingo Molnar8159f872007-08-09 11:16:49 +02001460static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001461{
1462 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001463 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001464 p->se.on_rq = 1;
1465}
1466
Ingo Molnar69be72c2007-08-09 11:16:49 +02001467static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001468{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001469 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001470 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001471}
1472
1473/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001474 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001475 */
Ingo Molnar14531182007-07-09 18:51:59 +02001476static inline int __normal_prio(struct task_struct *p)
1477{
Ingo Molnardd41f592007-07-09 18:51:59 +02001478 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001479}
1480
1481/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001482 * Calculate the expected normal priority: i.e. priority
1483 * without taking RT-inheritance into account. Might be
1484 * boosted by interactivity modifiers. Changes upon fork,
1485 * setprio syscalls, and whenever the interactivity
1486 * estimator recalculates.
1487 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001488static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001489{
1490 int prio;
1491
Ingo Molnare05606d2007-07-09 18:51:59 +02001492 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001493 prio = MAX_RT_PRIO-1 - p->rt_priority;
1494 else
1495 prio = __normal_prio(p);
1496 return prio;
1497}
1498
1499/*
1500 * Calculate the current priority, i.e. the priority
1501 * taken into account by the scheduler. This value might
1502 * be boosted by RT tasks, or might be boosted by
1503 * interactivity modifiers. Will be RT if the task got
1504 * RT-boosted. If not then it returns p->normal_prio.
1505 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001506static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001507{
1508 p->normal_prio = normal_prio(p);
1509 /*
1510 * If we are RT tasks or we were boosted to RT priority,
1511 * keep the priority unchanged. Otherwise, update priority
1512 * to the normal priority:
1513 */
1514 if (!rt_prio(p->prio))
1515 return p->normal_prio;
1516 return p->prio;
1517}
1518
1519/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001520 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001522static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001524 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001525 rq->nr_uninterruptible--;
1526
Ingo Molnar8159f872007-08-09 11:16:49 +02001527 enqueue_task(rq, p, wakeup);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001528 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
1531/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 * deactivate_task - remove a task from the runqueue.
1533 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001534static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001536 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001537 rq->nr_uninterruptible++;
1538
Ingo Molnar69be72c2007-08-09 11:16:49 +02001539 dequeue_task(rq, p, sleep);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001540 dec_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543/**
1544 * task_curr - is this task currently executing on a CPU?
1545 * @p: the task in question.
1546 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001547inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 return cpu_curr(task_cpu(p)) == p;
1550}
1551
Peter Williams2dd73a42006-06-27 02:54:34 -07001552/* Used instead of source_load when we know the type == 0 */
1553unsigned long weighted_cpuload(const int cpu)
1554{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001555 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001556}
1557
1558static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1559{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001560 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001561#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001562 /*
1563 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1564 * successfuly executed on another CPU. We must ensure that updates of
1565 * per-task data have been completed by this moment.
1566 */
1567 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001568 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001569#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001570}
1571
Steven Rostedtcb469842008-01-25 21:08:22 +01001572static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1573 const struct sched_class *prev_class,
1574 int oldprio, int running)
1575{
1576 if (prev_class != p->sched_class) {
1577 if (prev_class->switched_from)
1578 prev_class->switched_from(rq, p, running);
1579 p->sched_class->switched_to(rq, p, running);
1580 } else
1581 p->sched_class->prio_changed(rq, p, oldprio, running);
1582}
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001585
Ingo Molnarcc367732007-10-15 17:00:18 +02001586/*
1587 * Is this task likely cache-hot:
1588 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001589static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001590task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1591{
1592 s64 delta;
1593
Ingo Molnarf540a602008-03-15 17:10:34 +01001594 /*
1595 * Buddy candidates are cache hot:
1596 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01001597 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01001598 return 1;
1599
Ingo Molnarcc367732007-10-15 17:00:18 +02001600 if (p->sched_class != &fair_sched_class)
1601 return 0;
1602
Ingo Molnar6bc16652007-10-15 17:00:18 +02001603 if (sysctl_sched_migration_cost == -1)
1604 return 1;
1605 if (sysctl_sched_migration_cost == 0)
1606 return 0;
1607
Ingo Molnarcc367732007-10-15 17:00:18 +02001608 delta = now - p->se.exec_start;
1609
1610 return delta < (s64)sysctl_sched_migration_cost;
1611}
1612
1613
Ingo Molnardd41f592007-07-09 18:51:59 +02001614void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001615{
Ingo Molnardd41f592007-07-09 18:51:59 +02001616 int old_cpu = task_cpu(p);
1617 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001618 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1619 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001620 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001621
1622 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001623
1624#ifdef CONFIG_SCHEDSTATS
1625 if (p->se.wait_start)
1626 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001627 if (p->se.sleep_start)
1628 p->se.sleep_start -= clock_offset;
1629 if (p->se.block_start)
1630 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001631 if (old_cpu != new_cpu) {
1632 schedstat_inc(p, se.nr_migrations);
1633 if (task_hot(p, old_rq->clock, NULL))
1634 schedstat_inc(p, se.nr_forced2_migrations);
1635 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001636#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001637 p->se.vruntime -= old_cfsrq->min_vruntime -
1638 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001639
1640 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001641}
1642
Ingo Molnar70b97a72006-07-03 00:25:42 -07001643struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Ingo Molnar36c8b582006-07-03 00:25:41 -07001646 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 int dest_cpu;
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001650};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652/*
1653 * The task's runqueue lock must be held.
1654 * Returns true if you have to wait for migration thread.
1655 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001656static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001657migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001659 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /*
1662 * If the task is not on a runqueue (and not running), then
1663 * it is sufficient to simply update the task's cpu field.
1664 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001665 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 set_task_cpu(p, dest_cpu);
1667 return 0;
1668 }
1669
1670 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 req->task = p;
1672 req->dest_cpu = dest_cpu;
1673 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return 1;
1676}
1677
1678/*
1679 * wait_task_inactive - wait for a thread to unschedule.
1680 *
1681 * The caller must ensure that the task *will* unschedule sometime soon,
1682 * else this function might spin for a *long* time. This function can't
1683 * be called with interrupts off, or it may introduce deadlock with
1684 * smp_call_function() if an IPI is sent by the same process we are
1685 * waiting to become inactive.
1686 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001687void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001690 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001691 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Andi Kleen3a5c3592007-10-15 17:00:14 +02001693 for (;;) {
1694 /*
1695 * We do the initial early heuristics without holding
1696 * any task-queue locks at all. We'll only try to get
1697 * the runqueue lock when things look like they will
1698 * work out!
1699 */
1700 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001701
Andi Kleen3a5c3592007-10-15 17:00:14 +02001702 /*
1703 * If the task is actively running on another CPU
1704 * still, just relax and busy-wait without holding
1705 * any locks.
1706 *
1707 * NOTE! Since we don't hold any locks, it's not
1708 * even sure that "rq" stays as the right runqueue!
1709 * But we don't care, since "task_running()" will
1710 * return false if the runqueue has changed and p
1711 * is actually now running somewhere else!
1712 */
1713 while (task_running(rq, p))
1714 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001715
Andi Kleen3a5c3592007-10-15 17:00:14 +02001716 /*
1717 * Ok, time to look more closely! We need the rq
1718 * lock now, to be *sure*. If we're wrong, we'll
1719 * just go back and repeat.
1720 */
1721 rq = task_rq_lock(p, &flags);
1722 running = task_running(rq, p);
1723 on_rq = p->se.on_rq;
1724 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001725
Andi Kleen3a5c3592007-10-15 17:00:14 +02001726 /*
1727 * Was it really running after all now that we
1728 * checked with the proper locks actually held?
1729 *
1730 * Oops. Go back and try again..
1731 */
1732 if (unlikely(running)) {
1733 cpu_relax();
1734 continue;
1735 }
1736
1737 /*
1738 * It's not enough that it's not actively running,
1739 * it must be off the runqueue _entirely_, and not
1740 * preempted!
1741 *
1742 * So if it wa still runnable (but just not actively
1743 * running right now), it's preempted, and we should
1744 * yield - it could be a while.
1745 */
1746 if (unlikely(on_rq)) {
1747 schedule_timeout_uninterruptible(1);
1748 continue;
1749 }
1750
1751 /*
1752 * Ahh, all good. It wasn't running, and it wasn't
1753 * runnable, which means that it will never become
1754 * running in the future either. We're all done!
1755 */
1756 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
1760/***
1761 * kick_process - kick a running thread to enter/exit the kernel
1762 * @p: the to-be-kicked thread
1763 *
1764 * Cause a process which is running on another CPU to enter
1765 * kernel-mode, without any delay. (to get signals handled.)
1766 *
1767 * NOTE: this function doesnt have to take the runqueue lock,
1768 * because all it wants to ensure is that the remote task enters
1769 * the kernel. If the IPI races and the task has been migrated
1770 * to another CPU then no harm is done and the purpose has been
1771 * achieved as well.
1772 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001773void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 int cpu;
1776
1777 preempt_disable();
1778 cpu = task_cpu(p);
1779 if ((cpu != smp_processor_id()) && task_curr(p))
1780 smp_send_reschedule(cpu);
1781 preempt_enable();
1782}
1783
1784/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001785 * Return a low guess at the load of a migration-source cpu weighted
1786 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 *
1788 * We want to under-estimate the load of migration sources, to
1789 * balance conservatively.
1790 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001791static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001792{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001793 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001794 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001795
Peter Williams2dd73a42006-06-27 02:54:34 -07001796 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001797 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001798
Ingo Molnardd41f592007-07-09 18:51:59 +02001799 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
1802/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001803 * Return a high guess at the load of a migration-target cpu weighted
1804 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001806static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001807{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001808 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001809 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001810
Peter Williams2dd73a42006-06-27 02:54:34 -07001811 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001812 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001813
Ingo Molnardd41f592007-07-09 18:51:59 +02001814 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001815}
1816
1817/*
1818 * Return the average load per task on the cpu's run queue
1819 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001820static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07001821{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001822 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001823 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001824 unsigned long n = rq->nr_running;
1825
Ingo Molnardd41f592007-07-09 18:51:59 +02001826 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Nick Piggin147cbb42005-06-25 14:57:19 -07001829/*
1830 * find_idlest_group finds and returns the least busy CPU group within the
1831 * domain.
1832 */
1833static struct sched_group *
1834find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1835{
1836 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1837 unsigned long min_load = ULONG_MAX, this_load = 0;
1838 int load_idx = sd->forkexec_idx;
1839 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1840
1841 do {
1842 unsigned long load, avg_load;
1843 int local_group;
1844 int i;
1845
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001846 /* Skip over this group if it has no CPUs allowed */
1847 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001848 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001849
Nick Piggin147cbb42005-06-25 14:57:19 -07001850 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001851
1852 /* Tally up the load of all CPUs in the group */
1853 avg_load = 0;
1854
1855 for_each_cpu_mask(i, group->cpumask) {
1856 /* Bias balancing toward cpus of our domain */
1857 if (local_group)
1858 load = source_load(i, load_idx);
1859 else
1860 load = target_load(i, load_idx);
1861
1862 avg_load += load;
1863 }
1864
1865 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001866 avg_load = sg_div_cpu_power(group,
1867 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001868
1869 if (local_group) {
1870 this_load = avg_load;
1871 this = group;
1872 } else if (avg_load < min_load) {
1873 min_load = avg_load;
1874 idlest = group;
1875 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02001876 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07001877
1878 if (!idlest || 100*this_load < imbalance*min_load)
1879 return NULL;
1880 return idlest;
1881}
1882
1883/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001884 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001885 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001886static int
1887find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07001888{
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001889 cpumask_t tmp;
Nick Piggin147cbb42005-06-25 14:57:19 -07001890 unsigned long load, min_load = ULONG_MAX;
1891 int idlest = -1;
1892 int i;
1893
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001894 /* Traverse only the allowed CPUs */
1895 cpus_and(tmp, group->cpumask, p->cpus_allowed);
1896
1897 for_each_cpu_mask(i, tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001898 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001899
1900 if (load < min_load || (load == min_load && i == this_cpu)) {
1901 min_load = load;
1902 idlest = i;
1903 }
1904 }
1905
1906 return idlest;
1907}
1908
Nick Piggin476d1392005-06-25 14:57:29 -07001909/*
1910 * sched_balance_self: balance the current task (running on cpu) in domains
1911 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1912 * SD_BALANCE_EXEC.
1913 *
1914 * Balance, ie. select the least loaded group.
1915 *
1916 * Returns the target CPU number, or the same CPU if no balancing is needed.
1917 *
1918 * preempt must be disabled.
1919 */
1920static int sched_balance_self(int cpu, int flag)
1921{
1922 struct task_struct *t = current;
1923 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07001924
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001925 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02001926 /*
1927 * If power savings logic is enabled for a domain, stop there.
1928 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07001929 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1930 break;
Nick Piggin476d1392005-06-25 14:57:29 -07001931 if (tmp->flags & flag)
1932 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001933 }
Nick Piggin476d1392005-06-25 14:57:29 -07001934
1935 while (sd) {
1936 cpumask_t span;
1937 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001938 int new_cpu, weight;
1939
1940 if (!(sd->flags & flag)) {
1941 sd = sd->child;
1942 continue;
1943 }
Nick Piggin476d1392005-06-25 14:57:29 -07001944
1945 span = sd->span;
1946 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001947 if (!group) {
1948 sd = sd->child;
1949 continue;
1950 }
Nick Piggin476d1392005-06-25 14:57:29 -07001951
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001952 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001953 if (new_cpu == -1 || new_cpu == cpu) {
1954 /* Now try balancing at a lower domain level of cpu */
1955 sd = sd->child;
1956 continue;
1957 }
Nick Piggin476d1392005-06-25 14:57:29 -07001958
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001959 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07001960 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07001961 sd = NULL;
1962 weight = cpus_weight(span);
1963 for_each_domain(cpu, tmp) {
1964 if (weight <= cpus_weight(tmp->span))
1965 break;
1966 if (tmp->flags & flag)
1967 sd = tmp;
1968 }
1969 /* while loop will break here if sd == NULL */
1970 }
1971
1972 return cpu;
1973}
1974
1975#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977/***
1978 * try_to_wake_up - wake up a thread
1979 * @p: the to-be-woken-up thread
1980 * @state: the mask of task states that can be woken
1981 * @sync: do a synchronous wakeup?
1982 *
1983 * Put it on the run-queue if it's not already there. The "current"
1984 * thread is always on the run-queue (except when the actual
1985 * re-schedule is in progress), and as such you're allowed to do
1986 * the simpler "current->state = TASK_RUNNING" to mark yourself
1987 * runnable without the overhead of this.
1988 *
1989 * returns failure only if the task is already active.
1990 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001991static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Ingo Molnarcc367732007-10-15 17:00:18 +02001993 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 unsigned long flags;
1995 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001996 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Ingo Molnarb85d0662008-03-16 20:03:22 +01001998 if (!sched_feat(SYNC_WAKEUPS))
1999 sync = 0;
2000
Linus Torvalds04e2f172008-02-23 18:05:03 -08002001 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 rq = task_rq_lock(p, &flags);
2003 old_state = p->state;
2004 if (!(old_state & state))
2005 goto out;
2006
Ingo Molnardd41f592007-07-09 18:51:59 +02002007 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 goto out_running;
2009
2010 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002011 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 this_cpu = smp_processor_id();
2013
2014#ifdef CONFIG_SMP
2015 if (unlikely(task_running(rq, p)))
2016 goto out_activate;
2017
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002018 cpu = p->sched_class->select_task_rq(p, sync);
2019 if (cpu != orig_cpu) {
2020 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 task_rq_unlock(rq, &flags);
2022 /* might preempt at this point */
2023 rq = task_rq_lock(p, &flags);
2024 old_state = p->state;
2025 if (!(old_state & state))
2026 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002027 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 goto out_running;
2029
2030 this_cpu = smp_processor_id();
2031 cpu = task_cpu(p);
2032 }
2033
Gregory Haskinse7693a32008-01-25 21:08:09 +01002034#ifdef CONFIG_SCHEDSTATS
2035 schedstat_inc(rq, ttwu_count);
2036 if (cpu == this_cpu)
2037 schedstat_inc(rq, ttwu_local);
2038 else {
2039 struct sched_domain *sd;
2040 for_each_domain(this_cpu, sd) {
2041 if (cpu_isset(cpu, sd->span)) {
2042 schedstat_inc(sd, ttwu_wake_remote);
2043 break;
2044 }
2045 }
2046 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002047#endif
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049out_activate:
2050#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002051 schedstat_inc(p, se.nr_wakeups);
2052 if (sync)
2053 schedstat_inc(p, se.nr_wakeups_sync);
2054 if (orig_cpu != cpu)
2055 schedstat_inc(p, se.nr_wakeups_migrate);
2056 if (cpu == this_cpu)
2057 schedstat_inc(p, se.nr_wakeups_local);
2058 else
2059 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002060 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002061 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 success = 1;
2063
2064out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002065 check_preempt_curr(rq, p);
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002068#ifdef CONFIG_SMP
2069 if (p->sched_class->task_wake_up)
2070 p->sched_class->task_wake_up(rq, p);
2071#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072out:
2073 task_rq_unlock(rq, &flags);
2074
2075 return success;
2076}
2077
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002078int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002080 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082EXPORT_SYMBOL(wake_up_process);
2083
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002084int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086 return try_to_wake_up(p, state, 0);
2087}
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089/*
2090 * Perform scheduler related setup for a newly forked process p.
2091 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002092 *
2093 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002095static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Ingo Molnardd41f592007-07-09 18:51:59 +02002097 p->se.exec_start = 0;
2098 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002099 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002100 p->se.last_wakeup = 0;
2101 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002102
2103#ifdef CONFIG_SCHEDSTATS
2104 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002105 p->se.sum_sleep_runtime = 0;
2106 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002107 p->se.block_start = 0;
2108 p->se.sleep_max = 0;
2109 p->se.block_max = 0;
2110 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002111 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002112 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002113#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002114
Peter Zijlstrafa717062008-01-25 21:08:27 +01002115 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002116 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07002117
Avi Kivitye107be32007-07-26 13:40:43 +02002118#ifdef CONFIG_PREEMPT_NOTIFIERS
2119 INIT_HLIST_HEAD(&p->preempt_notifiers);
2120#endif
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /*
2123 * We mark the process as running here, but have not actually
2124 * inserted it onto the runqueue yet. This guarantees that
2125 * nobody will actually run it, and a signal or other external
2126 * event cannot wake it up and insert it on the runqueue either.
2127 */
2128 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002129}
2130
2131/*
2132 * fork()/clone()-time setup:
2133 */
2134void sched_fork(struct task_struct *p, int clone_flags)
2135{
2136 int cpu = get_cpu();
2137
2138 __sched_fork(p);
2139
2140#ifdef CONFIG_SMP
2141 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2142#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002143 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002144
2145 /*
2146 * Make sure we do not leak PI boosting priority to the child:
2147 */
2148 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002149 if (!rt_prio(p->prio))
2150 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002151
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002152#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002153 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002154 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002156#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002157 p->oncpu = 0;
2158#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002160 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002161 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002163 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
2166/*
2167 * wake_up_new_task - wake up a newly created task for the first time.
2168 *
2169 * This function will do some initial scheduler statistics housekeeping
2170 * that must be done for every newly created context, then puts the task
2171 * on the runqueue and wakes it.
2172 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002173void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002176 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002180 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 p->prio = effective_prio(p);
2183
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002184 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002185 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002188 * Let the scheduling class do new task startup
2189 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002191 p->sched_class->task_new(rq, p);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002192 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002194 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002195#ifdef CONFIG_SMP
2196 if (p->sched_class->task_wake_up)
2197 p->sched_class->task_wake_up(rq, p);
2198#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002199 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
Avi Kivitye107be32007-07-26 13:40:43 +02002202#ifdef CONFIG_PREEMPT_NOTIFIERS
2203
2204/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002205 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2206 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002207 */
2208void preempt_notifier_register(struct preempt_notifier *notifier)
2209{
2210 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2211}
2212EXPORT_SYMBOL_GPL(preempt_notifier_register);
2213
2214/**
2215 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002216 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002217 *
2218 * This is safe to call from within a preemption notifier.
2219 */
2220void preempt_notifier_unregister(struct preempt_notifier *notifier)
2221{
2222 hlist_del(&notifier->link);
2223}
2224EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2225
2226static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2227{
2228 struct preempt_notifier *notifier;
2229 struct hlist_node *node;
2230
2231 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2232 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2233}
2234
2235static void
2236fire_sched_out_preempt_notifiers(struct task_struct *curr,
2237 struct task_struct *next)
2238{
2239 struct preempt_notifier *notifier;
2240 struct hlist_node *node;
2241
2242 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2243 notifier->ops->sched_out(notifier, next);
2244}
2245
2246#else
2247
2248static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2249{
2250}
2251
2252static void
2253fire_sched_out_preempt_notifiers(struct task_struct *curr,
2254 struct task_struct *next)
2255{
2256}
2257
2258#endif
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002261 * prepare_task_switch - prepare to switch tasks
2262 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002263 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002264 * @next: the task we are going to switch to.
2265 *
2266 * This is called with the rq lock held and interrupts off. It must
2267 * be paired with a subsequent finish_task_switch after the context
2268 * switch.
2269 *
2270 * prepare_task_switch sets up locking and calls architecture specific
2271 * hooks.
2272 */
Avi Kivitye107be32007-07-26 13:40:43 +02002273static inline void
2274prepare_task_switch(struct rq *rq, struct task_struct *prev,
2275 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002276{
Avi Kivitye107be32007-07-26 13:40:43 +02002277 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002278 prepare_lock_switch(rq, next);
2279 prepare_arch_switch(next);
2280}
2281
2282/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002284 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 * @prev: the thread we just switched away from.
2286 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002287 * finish_task_switch must be called after the context switch, paired
2288 * with a prepare_task_switch call before the context switch.
2289 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2290 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 *
2292 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002293 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 * with the lock held can cause deadlocks; see schedule() for
2295 * details.)
2296 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002297static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 __releases(rq->lock)
2299{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002301 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 rq->prev_mm = NULL;
2304
2305 /*
2306 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002307 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002308 * schedule one last time. The schedule call will never return, and
2309 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002310 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 * still held, otherwise prev could be scheduled on another cpu, die
2312 * there before we look at prev->state, and then the reference would
2313 * be dropped twice.
2314 * Manfred Spraul <manfred@colorfullife.com>
2315 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002316 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002317 finish_arch_switch(prev);
2318 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002319#ifdef CONFIG_SMP
2320 if (current->sched_class->post_schedule)
2321 current->sched_class->post_schedule(rq);
2322#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002323
Avi Kivitye107be32007-07-26 13:40:43 +02002324 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (mm)
2326 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002327 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002328 /*
2329 * Remove function-return probe instances associated with this
2330 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002331 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002332 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
2337/**
2338 * schedule_tail - first thing a freshly forked thread must call.
2339 * @prev: the thread we just switched away from.
2340 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002341asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 __releases(rq->lock)
2343{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002344 struct rq *rq = this_rq();
2345
Nick Piggin4866cde2005-06-25 14:57:23 -07002346 finish_task_switch(rq, prev);
2347#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2348 /* In this case, finish_task_switch does not reenable preemption */
2349 preempt_enable();
2350#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002352 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
2355/*
2356 * context_switch - switch to the new MM and the new
2357 * thread's register state.
2358 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002359static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002360context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002361 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Ingo Molnardd41f592007-07-09 18:51:59 +02002363 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Avi Kivitye107be32007-07-26 13:40:43 +02002365 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002366 mm = next->mm;
2367 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002368 /*
2369 * For paravirt, this is coupled with an exit in switch_to to
2370 * combine the page table reload and the switch backend into
2371 * one hypercall.
2372 */
2373 arch_enter_lazy_cpu_mode();
2374
Ingo Molnardd41f592007-07-09 18:51:59 +02002375 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 next->active_mm = oldmm;
2377 atomic_inc(&oldmm->mm_count);
2378 enter_lazy_tlb(oldmm, next);
2379 } else
2380 switch_mm(oldmm, mm, next);
2381
Ingo Molnardd41f592007-07-09 18:51:59 +02002382 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 rq->prev_mm = oldmm;
2385 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002386 /*
2387 * Since the runqueue lock will be released by the next
2388 * task (which is an invalid locking op but in the case
2389 * of the scheduler it's an obvious special-case), so we
2390 * do an early lockdep release here:
2391 */
2392#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002393 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 /* Here we just switch the register state and the stack. */
2397 switch_to(prev, next, prev);
2398
Ingo Molnardd41f592007-07-09 18:51:59 +02002399 barrier();
2400 /*
2401 * this_rq must be evaluated again because prev may have moved
2402 * CPUs since it called schedule(), thus the 'rq' on its stack
2403 * frame will be invalid.
2404 */
2405 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
2408/*
2409 * nr_running, nr_uninterruptible and nr_context_switches:
2410 *
2411 * externally visible scheduler statistics: current number of runnable
2412 * threads, current number of uninterruptible-sleeping threads, total
2413 * number of context switches performed since bootup.
2414 */
2415unsigned long nr_running(void)
2416{
2417 unsigned long i, sum = 0;
2418
2419 for_each_online_cpu(i)
2420 sum += cpu_rq(i)->nr_running;
2421
2422 return sum;
2423}
2424
2425unsigned long nr_uninterruptible(void)
2426{
2427 unsigned long i, sum = 0;
2428
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002429 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 sum += cpu_rq(i)->nr_uninterruptible;
2431
2432 /*
2433 * Since we read the counters lockless, it might be slightly
2434 * inaccurate. Do not allow it to go below zero though:
2435 */
2436 if (unlikely((long)sum < 0))
2437 sum = 0;
2438
2439 return sum;
2440}
2441
2442unsigned long long nr_context_switches(void)
2443{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002444 int i;
2445 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002447 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 sum += cpu_rq(i)->nr_switches;
2449
2450 return sum;
2451}
2452
2453unsigned long nr_iowait(void)
2454{
2455 unsigned long i, sum = 0;
2456
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002457 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2459
2460 return sum;
2461}
2462
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002463unsigned long nr_active(void)
2464{
2465 unsigned long i, running = 0, uninterruptible = 0;
2466
2467 for_each_online_cpu(i) {
2468 running += cpu_rq(i)->nr_running;
2469 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2470 }
2471
2472 if (unlikely((long)uninterruptible < 0))
2473 uninterruptible = 0;
2474
2475 return running + uninterruptible;
2476}
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002479 * Update rq->cpu_load[] statistics. This function is usually called every
2480 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002481 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002482static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002483{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002484 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002485 int i, scale;
2486
2487 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002488
2489 /* Update our load: */
2490 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2491 unsigned long old_load, new_load;
2492
2493 /* scale is effectively 1 << i now, and >> i divides by scale */
2494
2495 old_load = this_rq->cpu_load[i];
2496 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002497 /*
2498 * Round up the averaging division if load is increasing. This
2499 * prevents us from getting stuck on 9 if the load is 10, for
2500 * example.
2501 */
2502 if (new_load > old_load)
2503 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002504 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2505 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002506}
2507
Ingo Molnardd41f592007-07-09 18:51:59 +02002508#ifdef CONFIG_SMP
2509
Ingo Molnar48f24c42006-07-03 00:25:40 -07002510/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 * double_rq_lock - safely lock two runqueues
2512 *
2513 * Note this does not disable interrupts like task_rq_lock,
2514 * you need to do so manually before calling.
2515 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002516static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 __acquires(rq1->lock)
2518 __acquires(rq2->lock)
2519{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002520 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 if (rq1 == rq2) {
2522 spin_lock(&rq1->lock);
2523 __acquire(rq2->lock); /* Fake it out ;) */
2524 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002525 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 spin_lock(&rq1->lock);
2527 spin_lock(&rq2->lock);
2528 } else {
2529 spin_lock(&rq2->lock);
2530 spin_lock(&rq1->lock);
2531 }
2532 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002533 update_rq_clock(rq1);
2534 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
2537/*
2538 * double_rq_unlock - safely unlock two runqueues
2539 *
2540 * Note this does not restore interrupts like task_rq_unlock,
2541 * you need to do so manually after calling.
2542 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002543static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 __releases(rq1->lock)
2545 __releases(rq2->lock)
2546{
2547 spin_unlock(&rq1->lock);
2548 if (rq1 != rq2)
2549 spin_unlock(&rq2->lock);
2550 else
2551 __release(rq2->lock);
2552}
2553
2554/*
2555 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2556 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002557static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 __releases(this_rq->lock)
2559 __acquires(busiest->lock)
2560 __acquires(this_rq->lock)
2561{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002562 int ret = 0;
2563
Kirill Korotaev054b9102006-12-10 02:20:11 -08002564 if (unlikely(!irqs_disabled())) {
2565 /* printk() doesn't work good under rq->lock */
2566 spin_unlock(&this_rq->lock);
2567 BUG_ON(1);
2568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002570 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 spin_unlock(&this_rq->lock);
2572 spin_lock(&busiest->lock);
2573 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002574 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 } else
2576 spin_lock(&busiest->lock);
2577 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002578 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
2581/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 * If dest_cpu is allowed for this process, migrate the task to it.
2583 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002584 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 * the cpu_allowed mask is restored.
2586 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002587static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002589 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002591 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 rq = task_rq_lock(p, &flags);
2594 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2595 || unlikely(cpu_is_offline(dest_cpu)))
2596 goto out;
2597
2598 /* force the process onto the specified CPU */
2599 if (migrate_task(p, dest_cpu, &req)) {
2600 /* Need to wait for migration thread (might exit: take ref). */
2601 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 get_task_struct(mt);
2604 task_rq_unlock(rq, &flags);
2605 wake_up_process(mt);
2606 put_task_struct(mt);
2607 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 return;
2610 }
2611out:
2612 task_rq_unlock(rq, &flags);
2613}
2614
2615/*
Nick Piggin476d1392005-06-25 14:57:29 -07002616 * sched_exec - execve() is a valuable balancing opportunity, because at
2617 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 */
2619void sched_exec(void)
2620{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002622 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002624 if (new_cpu != this_cpu)
2625 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
2628/*
2629 * pull_task - move a task from a remote runqueue to the local runqueue.
2630 * Both runqueues must be locked.
2631 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002632static void pull_task(struct rq *src_rq, struct task_struct *p,
2633 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002635 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002637 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 /*
2639 * Note that idle threads have a prio of MAX_PRIO, for this test
2640 * to be always true for them.
2641 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002642 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643}
2644
2645/*
2646 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2647 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002648static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002649int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002650 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002651 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
2653 /*
2654 * We do not migrate tasks that are:
2655 * 1) running (obviously), or
2656 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2657 * 3) are cache-hot on their current CPU.
2658 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002659 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2660 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002662 }
Nick Piggin81026792005-06-25 14:57:07 -07002663 *all_pinned = 0;
2664
Ingo Molnarcc367732007-10-15 17:00:18 +02002665 if (task_running(rq, p)) {
2666 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002667 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Ingo Molnarda84d962007-10-15 17:00:18 +02002670 /*
2671 * Aggressive migration if:
2672 * 1) task is cache cold, or
2673 * 2) too many balance attempts have failed.
2674 */
2675
Ingo Molnar6bc16652007-10-15 17:00:18 +02002676 if (!task_hot(p, rq->clock, sd) ||
2677 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002678#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002679 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002680 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002681 schedstat_inc(p, se.nr_forced_migrations);
2682 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002683#endif
2684 return 1;
2685 }
2686
Ingo Molnarcc367732007-10-15 17:00:18 +02002687 if (task_hot(p, rq->clock, sd)) {
2688 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002689 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return 1;
2692}
2693
Peter Williamse1d14842007-10-24 18:23:51 +02002694static unsigned long
2695balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2696 unsigned long max_load_move, struct sched_domain *sd,
2697 enum cpu_idle_type idle, int *all_pinned,
2698 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002699{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002700 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02002701 struct task_struct *p;
2702 long rem_load_move = max_load_move;
2703
Peter Williamse1d14842007-10-24 18:23:51 +02002704 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002705 goto out;
2706
2707 pinned = 1;
2708
2709 /*
2710 * Start the load-balancing iterator:
2711 */
2712 p = iterator->start(iterator->arg);
2713next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002714 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002715 goto out;
2716 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002717 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02002718 * skip a task if it will be the highest priority task (i.e. smallest
2719 * prio value) on its new queue regardless of its load weight
2720 */
2721 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2722 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002723 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002724 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002725 p = iterator->next(iterator->arg);
2726 goto next;
2727 }
2728
2729 pull_task(busiest, p, this_rq, this_cpu);
2730 pulled++;
2731 rem_load_move -= p->se.load.weight;
2732
2733 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002734 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002735 */
Peter Williamse1d14842007-10-24 18:23:51 +02002736 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002737 if (p->prio < *this_best_prio)
2738 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002739 p = iterator->next(iterator->arg);
2740 goto next;
2741 }
2742out:
2743 /*
Peter Williamse1d14842007-10-24 18:23:51 +02002744 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02002745 * so we can safely collect pull_task() stats here rather than
2746 * inside pull_task().
2747 */
2748 schedstat_add(sd, lb_gained[idle], pulled);
2749
2750 if (all_pinned)
2751 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02002752
2753 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02002754}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756/*
Peter Williams43010652007-08-09 11:16:46 +02002757 * move_tasks tries to move up to max_load_move weighted load from busiest to
2758 * this_rq, as part of a balancing operation within domain "sd".
2759 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 *
2761 * Called with both runqueues locked.
2762 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002763static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002764 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002765 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002766 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002768 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002769 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002770 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Ingo Molnardd41f592007-07-09 18:51:59 +02002772 do {
Peter Williams43010652007-08-09 11:16:46 +02002773 total_load_moved +=
2774 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02002775 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002776 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002777 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002778 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Peter Williams43010652007-08-09 11:16:46 +02002780 return total_load_moved > 0;
2781}
2782
Peter Williamse1d14842007-10-24 18:23:51 +02002783static int
2784iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2785 struct sched_domain *sd, enum cpu_idle_type idle,
2786 struct rq_iterator *iterator)
2787{
2788 struct task_struct *p = iterator->start(iterator->arg);
2789 int pinned = 0;
2790
2791 while (p) {
2792 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
2793 pull_task(busiest, p, this_rq, this_cpu);
2794 /*
2795 * Right now, this is only the second place pull_task()
2796 * is called, so we can safely collect pull_task()
2797 * stats here rather than inside pull_task().
2798 */
2799 schedstat_inc(sd, lb_gained[idle]);
2800
2801 return 1;
2802 }
2803 p = iterator->next(iterator->arg);
2804 }
2805
2806 return 0;
2807}
2808
Peter Williams43010652007-08-09 11:16:46 +02002809/*
2810 * move_one_task tries to move exactly one task from busiest to this_rq, as
2811 * part of active balancing operations within "domain".
2812 * Returns 1 if successful and 0 otherwise.
2813 *
2814 * Called with both runqueues locked.
2815 */
2816static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2817 struct sched_domain *sd, enum cpu_idle_type idle)
2818{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002819 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02002820
2821 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02002822 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02002823 return 1;
2824
2825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
2828/*
2829 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002830 * domain. It calculates and returns the amount of weighted load which
2831 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 */
2833static struct sched_group *
2834find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002835 unsigned long *imbalance, enum cpu_idle_type idle,
2836 int *sd_idle, cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
2838 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2839 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002840 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002841 unsigned long busiest_load_per_task, busiest_nr_running;
2842 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002843 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002844#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2845 int power_savings_balance = 1;
2846 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2847 unsigned long min_nr_running = ULONG_MAX;
2848 struct sched_group *group_min = NULL, *group_leader = NULL;
2849#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002852 busiest_load_per_task = busiest_nr_running = 0;
2853 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002854 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002855 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002856 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002857 load_idx = sd->newidle_idx;
2858 else
2859 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
2861 do {
Ken Chen908a7c12007-10-17 16:55:11 +02002862 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 int local_group;
2864 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02002865 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002866 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002867 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 local_group = cpu_isset(this_cpu, group->cpumask);
2870
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002871 if (local_group)
2872 balance_cpu = first_cpu(group->cpumask);
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002875 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02002876 max_cpu_load = 0;
2877 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002880 struct rq *rq;
2881
2882 if (!cpu_isset(i, *cpus))
2883 continue;
2884
2885 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002886
Suresh Siddha9439aab2007-07-19 21:28:35 +02002887 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002888 *sd_idle = 0;
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002891 if (local_group) {
2892 if (idle_cpu(i) && !first_idle_cpu) {
2893 first_idle_cpu = 1;
2894 balance_cpu = i;
2895 }
2896
Nick Piggina2000572006-02-10 01:51:02 -08002897 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002898 } else {
Nick Piggina2000572006-02-10 01:51:02 -08002899 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002900 if (load > max_cpu_load)
2901 max_cpu_load = load;
2902 if (min_cpu_load > load)
2903 min_cpu_load = load;
2904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
2906 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002907 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002908 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
2910
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002911 /*
2912 * First idle cpu or the first cpu(busiest) in this sched group
2913 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002914 * domains. In the newly idle case, we will allow all the cpu's
2915 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002916 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002917 if (idle != CPU_NEWLY_IDLE && local_group &&
2918 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002919 *balance = 0;
2920 goto ret;
2921 }
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002924 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002927 avg_load = sg_div_cpu_power(group,
2928 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Ken Chen908a7c12007-10-17 16:55:11 +02002930 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
2931 __group_imb = 1;
2932
Eric Dumazet5517d862007-05-08 00:32:57 -07002933 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 if (local_group) {
2936 this_load = avg_load;
2937 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002938 this_nr_running = sum_nr_running;
2939 this_load_per_task = sum_weighted_load;
2940 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02002941 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 max_load = avg_load;
2943 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002944 busiest_nr_running = sum_nr_running;
2945 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02002946 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002948
2949#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2950 /*
2951 * Busy processors will not participate in power savings
2952 * balance.
2953 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002954 if (idle == CPU_NOT_IDLE ||
2955 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2956 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002957
2958 /*
2959 * If the local group is idle or completely loaded
2960 * no need to do power savings balance at this domain
2961 */
2962 if (local_group && (this_nr_running >= group_capacity ||
2963 !this_nr_running))
2964 power_savings_balance = 0;
2965
Ingo Molnardd41f592007-07-09 18:51:59 +02002966 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002967 * If a group is already running at full capacity or idle,
2968 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002969 */
2970 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002971 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002972 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002973
Ingo Molnardd41f592007-07-09 18:51:59 +02002974 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002975 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002976 * This is the group from where we need to pick up the load
2977 * for saving power
2978 */
2979 if ((sum_nr_running < min_nr_running) ||
2980 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002981 first_cpu(group->cpumask) <
2982 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002983 group_min = group;
2984 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002985 min_load_per_task = sum_weighted_load /
2986 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002987 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002988
Ingo Molnardd41f592007-07-09 18:51:59 +02002989 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002990 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002991 * capacity but still has some space to pick up some load
2992 * from other group and save more power
2993 */
2994 if (sum_nr_running <= group_capacity - 1) {
2995 if (sum_nr_running > leader_nr_running ||
2996 (sum_nr_running == leader_nr_running &&
2997 first_cpu(group->cpumask) >
2998 first_cpu(group_leader->cpumask))) {
2999 group_leader = group;
3000 leader_nr_running = sum_nr_running;
3001 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003002 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003003group_next:
3004#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 group = group->next;
3006 } while (group != sd->groups);
3007
Peter Williams2dd73a42006-06-27 02:54:34 -07003008 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 goto out_balanced;
3010
3011 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3012
3013 if (this_load >= avg_load ||
3014 100*max_load <= sd->imbalance_pct*this_load)
3015 goto out_balanced;
3016
Peter Williams2dd73a42006-06-27 02:54:34 -07003017 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003018 if (group_imb)
3019 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 /*
3022 * We're trying to get all the cpus to the average_load, so we don't
3023 * want to push ourselves above the average load, nor do we wish to
3024 * reduce the max loaded cpu below the average load, as either of these
3025 * actions would just result in more rebalancing later, and ping-pong
3026 * tasks around. Thus we look for the minimum possible imbalance.
3027 * Negative imbalances (*we* are more loaded than anyone else) will
3028 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003029 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 * appear as very large values with unsigned longs.
3031 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003032 if (max_load <= busiest_load_per_task)
3033 goto out_balanced;
3034
3035 /*
3036 * In the presence of smp nice balancing, certain scenarios can have
3037 * max load less than avg load(as we skip the groups at or below
3038 * its cpu_power, while calculating max_load..)
3039 */
3040 if (max_load < avg_load) {
3041 *imbalance = 0;
3042 goto small_imbalance;
3043 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003044
3045 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003046 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003049 *imbalance = min(max_pull * busiest->__cpu_power,
3050 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 / SCHED_LOAD_SCALE;
3052
Peter Williams2dd73a42006-06-27 02:54:34 -07003053 /*
3054 * if *imbalance is less than the average load per runnable task
3055 * there is no gaurantee that any tasks will be moved so we'll have
3056 * a think about bumping its value to force at least one task to be
3057 * moved
3058 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003059 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003060 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003061 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Peter Williams2dd73a42006-06-27 02:54:34 -07003063small_imbalance:
3064 pwr_move = pwr_now = 0;
3065 imbn = 2;
3066 if (this_nr_running) {
3067 this_load_per_task /= this_nr_running;
3068 if (busiest_load_per_task > this_load_per_task)
3069 imbn = 1;
3070 } else
3071 this_load_per_task = SCHED_LOAD_SCALE;
3072
Ingo Molnardd41f592007-07-09 18:51:59 +02003073 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3074 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003075 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 return busiest;
3077 }
3078
3079 /*
3080 * OK, we don't have enough imbalance to justify moving tasks,
3081 * however we may be able to increase total CPU power used by
3082 * moving them.
3083 */
3084
Eric Dumazet5517d862007-05-08 00:32:57 -07003085 pwr_now += busiest->__cpu_power *
3086 min(busiest_load_per_task, max_load);
3087 pwr_now += this->__cpu_power *
3088 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 pwr_now /= SCHED_LOAD_SCALE;
3090
3091 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003092 tmp = sg_div_cpu_power(busiest,
3093 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003095 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003096 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003099 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003100 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003101 tmp = sg_div_cpu_power(this,
3102 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003104 tmp = sg_div_cpu_power(this,
3105 busiest_load_per_task * SCHED_LOAD_SCALE);
3106 pwr_move += this->__cpu_power *
3107 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 pwr_move /= SCHED_LOAD_SCALE;
3109
3110 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003111 if (pwr_move > pwr_now)
3112 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 }
3114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return busiest;
3116
3117out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003118#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003119 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003120 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003122 if (this == group_leader && group_leader != group_min) {
3123 *imbalance = min_load_per_task;
3124 return group_min;
3125 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003126#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003127ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 *imbalance = 0;
3129 return NULL;
3130}
3131
3132/*
3133 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3134 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003135static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003136find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003137 unsigned long imbalance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003139 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003140 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 int i;
3142
3143 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003144 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003145
3146 if (!cpu_isset(i, *cpus))
3147 continue;
3148
Ingo Molnar48f24c42006-07-03 00:25:40 -07003149 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003150 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Ingo Molnardd41f592007-07-09 18:51:59 +02003152 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003153 continue;
3154
Ingo Molnardd41f592007-07-09 18:51:59 +02003155 if (wl > max_load) {
3156 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003157 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 }
3159 }
3160
3161 return busiest;
3162}
3163
3164/*
Nick Piggin77391d72005-06-25 14:57:30 -07003165 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3166 * so long as it is large enough.
3167 */
3168#define MAX_PINNED_INTERVAL 512
3169
3170/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3172 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003174static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003175 struct sched_domain *sd, enum cpu_idle_type idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003176 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
Peter Williams43010652007-08-09 11:16:46 +02003178 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003181 struct rq *busiest;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003182 cpumask_t cpus = CPU_MASK_ALL;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003183 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003184
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003185 /*
3186 * When power savings policy is enabled for the parent domain, idle
3187 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003188 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003189 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003190 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003191 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003192 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003193 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Ingo Molnar2d723762007-10-15 17:00:12 +02003195 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003197redo:
3198 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003199 &cpus, balance);
3200
Chen, Kenneth W06066712006-12-10 02:20:35 -08003201 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003202 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 if (!group) {
3205 schedstat_inc(sd, lb_nobusyg[idle]);
3206 goto out_balanced;
3207 }
3208
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003209 busiest = find_busiest_queue(group, idle, imbalance, &cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 if (!busiest) {
3211 schedstat_inc(sd, lb_nobusyq[idle]);
3212 goto out_balanced;
3213 }
3214
Nick Piggindb935db2005-06-25 14:57:11 -07003215 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 schedstat_add(sd, lb_imbalance[idle], imbalance);
3218
Peter Williams43010652007-08-09 11:16:46 +02003219 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (busiest->nr_running > 1) {
3221 /*
3222 * Attempt to move tasks. If find_busiest_group has found
3223 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003224 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 * correctly treated as an imbalance.
3226 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003227 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003228 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003229 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003230 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003231 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003232 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003233
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003234 /*
3235 * some other cpu did the load balance for us.
3236 */
Peter Williams43010652007-08-09 11:16:46 +02003237 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003238 resched_cpu(this_cpu);
3239
Nick Piggin81026792005-06-25 14:57:07 -07003240 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003241 if (unlikely(all_pinned)) {
3242 cpu_clear(cpu_of(busiest), cpus);
3243 if (!cpus_empty(cpus))
3244 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003245 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 }
Nick Piggin81026792005-06-25 14:57:07 -07003248
Peter Williams43010652007-08-09 11:16:46 +02003249 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 schedstat_inc(sd, lb_failed[idle]);
3251 sd->nr_balance_failed++;
3252
3253 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003255 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003256
3257 /* don't kick the migration_thread, if the curr
3258 * task on busiest cpu can't be moved to this_cpu
3259 */
3260 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003261 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003262 all_pinned = 1;
3263 goto out_one_pinned;
3264 }
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 if (!busiest->active_balance) {
3267 busiest->active_balance = 1;
3268 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003269 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003271 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003272 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 wake_up_process(busiest->migration_thread);
3274
3275 /*
3276 * We've kicked active balancing, reset the failure
3277 * counter.
3278 */
Nick Piggin39507452005-06-25 14:57:09 -07003279 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
Nick Piggin81026792005-06-25 14:57:07 -07003281 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 sd->nr_balance_failed = 0;
3283
Nick Piggin81026792005-06-25 14:57:07 -07003284 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 /* We were unbalanced, so reset the balancing interval */
3286 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003287 } else {
3288 /*
3289 * If we've begun active balancing, start to back off. This
3290 * case may not be covered by the all_pinned logic if there
3291 * is only 1 task on the busy runqueue (because we don't call
3292 * move_tasks).
3293 */
3294 if (sd->balance_interval < sd->max_interval)
3295 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 }
3297
Peter Williams43010652007-08-09 11:16:46 +02003298 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003299 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003300 return -1;
Peter Williams43010652007-08-09 11:16:46 +02003301 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
3303out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 schedstat_inc(sd, lb_balanced[idle]);
3305
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003306 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003307
3308out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003310 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3311 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 sd->balance_interval *= 2;
3313
Ingo Molnar48f24c42006-07-03 00:25:40 -07003314 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003315 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003316 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 return 0;
3318}
3319
3320/*
3321 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3322 * tasks if there is an imbalance.
3323 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003324 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 * this_rq is locked.
3326 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003327static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07003328load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
3330 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003331 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003333 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003334 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003335 int all_pinned = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003336 cpumask_t cpus = CPU_MASK_ALL;
Nick Piggin5969fe02005-09-10 00:26:19 -07003337
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003338 /*
3339 * When power savings policy is enabled for the parent domain, idle
3340 * sibling can pick up load irrespective of busy siblings. In this case,
3341 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003342 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003343 */
3344 if (sd->flags & SD_SHARE_CPUPOWER &&
3345 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003346 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Ingo Molnar2d723762007-10-15 17:00:12 +02003348 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003349redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003350 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003351 &sd_idle, &cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003353 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003354 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
3356
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003357 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003358 &cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003359 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003360 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003361 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 }
3363
Nick Piggindb935db2005-06-25 14:57:11 -07003364 BUG_ON(busiest == this_rq);
3365
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003366 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003367
Peter Williams43010652007-08-09 11:16:46 +02003368 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003369 if (busiest->nr_running > 1) {
3370 /* Attempt to move tasks */
3371 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003372 /* this_rq->clock is already updated */
3373 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003374 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003375 imbalance, sd, CPU_NEWLY_IDLE,
3376 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003377 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003378
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003379 if (unlikely(all_pinned)) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003380 cpu_clear(cpu_of(busiest), cpus);
3381 if (!cpus_empty(cpus))
3382 goto redo;
3383 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003384 }
3385
Peter Williams43010652007-08-09 11:16:46 +02003386 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003387 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003388 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3389 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003390 return -1;
3391 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003392 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Peter Williams43010652007-08-09 11:16:46 +02003394 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003395
3396out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003397 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003398 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003399 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003400 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003401 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003402
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
3406/*
3407 * idle_balance is called by schedule() if this_cpu is about to become
3408 * idle. Attempts to pull tasks from other CPUs.
3409 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003410static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411{
3412 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003413 int pulled_task = -1;
3414 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
3416 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003417 unsigned long interval;
3418
3419 if (!(sd->flags & SD_LOAD_BALANCE))
3420 continue;
3421
3422 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003423 /* If we've pulled tasks over stop searching: */
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003424 pulled_task = load_balance_newidle(this_cpu,
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003425 this_rq, sd);
3426
3427 interval = msecs_to_jiffies(sd->balance_interval);
3428 if (time_after(next_balance, sd->last_balance + interval))
3429 next_balance = sd->last_balance + interval;
3430 if (pulled_task)
3431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003433 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003434 /*
3435 * We are going idle. next_balance may be set based on
3436 * a busy processor. So reset next_balance.
3437 */
3438 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440}
3441
3442/*
3443 * active_load_balance is run by migration threads. It pushes running tasks
3444 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3445 * running on each physical CPU where possible, and avoids physical /
3446 * logical imbalances.
3447 *
3448 * Called with busiest_rq locked.
3449 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003450static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
Nick Piggin39507452005-06-25 14:57:09 -07003452 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003453 struct sched_domain *sd;
3454 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003455
Ingo Molnar48f24c42006-07-03 00:25:40 -07003456 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003457 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003458 return;
3459
3460 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
3462 /*
Nick Piggin39507452005-06-25 14:57:09 -07003463 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003464 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003465 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 */
Nick Piggin39507452005-06-25 14:57:09 -07003467 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Nick Piggin39507452005-06-25 14:57:09 -07003469 /* move a task from busiest_rq to target_rq */
3470 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003471 update_rq_clock(busiest_rq);
3472 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Nick Piggin39507452005-06-25 14:57:09 -07003474 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003475 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003476 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003477 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003478 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Ingo Molnar48f24c42006-07-03 00:25:40 -07003481 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003482 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Peter Williams43010652007-08-09 11:16:46 +02003484 if (move_one_task(target_rq, target_cpu, busiest_rq,
3485 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003486 schedstat_inc(sd, alb_pushed);
3487 else
3488 schedstat_inc(sd, alb_failed);
3489 }
Nick Piggin39507452005-06-25 14:57:09 -07003490 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491}
3492
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003493#ifdef CONFIG_NO_HZ
3494static struct {
3495 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003496 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003497} nohz ____cacheline_aligned = {
3498 .load_balancer = ATOMIC_INIT(-1),
3499 .cpu_mask = CPU_MASK_NONE,
3500};
3501
Christoph Lameter7835b982006-12-10 02:20:22 -08003502/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003503 * This routine will try to nominate the ilb (idle load balancing)
3504 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3505 * load balancing on behalf of all those cpus. If all the cpus in the system
3506 * go into this tickless mode, then there will be no ilb owner (as there is
3507 * no need for one) and all the cpus will sleep till the next wakeup event
3508 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003509 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003510 * For the ilb owner, tick is not stopped. And this tick will be used
3511 * for idle load balancing. ilb owner will still be part of
3512 * nohz.cpu_mask..
3513 *
3514 * While stopping the tick, this cpu will become the ilb owner if there
3515 * is no other owner. And will be the owner till that cpu becomes busy
3516 * or if all cpus in the system stop their ticks at which point
3517 * there is no need for ilb owner.
3518 *
3519 * When the ilb owner becomes busy, it nominates another owner, during the
3520 * next busy scheduler_tick()
3521 */
3522int select_nohz_load_balancer(int stop_tick)
3523{
3524 int cpu = smp_processor_id();
3525
3526 if (stop_tick) {
3527 cpu_set(cpu, nohz.cpu_mask);
3528 cpu_rq(cpu)->in_nohz_recently = 1;
3529
3530 /*
3531 * If we are going offline and still the leader, give up!
3532 */
3533 if (cpu_is_offline(cpu) &&
3534 atomic_read(&nohz.load_balancer) == cpu) {
3535 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3536 BUG();
3537 return 0;
3538 }
3539
3540 /* time for ilb owner also to sleep */
3541 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3542 if (atomic_read(&nohz.load_balancer) == cpu)
3543 atomic_set(&nohz.load_balancer, -1);
3544 return 0;
3545 }
3546
3547 if (atomic_read(&nohz.load_balancer) == -1) {
3548 /* make me the ilb owner */
3549 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3550 return 1;
3551 } else if (atomic_read(&nohz.load_balancer) == cpu)
3552 return 1;
3553 } else {
3554 if (!cpu_isset(cpu, nohz.cpu_mask))
3555 return 0;
3556
3557 cpu_clear(cpu, nohz.cpu_mask);
3558
3559 if (atomic_read(&nohz.load_balancer) == cpu)
3560 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3561 BUG();
3562 }
3563 return 0;
3564}
3565#endif
3566
3567static DEFINE_SPINLOCK(balancing);
3568
3569/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003570 * It checks each scheduling domain to see if it is due to be balanced,
3571 * and initiates a balancing operation if so.
3572 *
3573 * Balancing parameters are set up in arch_init_sched_domains.
3574 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003575static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003576{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003577 int balance = 1;
3578 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003579 unsigned long interval;
3580 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003581 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003582 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003583 int update_next_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003585 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 if (!(sd->flags & SD_LOAD_BALANCE))
3587 continue;
3588
3589 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003590 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 interval *= sd->busy_factor;
3592
3593 /* scale ms to jiffies */
3594 interval = msecs_to_jiffies(interval);
3595 if (unlikely(!interval))
3596 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003597 if (interval > HZ*NR_CPUS/10)
3598 interval = HZ*NR_CPUS/10;
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Christoph Lameter08c183f2006-12-10 02:20:29 -08003601 if (sd->flags & SD_SERIALIZE) {
3602 if (!spin_trylock(&balancing))
3603 goto out;
3604 }
3605
Christoph Lameterc9819f42006-12-10 02:20:25 -08003606 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003607 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003608 /*
3609 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003610 * longer idle, or one of our SMT siblings is
3611 * not idle.
3612 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003613 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003615 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003617 if (sd->flags & SD_SERIALIZE)
3618 spin_unlock(&balancing);
3619out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003620 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003621 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003622 update_next_balance = 1;
3623 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003624
3625 /*
3626 * Stop the load balance at this level. There is another
3627 * CPU in our sched group which is doing load balancing more
3628 * actively.
3629 */
3630 if (!balance)
3631 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003633
3634 /*
3635 * next_balance will be updated only when there is a need.
3636 * When the cpu is attached to null domain for ex, it will not be
3637 * updated.
3638 */
3639 if (likely(update_next_balance))
3640 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003641}
3642
3643/*
3644 * run_rebalance_domains is triggered when needed from the scheduler tick.
3645 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3646 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3647 */
3648static void run_rebalance_domains(struct softirq_action *h)
3649{
Ingo Molnardd41f592007-07-09 18:51:59 +02003650 int this_cpu = smp_processor_id();
3651 struct rq *this_rq = cpu_rq(this_cpu);
3652 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3653 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003654
Ingo Molnardd41f592007-07-09 18:51:59 +02003655 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003656
3657#ifdef CONFIG_NO_HZ
3658 /*
3659 * If this cpu is the owner for idle load balancing, then do the
3660 * balancing on behalf of the other idle cpus whose ticks are
3661 * stopped.
3662 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003663 if (this_rq->idle_at_tick &&
3664 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003665 cpumask_t cpus = nohz.cpu_mask;
3666 struct rq *rq;
3667 int balance_cpu;
3668
Ingo Molnardd41f592007-07-09 18:51:59 +02003669 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003670 for_each_cpu_mask(balance_cpu, cpus) {
3671 /*
3672 * If this cpu gets work to do, stop the load balancing
3673 * work being done for other cpus. Next load
3674 * balancing owner will pick it up.
3675 */
3676 if (need_resched())
3677 break;
3678
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003679 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003680
3681 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003682 if (time_after(this_rq->next_balance, rq->next_balance))
3683 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003684 }
3685 }
3686#endif
3687}
3688
3689/*
3690 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3691 *
3692 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3693 * idle load balancing owner or decide to stop the periodic load balancing,
3694 * if the whole system is idle.
3695 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003696static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003697{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003698#ifdef CONFIG_NO_HZ
3699 /*
3700 * If we were in the nohz mode recently and busy at the current
3701 * scheduler tick, then check if we need to nominate new idle
3702 * load balancer.
3703 */
3704 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3705 rq->in_nohz_recently = 0;
3706
3707 if (atomic_read(&nohz.load_balancer) == cpu) {
3708 cpu_clear(cpu, nohz.cpu_mask);
3709 atomic_set(&nohz.load_balancer, -1);
3710 }
3711
3712 if (atomic_read(&nohz.load_balancer) == -1) {
3713 /*
3714 * simple selection for now: Nominate the
3715 * first cpu in the nohz list to be the next
3716 * ilb owner.
3717 *
3718 * TBD: Traverse the sched domains and nominate
3719 * the nearest cpu in the nohz.cpu_mask.
3720 */
3721 int ilb = first_cpu(nohz.cpu_mask);
3722
3723 if (ilb != NR_CPUS)
3724 resched_cpu(ilb);
3725 }
3726 }
3727
3728 /*
3729 * If this cpu is idle and doing idle load balancing for all the
3730 * cpus with ticks stopped, is it time for that to stop?
3731 */
3732 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3733 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3734 resched_cpu(cpu);
3735 return;
3736 }
3737
3738 /*
3739 * If this cpu is idle and the idle load balancing is done by
3740 * someone else, then no need raise the SCHED_SOFTIRQ
3741 */
3742 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3743 cpu_isset(cpu, nohz.cpu_mask))
3744 return;
3745#endif
3746 if (time_after_eq(jiffies, rq->next_balance))
3747 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748}
Ingo Molnardd41f592007-07-09 18:51:59 +02003749
3750#else /* CONFIG_SMP */
3751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752/*
3753 * on UP we do not need to balance between CPUs:
3754 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003755static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
3757}
Ingo Molnardd41f592007-07-09 18:51:59 +02003758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759#endif
3760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761DEFINE_PER_CPU(struct kernel_stat, kstat);
3762
3763EXPORT_PER_CPU_SYMBOL(kstat);
3764
3765/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003766 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3767 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003769unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003772 u64 ns, delta_exec;
3773 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003774
Ingo Molnar41b86e92007-07-09 18:51:58 +02003775 rq = task_rq_lock(p, &flags);
3776 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003777 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003778 update_rq_clock(rq);
3779 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003780 if ((s64)delta_exec > 0)
3781 ns += delta_exec;
3782 }
3783 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003784
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 return ns;
3786}
3787
3788/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 * Account user cpu time to a process.
3790 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 * @cputime: the cpu time spent in user space since the last update
3792 */
3793void account_user_time(struct task_struct *p, cputime_t cputime)
3794{
3795 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3796 cputime64_t tmp;
3797
3798 p->utime = cputime_add(p->utime, cputime);
3799
3800 /* Add user time to cpustat. */
3801 tmp = cputime_to_cputime64(cputime);
3802 if (TASK_NICE(p) > 0)
3803 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3804 else
3805 cpustat->user = cputime64_add(cpustat->user, tmp);
3806}
3807
3808/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003809 * Account guest cpu time to a process.
3810 * @p: the process that the cpu time gets accounted to
3811 * @cputime: the cpu time spent in virtual machine since the last update
3812 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01003813static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02003814{
3815 cputime64_t tmp;
3816 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3817
3818 tmp = cputime_to_cputime64(cputime);
3819
3820 p->utime = cputime_add(p->utime, cputime);
3821 p->gtime = cputime_add(p->gtime, cputime);
3822
3823 cpustat->user = cputime64_add(cpustat->user, tmp);
3824 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3825}
3826
3827/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003828 * Account scaled user cpu time to a process.
3829 * @p: the process that the cpu time gets accounted to
3830 * @cputime: the cpu time spent in user space since the last update
3831 */
3832void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
3833{
3834 p->utimescaled = cputime_add(p->utimescaled, cputime);
3835}
3836
3837/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 * Account system cpu time to a process.
3839 * @p: the process that the cpu time gets accounted to
3840 * @hardirq_offset: the offset to subtract from hardirq_count()
3841 * @cputime: the cpu time spent in kernel space since the last update
3842 */
3843void account_system_time(struct task_struct *p, int hardirq_offset,
3844 cputime_t cputime)
3845{
3846 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003847 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 cputime64_t tmp;
3849
Christian Borntraeger97783852007-11-15 20:57:39 +01003850 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
3851 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003852
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 p->stime = cputime_add(p->stime, cputime);
3854
3855 /* Add system time to cpustat. */
3856 tmp = cputime_to_cputime64(cputime);
3857 if (hardirq_count() - hardirq_offset)
3858 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3859 else if (softirq_count())
3860 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003861 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003863 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3865 else
3866 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3867 /* Account for system time used */
3868 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869}
3870
3871/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003872 * Account scaled system cpu time to a process.
3873 * @p: the process that the cpu time gets accounted to
3874 * @hardirq_offset: the offset to subtract from hardirq_count()
3875 * @cputime: the cpu time spent in kernel space since the last update
3876 */
3877void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
3878{
3879 p->stimescaled = cputime_add(p->stimescaled, cputime);
3880}
3881
3882/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 * Account for involuntary wait time.
3884 * @p: the process from which the cpu time has been stolen
3885 * @steal: the cpu time spent in involuntary wait
3886 */
3887void account_steal_time(struct task_struct *p, cputime_t steal)
3888{
3889 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3890 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003891 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
3893 if (p == rq->idle) {
3894 p->stime = cputime_add(p->stime, steal);
3895 if (atomic_read(&rq->nr_iowait) > 0)
3896 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3897 else
3898 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003899 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3901}
3902
Christoph Lameter7835b982006-12-10 02:20:22 -08003903/*
3904 * This function gets called by the timer code, with HZ frequency.
3905 * We call it with interrupts disabled.
3906 *
3907 * It also gets called by the fork code, when changing the parent's
3908 * timeslices.
3909 */
3910void scheduler_tick(void)
3911{
Christoph Lameter7835b982006-12-10 02:20:22 -08003912 int cpu = smp_processor_id();
3913 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003914 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02003915 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08003916
Ingo Molnardd41f592007-07-09 18:51:59 +02003917 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02003918 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02003919 /*
3920 * Let rq->clock advance by at least TICK_NSEC:
3921 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003922 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02003923 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003924 rq->clock_underflows++;
3925 }
Ingo Molnar529c7722007-08-10 23:05:11 +02003926 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02003927 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003928 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003929 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02003930 spin_unlock(&rq->lock);
3931
Christoph Lametere418e1c2006-12-10 02:20:23 -08003932#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003933 rq->idle_at_tick = idle_cpu(cpu);
3934 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003935#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936}
3937
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3939
Srinivasa Ds43627582008-02-23 15:24:04 -08003940void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
3942 /*
3943 * Underflow?
3944 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003945 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3946 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 preempt_count() += val;
3948 /*
3949 * Spinlock count overflowing soon?
3950 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003951 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3952 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953}
3954EXPORT_SYMBOL(add_preempt_count);
3955
Srinivasa Ds43627582008-02-23 15:24:04 -08003956void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957{
3958 /*
3959 * Underflow?
3960 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003961 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3962 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 /*
3964 * Is the spinlock portion underflowing?
3965 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003966 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3967 !(preempt_count() & PREEMPT_MASK)))
3968 return;
3969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 preempt_count() -= val;
3971}
3972EXPORT_SYMBOL(sub_preempt_count);
3973
3974#endif
3975
3976/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003977 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003979static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
Satyam Sharma838225b2007-10-24 18:23:50 +02003981 struct pt_regs *regs = get_irq_regs();
3982
3983 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3984 prev->comm, prev->pid, preempt_count());
3985
Ingo Molnardd41f592007-07-09 18:51:59 +02003986 debug_show_held_locks(prev);
3987 if (irqs_disabled())
3988 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02003989
3990 if (regs)
3991 show_regs(regs);
3992 else
3993 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02003994}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Ingo Molnardd41f592007-07-09 18:51:59 +02003996/*
3997 * Various schedule()-time debugging checks and statistics:
3998 */
3999static inline void schedule_debug(struct task_struct *prev)
4000{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004002 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 * schedule() atomically, we ignore that path for now.
4004 * Otherwise, whine if we are scheduling when we should not be.
4005 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004006 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4007 __schedule_bug(prev);
4008
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4010
Ingo Molnar2d723762007-10-15 17:00:12 +02004011 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004012#ifdef CONFIG_SCHEDSTATS
4013 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004014 schedstat_inc(this_rq(), bkl_count);
4015 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004016 }
4017#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004018}
4019
4020/*
4021 * Pick up the highest-prio task:
4022 */
4023static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004024pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004025{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004026 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004027 struct task_struct *p;
4028
4029 /*
4030 * Optimization: we know that if all tasks are in
4031 * the fair class we can call that function directly:
4032 */
4033 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004034 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004035 if (likely(p))
4036 return p;
4037 }
4038
4039 class = sched_class_highest;
4040 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004041 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004042 if (p)
4043 return p;
4044 /*
4045 * Will never be NULL as the idle class always
4046 * returns a non-NULL p:
4047 */
4048 class = class->next;
4049 }
4050}
4051
4052/*
4053 * schedule() is the main scheduler function.
4054 */
4055asmlinkage void __sched schedule(void)
4056{
4057 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004058 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004059 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004060 int cpu;
4061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062need_resched:
4063 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004064 cpu = smp_processor_id();
4065 rq = cpu_rq(cpu);
4066 rcu_qsctr_inc(cpu);
4067 prev = rq->curr;
4068 switch_count = &prev->nivcsw;
4069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 release_kernel_lock(prev);
4071need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Ingo Molnardd41f592007-07-09 18:51:59 +02004073 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004075 hrtick_clear(rq);
4076
Ingo Molnar1e819952007-10-15 17:00:13 +02004077 /*
4078 * Do the rq-clock update outside the rq lock:
4079 */
4080 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004081 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004082 spin_lock(&rq->lock);
4083 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Ingo Molnardd41f592007-07-09 18:51:59 +02004085 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4086 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004087 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004088 prev->state = TASK_RUNNING;
4089 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004090 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004091 }
4092 switch_count = &prev->nvcsw;
4093 }
4094
Steven Rostedt9a897c52008-01-25 21:08:22 +01004095#ifdef CONFIG_SMP
4096 if (prev->sched_class->pre_schedule)
4097 prev->sched_class->pre_schedule(rq, prev);
4098#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004099
Ingo Molnardd41f592007-07-09 18:51:59 +02004100 if (unlikely(!rq->nr_running))
4101 idle_balance(cpu, rq);
4102
Ingo Molnar31ee5292007-08-09 11:16:49 +02004103 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004104 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
4106 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02004107
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 rq->nr_switches++;
4110 rq->curr = next;
4111 ++*switch_count;
4112
Ingo Molnardd41f592007-07-09 18:51:59 +02004113 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004114 /*
4115 * the context switch might have flipped the stack from under
4116 * us, hence refresh the local variables.
4117 */
4118 cpu = smp_processor_id();
4119 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 } else
4121 spin_unlock_irq(&rq->lock);
4122
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004123 hrtick_set(rq);
4124
4125 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 preempt_enable_no_resched();
4129 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4130 goto need_resched;
4131}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132EXPORT_SYMBOL(schedule);
4133
4134#ifdef CONFIG_PREEMPT
4135/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004136 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004137 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 * occur there and call schedule directly.
4139 */
4140asmlinkage void __sched preempt_schedule(void)
4141{
4142 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 struct task_struct *task = current;
4144 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 /*
4147 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004148 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004150 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 return;
4152
Andi Kleen3a5c3592007-10-15 17:00:14 +02004153 do {
4154 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Andi Kleen3a5c3592007-10-15 17:00:14 +02004156 /*
4157 * We keep the big kernel semaphore locked, but we
4158 * clear ->lock_depth so that schedule() doesnt
4159 * auto-release the semaphore:
4160 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004161 saved_lock_depth = task->lock_depth;
4162 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004163 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004164 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004165 sub_preempt_count(PREEMPT_ACTIVE);
4166
4167 /*
4168 * Check again in case we missed a preemption opportunity
4169 * between schedule and now.
4170 */
4171 barrier();
4172 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174EXPORT_SYMBOL(preempt_schedule);
4175
4176/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004177 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 * off of irq context.
4179 * Note, that this is called and return with irqs disabled. This will
4180 * protect us against recursive calling from irq.
4181 */
4182asmlinkage void __sched preempt_schedule_irq(void)
4183{
4184 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 struct task_struct *task = current;
4186 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004187
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004188 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 BUG_ON(ti->preempt_count || !irqs_disabled());
4190
Andi Kleen3a5c3592007-10-15 17:00:14 +02004191 do {
4192 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
Andi Kleen3a5c3592007-10-15 17:00:14 +02004194 /*
4195 * We keep the big kernel semaphore locked, but we
4196 * clear ->lock_depth so that schedule() doesnt
4197 * auto-release the semaphore:
4198 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004199 saved_lock_depth = task->lock_depth;
4200 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004201 local_irq_enable();
4202 schedule();
4203 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004204 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004205 sub_preempt_count(PREEMPT_ACTIVE);
4206
4207 /*
4208 * Check again in case we missed a preemption opportunity
4209 * between schedule and now.
4210 */
4211 barrier();
4212 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
4215#endif /* CONFIG_PREEMPT */
4216
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004217int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4218 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004220 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222EXPORT_SYMBOL(default_wake_function);
4223
4224/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004225 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4226 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 * number) then we wake all the non-exclusive tasks and one exclusive task.
4228 *
4229 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004230 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4232 */
4233static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4234 int nr_exclusive, int sync, void *key)
4235{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004236 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004238 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004239 unsigned flags = curr->flags;
4240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004242 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 break;
4244 }
4245}
4246
4247/**
4248 * __wake_up - wake up threads blocked on a waitqueue.
4249 * @q: the waitqueue
4250 * @mode: which threads
4251 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004252 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004254void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004255 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256{
4257 unsigned long flags;
4258
4259 spin_lock_irqsave(&q->lock, flags);
4260 __wake_up_common(q, mode, nr_exclusive, 0, key);
4261 spin_unlock_irqrestore(&q->lock, flags);
4262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263EXPORT_SYMBOL(__wake_up);
4264
4265/*
4266 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4267 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004268void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
4270 __wake_up_common(q, mode, 1, 0, NULL);
4271}
4272
4273/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004274 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 * @q: the waitqueue
4276 * @mode: which threads
4277 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4278 *
4279 * The sync wakeup differs that the waker knows that it will schedule
4280 * away soon, so while the target thread will be woken up, it will not
4281 * be migrated to another CPU - ie. the two threads are 'synchronized'
4282 * with each other. This can prevent needless bouncing between CPUs.
4283 *
4284 * On UP it can prevent extra preemption.
4285 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004286void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004287__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
4289 unsigned long flags;
4290 int sync = 1;
4291
4292 if (unlikely(!q))
4293 return;
4294
4295 if (unlikely(!nr_exclusive))
4296 sync = 0;
4297
4298 spin_lock_irqsave(&q->lock, flags);
4299 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4300 spin_unlock_irqrestore(&q->lock, flags);
4301}
4302EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4303
Ingo Molnarb15136e2007-10-24 18:23:48 +02004304void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
4306 unsigned long flags;
4307
4308 spin_lock_irqsave(&x->wait.lock, flags);
4309 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004310 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 spin_unlock_irqrestore(&x->wait.lock, flags);
4312}
4313EXPORT_SYMBOL(complete);
4314
Ingo Molnarb15136e2007-10-24 18:23:48 +02004315void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316{
4317 unsigned long flags;
4318
4319 spin_lock_irqsave(&x->wait.lock, flags);
4320 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004321 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 spin_unlock_irqrestore(&x->wait.lock, flags);
4323}
4324EXPORT_SYMBOL(complete_all);
4325
Andi Kleen8cbbe862007-10-15 17:00:14 +02004326static inline long __sched
4327do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 if (!x->done) {
4330 DECLARE_WAITQUEUE(wait, current);
4331
4332 wait.flags |= WQ_FLAG_EXCLUSIVE;
4333 __add_wait_queue_tail(&x->wait, &wait);
4334 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004335 if ((state == TASK_INTERRUPTIBLE &&
4336 signal_pending(current)) ||
4337 (state == TASK_KILLABLE &&
4338 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004339 __remove_wait_queue(&x->wait, &wait);
4340 return -ERESTARTSYS;
4341 }
4342 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004344 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004346 if (!timeout) {
4347 __remove_wait_queue(&x->wait, &wait);
4348 return timeout;
4349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 } while (!x->done);
4351 __remove_wait_queue(&x->wait, &wait);
4352 }
4353 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004354 return timeout;
4355}
4356
4357static long __sched
4358wait_for_common(struct completion *x, long timeout, int state)
4359{
4360 might_sleep();
4361
4362 spin_lock_irq(&x->wait.lock);
4363 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004365 return timeout;
4366}
4367
Ingo Molnarb15136e2007-10-24 18:23:48 +02004368void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004369{
4370 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371}
4372EXPORT_SYMBOL(wait_for_completion);
4373
Ingo Molnarb15136e2007-10-24 18:23:48 +02004374unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4376{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004377 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378}
4379EXPORT_SYMBOL(wait_for_completion_timeout);
4380
Andi Kleen8cbbe862007-10-15 17:00:14 +02004381int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382{
Andi Kleen51e97992007-10-18 21:32:55 +02004383 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4384 if (t == -ERESTARTSYS)
4385 return t;
4386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387}
4388EXPORT_SYMBOL(wait_for_completion_interruptible);
4389
Ingo Molnarb15136e2007-10-24 18:23:48 +02004390unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391wait_for_completion_interruptible_timeout(struct completion *x,
4392 unsigned long timeout)
4393{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004394 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395}
4396EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4397
Matthew Wilcox009e5772007-12-06 12:29:54 -05004398int __sched wait_for_completion_killable(struct completion *x)
4399{
4400 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4401 if (t == -ERESTARTSYS)
4402 return t;
4403 return 0;
4404}
4405EXPORT_SYMBOL(wait_for_completion_killable);
4406
Andi Kleen8cbbe862007-10-15 17:00:14 +02004407static long __sched
4408sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004409{
4410 unsigned long flags;
4411 wait_queue_t wait;
4412
4413 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
Andi Kleen8cbbe862007-10-15 17:00:14 +02004415 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Andi Kleen8cbbe862007-10-15 17:00:14 +02004417 spin_lock_irqsave(&q->lock, flags);
4418 __add_wait_queue(q, &wait);
4419 spin_unlock(&q->lock);
4420 timeout = schedule_timeout(timeout);
4421 spin_lock_irq(&q->lock);
4422 __remove_wait_queue(q, &wait);
4423 spin_unlock_irqrestore(&q->lock, flags);
4424
4425 return timeout;
4426}
4427
4428void __sched interruptible_sleep_on(wait_queue_head_t *q)
4429{
4430 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432EXPORT_SYMBOL(interruptible_sleep_on);
4433
Ingo Molnar0fec1712007-07-09 18:52:01 +02004434long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004435interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004437 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4440
Ingo Molnar0fec1712007-07-09 18:52:01 +02004441void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004443 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445EXPORT_SYMBOL(sleep_on);
4446
Ingo Molnar0fec1712007-07-09 18:52:01 +02004447long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004449 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451EXPORT_SYMBOL(sleep_on_timeout);
4452
Ingo Molnarb29739f2006-06-27 02:54:51 -07004453#ifdef CONFIG_RT_MUTEXES
4454
4455/*
4456 * rt_mutex_setprio - set the current priority of a task
4457 * @p: task
4458 * @prio: prio value (kernel-internal form)
4459 *
4460 * This function changes the 'effective' priority of a task. It does
4461 * not touch ->normal_prio like __setscheduler().
4462 *
4463 * Used by the rt_mutex code to implement priority inheritance logic.
4464 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004465void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004466{
4467 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004468 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004469 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004470 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004471
4472 BUG_ON(prio < 0 || prio > MAX_PRIO);
4473
4474 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004475 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004476
Andrew Mortond5f9f942007-05-08 20:27:06 -07004477 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004478 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004479 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004480 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004481 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004482 if (running)
4483 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004484
4485 if (rt_prio(prio))
4486 p->sched_class = &rt_sched_class;
4487 else
4488 p->sched_class = &fair_sched_class;
4489
Ingo Molnarb29739f2006-06-27 02:54:51 -07004490 p->prio = prio;
4491
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004492 if (running)
4493 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004494 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004495 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004496
4497 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004498 }
4499 task_rq_unlock(rq, &flags);
4500}
4501
4502#endif
4503
Ingo Molnar36c8b582006-07-03 00:25:41 -07004504void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
Ingo Molnardd41f592007-07-09 18:51:59 +02004506 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004508 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
4510 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4511 return;
4512 /*
4513 * We have to be careful, if called from sys_setpriority(),
4514 * the task might be in the middle of scheduling on another CPU.
4515 */
4516 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004517 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 /*
4519 * The RT priorities are set via sched_setscheduler(), but we still
4520 * allow the 'normal' nice value to be set - but as expected
4521 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004522 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004524 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 p->static_prio = NICE_TO_PRIO(nice);
4526 goto out_unlock;
4527 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004528 on_rq = p->se.on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004529 if (on_rq) {
Ingo Molnar69be72c2007-08-09 11:16:49 +02004530 dequeue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004531 dec_load(rq, p);
4532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004535 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004536 old_prio = p->prio;
4537 p->prio = effective_prio(p);
4538 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Ingo Molnardd41f592007-07-09 18:51:59 +02004540 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004541 enqueue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004542 inc_load(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004544 * If the task increased its priority or is running and
4545 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004547 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 resched_task(rq->curr);
4549 }
4550out_unlock:
4551 task_rq_unlock(rq, &flags);
4552}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553EXPORT_SYMBOL(set_user_nice);
4554
Matt Mackalle43379f2005-05-01 08:59:00 -07004555/*
4556 * can_nice - check if a task can reduce its nice value
4557 * @p: task
4558 * @nice: nice value
4559 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004560int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004561{
Matt Mackall024f4742005-08-18 11:24:19 -07004562 /* convert nice value [19,-20] to rlimit style value [1,40] */
4563 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004564
Matt Mackalle43379f2005-05-01 08:59:00 -07004565 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4566 capable(CAP_SYS_NICE));
4567}
4568
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569#ifdef __ARCH_WANT_SYS_NICE
4570
4571/*
4572 * sys_nice - change the priority of the current process.
4573 * @increment: priority increment
4574 *
4575 * sys_setpriority is a more generic, but much slower function that
4576 * does similar things.
4577 */
4578asmlinkage long sys_nice(int increment)
4579{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004580 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
4582 /*
4583 * Setpriority might change our priority at the same moment.
4584 * We don't have to worry. Conceptually one call occurs first
4585 * and we have a single winner.
4586 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004587 if (increment < -40)
4588 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 if (increment > 40)
4590 increment = 40;
4591
4592 nice = PRIO_TO_NICE(current->static_prio) + increment;
4593 if (nice < -20)
4594 nice = -20;
4595 if (nice > 19)
4596 nice = 19;
4597
Matt Mackalle43379f2005-05-01 08:59:00 -07004598 if (increment < 0 && !can_nice(current, nice))
4599 return -EPERM;
4600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 retval = security_task_setnice(current, nice);
4602 if (retval)
4603 return retval;
4604
4605 set_user_nice(current, nice);
4606 return 0;
4607}
4608
4609#endif
4610
4611/**
4612 * task_prio - return the priority value of a given task.
4613 * @p: the task in question.
4614 *
4615 * This is the priority value as seen by users in /proc.
4616 * RT tasks are offset by -200. Normal tasks are centered
4617 * around 0, value goes from -16 to +15.
4618 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004619int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620{
4621 return p->prio - MAX_RT_PRIO;
4622}
4623
4624/**
4625 * task_nice - return the nice value of a given task.
4626 * @p: the task in question.
4627 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004628int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629{
4630 return TASK_NICE(p);
4631}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004632EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
4634/**
4635 * idle_cpu - is a given cpu idle currently?
4636 * @cpu: the processor in question.
4637 */
4638int idle_cpu(int cpu)
4639{
4640 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4641}
4642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643/**
4644 * idle_task - return the idle task for a given cpu.
4645 * @cpu: the processor in question.
4646 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004647struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
4649 return cpu_rq(cpu)->idle;
4650}
4651
4652/**
4653 * find_process_by_pid - find a process with a matching PID value.
4654 * @pid: the pid in question.
4655 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004656static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004658 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659}
4660
4661/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004662static void
4663__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664{
Ingo Molnardd41f592007-07-09 18:51:59 +02004665 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004668 switch (p->policy) {
4669 case SCHED_NORMAL:
4670 case SCHED_BATCH:
4671 case SCHED_IDLE:
4672 p->sched_class = &fair_sched_class;
4673 break;
4674 case SCHED_FIFO:
4675 case SCHED_RR:
4676 p->sched_class = &rt_sched_class;
4677 break;
4678 }
4679
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004681 p->normal_prio = normal_prio(p);
4682 /* we are holding p->pi_lock already */
4683 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004684 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685}
4686
4687/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004688 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 * @p: the task in question.
4690 * @policy: new policy.
4691 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004692 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004693 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004695int sched_setscheduler(struct task_struct *p, int policy,
4696 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004698 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01004700 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004701 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
Steven Rostedt66e53932006-06-27 02:54:44 -07004703 /* may grab non-irq protected spin_locks */
4704 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705recheck:
4706 /* double check policy once rq lock held */
4707 if (policy < 0)
4708 policy = oldpolicy = p->policy;
4709 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004710 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4711 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004712 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 /*
4714 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004715 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4716 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 */
4718 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004719 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004720 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004722 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 return -EINVAL;
4724
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004725 /*
4726 * Allow unprivileged RT tasks to decrease priority:
4727 */
4728 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004729 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004730 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004731
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004732 if (!lock_task_sighand(p, &flags))
4733 return -ESRCH;
4734 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4735 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004736
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004737 /* can't set/change the rt policy */
4738 if (policy != p->policy && !rlim_rtprio)
4739 return -EPERM;
4740
4741 /* can't increase priority */
4742 if (param->sched_priority > p->rt_priority &&
4743 param->sched_priority > rlim_rtprio)
4744 return -EPERM;
4745 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004746 /*
4747 * Like positive nice levels, dont allow tasks to
4748 * move out of SCHED_IDLE either:
4749 */
4750 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4751 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004752
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004753 /* can't change other user's priorities */
4754 if ((current->euid != p->euid) &&
4755 (current->euid != p->uid))
4756 return -EPERM;
4757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004759#ifdef CONFIG_RT_GROUP_SCHED
4760 /*
4761 * Do not allow realtime tasks into groups that have no runtime
4762 * assigned.
4763 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02004764 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004765 return -EPERM;
4766#endif
4767
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 retval = security_task_setscheduler(p, policy, param);
4769 if (retval)
4770 return retval;
4771 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004772 * make sure no PI-waiters arrive (or leave) while we are
4773 * changing the priority of the task:
4774 */
4775 spin_lock_irqsave(&p->pi_lock, flags);
4776 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 * To be able to change p->policy safely, the apropriate
4778 * runqueue lock must be held.
4779 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004780 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 /* recheck policy now with rq lock held */
4782 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4783 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004784 __task_rq_unlock(rq);
4785 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 goto recheck;
4787 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004788 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004789 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004790 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004791 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004792 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004793 if (running)
4794 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02004795
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004797 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02004798
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004799 if (running)
4800 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004801 if (on_rq) {
4802 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004803
4804 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004806 __task_rq_unlock(rq);
4807 spin_unlock_irqrestore(&p->pi_lock, flags);
4808
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004809 rt_mutex_adjust_pi(p);
4810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 return 0;
4812}
4813EXPORT_SYMBOL_GPL(sched_setscheduler);
4814
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004815static int
4816do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 struct sched_param lparam;
4819 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004820 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
4822 if (!param || pid < 0)
4823 return -EINVAL;
4824 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4825 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004826
4827 rcu_read_lock();
4828 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004830 if (p != NULL)
4831 retval = sched_setscheduler(p, policy, &lparam);
4832 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004833
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 return retval;
4835}
4836
4837/**
4838 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4839 * @pid: the pid in question.
4840 * @policy: new policy.
4841 * @param: structure containing the new RT priority.
4842 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004843asmlinkage long
4844sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845{
Jason Baronc21761f2006-01-18 17:43:03 -08004846 /* negative values for policy are not valid */
4847 if (policy < 0)
4848 return -EINVAL;
4849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 return do_sched_setscheduler(pid, policy, param);
4851}
4852
4853/**
4854 * sys_sched_setparam - set/change the RT priority of a thread
4855 * @pid: the pid in question.
4856 * @param: structure containing the new RT priority.
4857 */
4858asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4859{
4860 return do_sched_setscheduler(pid, -1, param);
4861}
4862
4863/**
4864 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4865 * @pid: the pid in question.
4866 */
4867asmlinkage long sys_sched_getscheduler(pid_t pid)
4868{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004869 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004870 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
4872 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004873 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
4875 retval = -ESRCH;
4876 read_lock(&tasklist_lock);
4877 p = find_process_by_pid(pid);
4878 if (p) {
4879 retval = security_task_getscheduler(p);
4880 if (!retval)
4881 retval = p->policy;
4882 }
4883 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 return retval;
4885}
4886
4887/**
4888 * sys_sched_getscheduler - get the RT priority of a thread
4889 * @pid: the pid in question.
4890 * @param: structure containing the RT priority.
4891 */
4892asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4893{
4894 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004895 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004896 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
4898 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004899 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
4901 read_lock(&tasklist_lock);
4902 p = find_process_by_pid(pid);
4903 retval = -ESRCH;
4904 if (!p)
4905 goto out_unlock;
4906
4907 retval = security_task_getscheduler(p);
4908 if (retval)
4909 goto out_unlock;
4910
4911 lp.sched_priority = p->rt_priority;
4912 read_unlock(&tasklist_lock);
4913
4914 /*
4915 * This one might sleep, we cannot do it with a spinlock held ...
4916 */
4917 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4918
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 return retval;
4920
4921out_unlock:
4922 read_unlock(&tasklist_lock);
4923 return retval;
4924}
4925
4926long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4927{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 cpumask_t cpus_allowed;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004929 struct task_struct *p;
4930 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004932 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 read_lock(&tasklist_lock);
4934
4935 p = find_process_by_pid(pid);
4936 if (!p) {
4937 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004938 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 return -ESRCH;
4940 }
4941
4942 /*
4943 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004944 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 * usage count and then drop tasklist_lock.
4946 */
4947 get_task_struct(p);
4948 read_unlock(&tasklist_lock);
4949
4950 retval = -EPERM;
4951 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4952 !capable(CAP_SYS_NICE))
4953 goto out_unlock;
4954
David Quigleye7834f82006-06-23 02:03:59 -07004955 retval = security_task_setscheduler(p, 0, NULL);
4956 if (retval)
4957 goto out_unlock;
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 cpus_allowed = cpuset_cpus_allowed(p);
4960 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004961 again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 retval = set_cpus_allowed(p, new_mask);
4963
Paul Menage8707d8b2007-10-18 23:40:22 -07004964 if (!retval) {
4965 cpus_allowed = cpuset_cpus_allowed(p);
4966 if (!cpus_subset(new_mask, cpus_allowed)) {
4967 /*
4968 * We must have raced with a concurrent cpuset
4969 * update. Just reset the cpus_allowed to the
4970 * cpuset's cpus_allowed
4971 */
4972 new_mask = cpus_allowed;
4973 goto again;
4974 }
4975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976out_unlock:
4977 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004978 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 return retval;
4980}
4981
4982static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4983 cpumask_t *new_mask)
4984{
4985 if (len < sizeof(cpumask_t)) {
4986 memset(new_mask, 0, sizeof(cpumask_t));
4987 } else if (len > sizeof(cpumask_t)) {
4988 len = sizeof(cpumask_t);
4989 }
4990 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4991}
4992
4993/**
4994 * sys_sched_setaffinity - set the cpu affinity of a process
4995 * @pid: pid of the process
4996 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4997 * @user_mask_ptr: user-space pointer to the new cpu mask
4998 */
4999asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5000 unsigned long __user *user_mask_ptr)
5001{
5002 cpumask_t new_mask;
5003 int retval;
5004
5005 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5006 if (retval)
5007 return retval;
5008
5009 return sched_setaffinity(pid, new_mask);
5010}
5011
5012/*
5013 * Represents all cpu's present in the system
5014 * In systems capable of hotplug, this map could dynamically grow
5015 * as new cpu's are detected in the system via any platform specific
5016 * method, such as ACPI for e.g.
5017 */
5018
Andi Kleen4cef0c62006-01-11 22:44:57 +01005019cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020EXPORT_SYMBOL(cpu_present_map);
5021
5022#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005023cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005024EXPORT_SYMBOL(cpu_online_map);
5025
Andi Kleen4cef0c62006-01-11 22:44:57 +01005026cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005027EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028#endif
5029
5030long sched_getaffinity(pid_t pid, cpumask_t *mask)
5031{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005032 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005035 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 read_lock(&tasklist_lock);
5037
5038 retval = -ESRCH;
5039 p = find_process_by_pid(pid);
5040 if (!p)
5041 goto out_unlock;
5042
David Quigleye7834f82006-06-23 02:03:59 -07005043 retval = security_task_getscheduler(p);
5044 if (retval)
5045 goto out_unlock;
5046
Jack Steiner2f7016d2006-02-01 03:05:18 -08005047 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048
5049out_unlock:
5050 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005051 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
Ulrich Drepper9531b622007-08-09 11:16:46 +02005053 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054}
5055
5056/**
5057 * sys_sched_getaffinity - get the cpu affinity of a process
5058 * @pid: pid of the process
5059 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5060 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5061 */
5062asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5063 unsigned long __user *user_mask_ptr)
5064{
5065 int ret;
5066 cpumask_t mask;
5067
5068 if (len < sizeof(cpumask_t))
5069 return -EINVAL;
5070
5071 ret = sched_getaffinity(pid, &mask);
5072 if (ret < 0)
5073 return ret;
5074
5075 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5076 return -EFAULT;
5077
5078 return sizeof(cpumask_t);
5079}
5080
5081/**
5082 * sys_sched_yield - yield the current processor to other threads.
5083 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005084 * This function yields the current CPU to other tasks. If there are no
5085 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 */
5087asmlinkage long sys_sched_yield(void)
5088{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005089 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Ingo Molnar2d723762007-10-15 17:00:12 +02005091 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005092 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
5094 /*
5095 * Since we are going to call schedule() anyway, there's
5096 * no need to preempt or enable interrupts:
5097 */
5098 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005099 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 _raw_spin_unlock(&rq->lock);
5101 preempt_enable_no_resched();
5102
5103 schedule();
5104
5105 return 0;
5106}
5107
Andrew Mortone7b38402006-06-30 01:56:00 -07005108static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005110#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5111 __might_sleep(__FILE__, __LINE__);
5112#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005113 /*
5114 * The BKS might be reacquired before we have dropped
5115 * PREEMPT_ACTIVE, which could trigger a second
5116 * cond_resched() call.
5117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 do {
5119 add_preempt_count(PREEMPT_ACTIVE);
5120 schedule();
5121 sub_preempt_count(PREEMPT_ACTIVE);
5122 } while (need_resched());
5123}
5124
Herbert Xu02b67cc32008-01-25 21:08:28 +01005125#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5126int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127{
Ingo Molnar94142322006-12-29 16:48:13 -08005128 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5129 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 __cond_resched();
5131 return 1;
5132 }
5133 return 0;
5134}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005135EXPORT_SYMBOL(_cond_resched);
5136#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
5138/*
5139 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5140 * call schedule, and on return reacquire the lock.
5141 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005142 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 * operations here to prevent schedule() from being called twice (once via
5144 * spin_unlock(), once by hand).
5145 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005146int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147{
Nick Piggin95c354f2008-01-30 13:31:20 +01005148 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005149 int ret = 0;
5150
Nick Piggin95c354f2008-01-30 13:31:20 +01005151 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005153 if (resched && need_resched())
5154 __cond_resched();
5155 else
5156 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005157 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005160 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162EXPORT_SYMBOL(cond_resched_lock);
5163
5164int __sched cond_resched_softirq(void)
5165{
5166 BUG_ON(!in_softirq());
5167
Ingo Molnar94142322006-12-29 16:48:13 -08005168 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005169 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 __cond_resched();
5171 local_bh_disable();
5172 return 1;
5173 }
5174 return 0;
5175}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176EXPORT_SYMBOL(cond_resched_softirq);
5177
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178/**
5179 * yield - yield the current processor to other threads.
5180 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005181 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 * thread runnable and calls sys_sched_yield().
5183 */
5184void __sched yield(void)
5185{
5186 set_current_state(TASK_RUNNING);
5187 sys_sched_yield();
5188}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189EXPORT_SYMBOL(yield);
5190
5191/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005192 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 * that process accounting knows that this is a task in IO wait state.
5194 *
5195 * But don't do that if it is a deliberate, throttling IO wait (this task
5196 * has set its backing_dev_info: the queue against which it should throttle)
5197 */
5198void __sched io_schedule(void)
5199{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005200 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005202 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 atomic_inc(&rq->nr_iowait);
5204 schedule();
5205 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005206 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208EXPORT_SYMBOL(io_schedule);
5209
5210long __sched io_schedule_timeout(long timeout)
5211{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005212 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 long ret;
5214
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005215 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 atomic_inc(&rq->nr_iowait);
5217 ret = schedule_timeout(timeout);
5218 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005219 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 return ret;
5221}
5222
5223/**
5224 * sys_sched_get_priority_max - return maximum RT priority.
5225 * @policy: scheduling class.
5226 *
5227 * this syscall returns the maximum rt_priority that can be used
5228 * by a given scheduling class.
5229 */
5230asmlinkage long sys_sched_get_priority_max(int policy)
5231{
5232 int ret = -EINVAL;
5233
5234 switch (policy) {
5235 case SCHED_FIFO:
5236 case SCHED_RR:
5237 ret = MAX_USER_RT_PRIO-1;
5238 break;
5239 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005240 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005241 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 ret = 0;
5243 break;
5244 }
5245 return ret;
5246}
5247
5248/**
5249 * sys_sched_get_priority_min - return minimum RT priority.
5250 * @policy: scheduling class.
5251 *
5252 * this syscall returns the minimum rt_priority that can be used
5253 * by a given scheduling class.
5254 */
5255asmlinkage long sys_sched_get_priority_min(int policy)
5256{
5257 int ret = -EINVAL;
5258
5259 switch (policy) {
5260 case SCHED_FIFO:
5261 case SCHED_RR:
5262 ret = 1;
5263 break;
5264 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005265 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005266 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 ret = 0;
5268 }
5269 return ret;
5270}
5271
5272/**
5273 * sys_sched_rr_get_interval - return the default timeslice of a process.
5274 * @pid: pid of the process.
5275 * @interval: userspace pointer to the timeslice value.
5276 *
5277 * this syscall writes the default timeslice value of a given process
5278 * into the user-space timespec buffer. A value of '0' means infinity.
5279 */
5280asmlinkage
5281long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5282{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005283 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005284 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005285 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
5288 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
5291 retval = -ESRCH;
5292 read_lock(&tasklist_lock);
5293 p = find_process_by_pid(pid);
5294 if (!p)
5295 goto out_unlock;
5296
5297 retval = security_task_getscheduler(p);
5298 if (retval)
5299 goto out_unlock;
5300
Ingo Molnar77034932007-12-04 17:04:39 +01005301 /*
5302 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5303 * tasks that are on an otherwise idle runqueue:
5304 */
5305 time_slice = 0;
5306 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005307 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005308 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005309 struct sched_entity *se = &p->se;
5310 unsigned long flags;
5311 struct rq *rq;
5312
5313 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005314 if (rq->cfs.load.weight)
5315 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005316 task_rq_unlock(rq, &flags);
5317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005319 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005322
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323out_unlock:
5324 read_unlock(&tasklist_lock);
5325 return retval;
5326}
5327
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005328static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005329
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005330void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005333 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005336 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005337 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005338#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005340 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005342 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343#else
5344 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005345 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005347 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348#endif
5349#ifdef CONFIG_DEBUG_STACK_USAGE
5350 {
Al Viro10ebffd2005-11-13 16:06:56 -08005351 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 while (!*n)
5353 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005354 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 }
5356#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005357 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005358 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005360 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361}
5362
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005363void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005365 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
Ingo Molnar4bd77322007-07-11 21:21:47 +02005367#if BITS_PER_LONG == 32
5368 printk(KERN_INFO
5369 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005371 printk(KERN_INFO
5372 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373#endif
5374 read_lock(&tasklist_lock);
5375 do_each_thread(g, p) {
5376 /*
5377 * reset the NMI-timeout, listing all files on a slow
5378 * console might take alot of time:
5379 */
5380 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005381 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005382 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 } while_each_thread(g, p);
5384
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005385 touch_all_softlockup_watchdogs();
5386
Ingo Molnardd41f592007-07-09 18:51:59 +02005387#ifdef CONFIG_SCHED_DEBUG
5388 sysrq_sched_debug_show();
5389#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005391 /*
5392 * Only show locks if all tasks are dumped:
5393 */
5394 if (state_filter == -1)
5395 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396}
5397
Ingo Molnar1df21052007-07-09 18:51:58 +02005398void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5399{
Ingo Molnardd41f592007-07-09 18:51:59 +02005400 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005401}
5402
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005403/**
5404 * init_idle - set up an idle thread for a given CPU
5405 * @idle: task in question
5406 * @cpu: cpu the idle task belongs to
5407 *
5408 * NOTE: this function does not set the idle thread's NEED_RESCHED
5409 * flag, to make booting more robust.
5410 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005411void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005413 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 unsigned long flags;
5415
Ingo Molnardd41f592007-07-09 18:51:59 +02005416 __sched_fork(idle);
5417 idle->se.exec_start = sched_clock();
5418
Ingo Molnarb29739f2006-06-27 02:54:51 -07005419 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005421 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
5423 spin_lock_irqsave(&rq->lock, flags);
5424 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005425#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5426 idle->oncpu = 1;
5427#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 spin_unlock_irqrestore(&rq->lock, flags);
5429
5430 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f542005-11-13 16:06:55 -08005431 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005432
Ingo Molnardd41f592007-07-09 18:51:59 +02005433 /*
5434 * The idle tasks have their own, simple scheduling class:
5435 */
5436 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437}
5438
5439/*
5440 * In a system that switches off the HZ timer nohz_cpu_mask
5441 * indicates which cpus entered this state. This is used
5442 * in the rcu update to wait only for active cpus. For system
5443 * which do not switch off the HZ timer nohz_cpu_mask should
5444 * always be CPU_MASK_NONE.
5445 */
5446cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5447
Ingo Molnar19978ca2007-11-09 22:39:38 +01005448/*
5449 * Increase the granularity value when there are more CPUs,
5450 * because with more CPUs the 'effective latency' as visible
5451 * to users decreases. But the relationship is not linear,
5452 * so pick a second-best guess by going with the log2 of the
5453 * number of CPUs.
5454 *
5455 * This idea comes from the SD scheduler of Con Kolivas:
5456 */
5457static inline void sched_init_granularity(void)
5458{
5459 unsigned int factor = 1 + ilog2(num_online_cpus());
5460 const unsigned long limit = 200000000;
5461
5462 sysctl_sched_min_granularity *= factor;
5463 if (sysctl_sched_min_granularity > limit)
5464 sysctl_sched_min_granularity = limit;
5465
5466 sysctl_sched_latency *= factor;
5467 if (sysctl_sched_latency > limit)
5468 sysctl_sched_latency = limit;
5469
5470 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005471}
5472
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473#ifdef CONFIG_SMP
5474/*
5475 * This is how migration works:
5476 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005477 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 * runqueue and wake up that CPU's migration thread.
5479 * 2) we down() the locked semaphore => thread blocks.
5480 * 3) migration thread wakes up (implicitly it forces the migrated
5481 * thread off the CPU)
5482 * 4) it gets the migration request and checks whether the migrated
5483 * task is still in the wrong runqueue.
5484 * 5) if it's in the wrong runqueue then the migration thread removes
5485 * it and puts it into the right queue.
5486 * 6) migration thread up()s the semaphore.
5487 * 7) we wake up and the migration is done.
5488 */
5489
5490/*
5491 * Change a given task's CPU affinity. Migrate the thread to a
5492 * proper CPU and schedule it away if the CPU it's executing on
5493 * is removed from the allowed bitmask.
5494 *
5495 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005496 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 * call is not atomic; no spinlocks may be held.
5498 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005499int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005501 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005503 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005504 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
5506 rq = task_rq_lock(p, &flags);
5507 if (!cpus_intersects(new_mask, cpu_online_map)) {
5508 ret = -EINVAL;
5509 goto out;
5510 }
5511
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005512 if (p->sched_class->set_cpus_allowed)
5513 p->sched_class->set_cpus_allowed(p, &new_mask);
5514 else {
Ingo Molnar0eab9142008-01-25 21:08:19 +01005515 p->cpus_allowed = new_mask;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01005516 p->rt.nr_cpus_allowed = cpus_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005517 }
5518
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 /* Can the task run on the task's current CPU? If so, we're done */
5520 if (cpu_isset(task_cpu(p), new_mask))
5521 goto out;
5522
5523 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
5524 /* Need help from migration thread: drop lock and wait. */
5525 task_rq_unlock(rq, &flags);
5526 wake_up_process(rq->migration_thread);
5527 wait_for_completion(&req.done);
5528 tlb_migrate_finish(p->mm);
5529 return 0;
5530 }
5531out:
5532 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005533
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 return ret;
5535}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536EXPORT_SYMBOL_GPL(set_cpus_allowed);
5537
5538/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005539 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 * this because either it can't run here any more (set_cpus_allowed()
5541 * away from this CPU, or CPU going down), or because we're
5542 * attempting to rebalance this task on exec (sched_exec).
5543 *
5544 * So we race with normal scheduler movements, but that's OK, as long
5545 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005546 *
5547 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005549static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005551 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005552 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
5554 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005555 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
5557 rq_src = cpu_rq(src_cpu);
5558 rq_dest = cpu_rq(dest_cpu);
5559
5560 double_rq_lock(rq_src, rq_dest);
5561 /* Already moved. */
5562 if (task_cpu(p) != src_cpu)
5563 goto out;
5564 /* Affinity changed (again). */
5565 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5566 goto out;
5567
Ingo Molnardd41f592007-07-09 18:51:59 +02005568 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005569 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005570 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005571
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005573 if (on_rq) {
5574 activate_task(rq_dest, p, 0);
5575 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005577 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578out:
5579 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005580 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581}
5582
5583/*
5584 * migration_thread - this is a highprio system thread that performs
5585 * thread migration by bumping thread off CPU then 'pushing' onto
5586 * another runqueue.
5587 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005588static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005591 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
5593 rq = cpu_rq(cpu);
5594 BUG_ON(rq->migration_thread != current);
5595
5596 set_current_state(TASK_INTERRUPTIBLE);
5597 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005598 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 spin_lock_irq(&rq->lock);
5602
5603 if (cpu_is_offline(cpu)) {
5604 spin_unlock_irq(&rq->lock);
5605 goto wait_to_die;
5606 }
5607
5608 if (rq->active_balance) {
5609 active_load_balance(rq, cpu);
5610 rq->active_balance = 0;
5611 }
5612
5613 head = &rq->migration_queue;
5614
5615 if (list_empty(head)) {
5616 spin_unlock_irq(&rq->lock);
5617 schedule();
5618 set_current_state(TASK_INTERRUPTIBLE);
5619 continue;
5620 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005621 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 list_del_init(head->next);
5623
Nick Piggin674311d2005-06-25 14:57:27 -07005624 spin_unlock(&rq->lock);
5625 __migrate_task(req->task, cpu, req->dest_cpu);
5626 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
5628 complete(&req->done);
5629 }
5630 __set_current_state(TASK_RUNNING);
5631 return 0;
5632
5633wait_to_die:
5634 /* Wait for kthread_stop */
5635 set_current_state(TASK_INTERRUPTIBLE);
5636 while (!kthread_should_stop()) {
5637 schedule();
5638 set_current_state(TASK_INTERRUPTIBLE);
5639 }
5640 __set_current_state(TASK_RUNNING);
5641 return 0;
5642}
5643
5644#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005645
5646static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5647{
5648 int ret;
5649
5650 local_irq_disable();
5651 ret = __migrate_task(p, src_cpu, dest_cpu);
5652 local_irq_enable();
5653 return ret;
5654}
5655
Kirill Korotaev054b9102006-12-10 02:20:11 -08005656/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005657 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005658 * NOTE: interrupts should be disabled by the caller
5659 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005660static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005662 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005664 struct rq *rq;
5665 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
Andi Kleen3a5c3592007-10-15 17:00:14 +02005667 do {
5668 /* On same node? */
5669 mask = node_to_cpumask(cpu_to_node(dead_cpu));
5670 cpus_and(mask, mask, p->cpus_allowed);
5671 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
Andi Kleen3a5c3592007-10-15 17:00:14 +02005673 /* On any allowed CPU? */
5674 if (dest_cpu == NR_CPUS)
5675 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676
Andi Kleen3a5c3592007-10-15 17:00:14 +02005677 /* No more Mr. Nice Guy. */
5678 if (dest_cpu == NR_CPUS) {
Cliff Wickman470fd642007-10-18 23:40:46 -07005679 cpumask_t cpus_allowed = cpuset_cpus_allowed_locked(p);
5680 /*
5681 * Try to stay on the same cpuset, where the
5682 * current cpuset may be a subset of all cpus.
5683 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005684 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07005685 * called within calls to cpuset_lock/cpuset_unlock.
5686 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02005687 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07005688 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005689 dest_cpu = any_online_cpu(p->cpus_allowed);
5690 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
Andi Kleen3a5c3592007-10-15 17:00:14 +02005692 /*
5693 * Don't tell them about moving exiting tasks or
5694 * kernel threads (both mm NULL), since they never
5695 * leave kernel.
5696 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005697 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02005698 printk(KERN_INFO "process %d (%s) no "
5699 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005700 task_pid_nr(p), p->comm, dead_cpu);
5701 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02005702 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005703 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704}
5705
5706/*
5707 * While a dead CPU has no uninterruptible tasks queued at this point,
5708 * it might still have a nonzero ->nr_uninterruptible counter, because
5709 * for performance reasons the counter is not stricly tracking tasks to
5710 * their home CPUs. So we just add the counter to another CPU's counter,
5711 * to keep the global sum constant after CPU-down:
5712 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005713static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005715 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 unsigned long flags;
5717
5718 local_irq_save(flags);
5719 double_rq_lock(rq_src, rq_dest);
5720 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5721 rq_src->nr_uninterruptible = 0;
5722 double_rq_unlock(rq_src, rq_dest);
5723 local_irq_restore(flags);
5724}
5725
5726/* Run through task list and migrate tasks from the dead cpu. */
5727static void migrate_live_tasks(int src_cpu)
5728{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005729 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005731 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732
Ingo Molnar48f24c42006-07-03 00:25:40 -07005733 do_each_thread(t, p) {
5734 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 continue;
5736
Ingo Molnar48f24c42006-07-03 00:25:40 -07005737 if (task_cpu(p) == src_cpu)
5738 move_task_off_dead_cpu(src_cpu, p);
5739 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005741 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742}
5743
Ingo Molnardd41f592007-07-09 18:51:59 +02005744/*
5745 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005746 * It does so by boosting its priority to highest possible.
5747 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 */
5749void sched_idle_next(void)
5750{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005751 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005752 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 struct task_struct *p = rq->idle;
5754 unsigned long flags;
5755
5756 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005757 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
Ingo Molnar48f24c42006-07-03 00:25:40 -07005759 /*
5760 * Strictly not necessary since rest of the CPUs are stopped by now
5761 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 */
5763 spin_lock_irqsave(&rq->lock, flags);
5764
Ingo Molnardd41f592007-07-09 18:51:59 +02005765 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005766
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005767 update_rq_clock(rq);
5768 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769
5770 spin_unlock_irqrestore(&rq->lock, flags);
5771}
5772
Ingo Molnar48f24c42006-07-03 00:25:40 -07005773/*
5774 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 * offline.
5776 */
5777void idle_task_exit(void)
5778{
5779 struct mm_struct *mm = current->active_mm;
5780
5781 BUG_ON(cpu_online(smp_processor_id()));
5782
5783 if (mm != &init_mm)
5784 switch_mm(mm, &init_mm, current);
5785 mmdrop(mm);
5786}
5787
Kirill Korotaev054b9102006-12-10 02:20:11 -08005788/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005789static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005791 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
5793 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07005794 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795
5796 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005797 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
Ingo Molnar48f24c42006-07-03 00:25:40 -07005799 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
5801 /*
5802 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005803 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 * fine.
5805 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005806 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005807 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005808 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
Ingo Molnar48f24c42006-07-03 00:25:40 -07005810 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811}
5812
5813/* release_task() removes task from tasklist, so we won't find dead tasks. */
5814static void migrate_dead_tasks(unsigned int dead_cpu)
5815{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005816 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005817 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818
Ingo Molnardd41f592007-07-09 18:51:59 +02005819 for ( ; ; ) {
5820 if (!rq->nr_running)
5821 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005822 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02005823 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02005824 if (!next)
5825 break;
5826 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005827
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 }
5829}
5830#endif /* CONFIG_HOTPLUG_CPU */
5831
Nick Piggine692ab52007-07-26 13:40:43 +02005832#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5833
5834static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005835 {
5836 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005837 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005838 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005839 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005840};
5841
5842static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005843 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005844 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005845 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005846 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005847 .child = sd_ctl_dir,
5848 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005849 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005850};
5851
5852static struct ctl_table *sd_alloc_ctl_entry(int n)
5853{
5854 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005855 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005856
Nick Piggine692ab52007-07-26 13:40:43 +02005857 return entry;
5858}
5859
Milton Miller6382bc92007-10-15 17:00:19 +02005860static void sd_free_ctl_entry(struct ctl_table **tablep)
5861{
Milton Millercd7900762007-10-17 16:55:11 +02005862 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005863
Milton Millercd7900762007-10-17 16:55:11 +02005864 /*
5865 * In the intermediate directories, both the child directory and
5866 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005867 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02005868 * static strings and all have proc handlers.
5869 */
5870 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005871 if (entry->child)
5872 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02005873 if (entry->proc_handler == NULL)
5874 kfree(entry->procname);
5875 }
Milton Miller6382bc92007-10-15 17:00:19 +02005876
5877 kfree(*tablep);
5878 *tablep = NULL;
5879}
5880
Nick Piggine692ab52007-07-26 13:40:43 +02005881static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005882set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005883 const char *procname, void *data, int maxlen,
5884 mode_t mode, proc_handler *proc_handler)
5885{
Nick Piggine692ab52007-07-26 13:40:43 +02005886 entry->procname = procname;
5887 entry->data = data;
5888 entry->maxlen = maxlen;
5889 entry->mode = mode;
5890 entry->proc_handler = proc_handler;
5891}
5892
5893static struct ctl_table *
5894sd_alloc_ctl_domain_table(struct sched_domain *sd)
5895{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005896 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02005897
Milton Millerad1cdc12007-10-15 17:00:19 +02005898 if (table == NULL)
5899 return NULL;
5900
Alexey Dobriyane0361852007-08-09 11:16:46 +02005901 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005902 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005903 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005904 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005905 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005906 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005907 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005908 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005909 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005910 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005911 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005912 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005913 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005914 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005915 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005916 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005917 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005918 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005919 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005920 &sd->cache_nice_tries,
5921 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005922 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005923 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02005924 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005925
5926 return table;
5927}
5928
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005929static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005930{
5931 struct ctl_table *entry, *table;
5932 struct sched_domain *sd;
5933 int domain_num = 0, i;
5934 char buf[32];
5935
5936 for_each_domain(cpu, sd)
5937 domain_num++;
5938 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005939 if (table == NULL)
5940 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005941
5942 i = 0;
5943 for_each_domain(cpu, sd) {
5944 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005945 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005946 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005947 entry->child = sd_alloc_ctl_domain_table(sd);
5948 entry++;
5949 i++;
5950 }
5951 return table;
5952}
5953
5954static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005955static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005956{
5957 int i, cpu_num = num_online_cpus();
5958 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5959 char buf[32];
5960
Milton Miller73785472007-10-24 18:23:48 +02005961 WARN_ON(sd_ctl_dir[0].child);
5962 sd_ctl_dir[0].child = entry;
5963
Milton Millerad1cdc12007-10-15 17:00:19 +02005964 if (entry == NULL)
5965 return;
5966
Milton Miller97b6ea72007-10-15 17:00:19 +02005967 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005968 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005969 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005970 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005971 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005972 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005973 }
Milton Miller73785472007-10-24 18:23:48 +02005974
5975 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005976 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5977}
Milton Miller6382bc92007-10-15 17:00:19 +02005978
Milton Miller73785472007-10-24 18:23:48 +02005979/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005980static void unregister_sched_domain_sysctl(void)
5981{
Milton Miller73785472007-10-24 18:23:48 +02005982 if (sd_sysctl_header)
5983 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005984 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005985 if (sd_ctl_dir[0].child)
5986 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005987}
Nick Piggine692ab52007-07-26 13:40:43 +02005988#else
Milton Miller6382bc92007-10-15 17:00:19 +02005989static void register_sched_domain_sysctl(void)
5990{
5991}
5992static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005993{
5994}
5995#endif
5996
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997/*
5998 * migration_call - callback that gets triggered when a CPU is added.
5999 * Here we can start up the necessary migration thread for the new CPU.
6000 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006001static int __cpuinit
6002migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006005 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006007 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
6009 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006010
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006012 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006013 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 if (IS_ERR(p))
6015 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 kthread_bind(p, cpu);
6017 /* Must be high prio: stop_machine expects to yield to it. */
6018 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006019 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 task_rq_unlock(rq, &flags);
6021 cpu_rq(cpu)->migration_thread = p;
6022 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006023
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006025 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006026 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006028
6029 /* Update our root-domain */
6030 rq = cpu_rq(cpu);
6031 spin_lock_irqsave(&rq->lock, flags);
6032 if (rq->rd) {
6033 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6034 cpu_set(cpu, rq->rd->online);
6035 }
6036 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006038
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039#ifdef CONFIG_HOTPLUG_CPU
6040 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006041 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006042 if (!cpu_rq(cpu)->migration_thread)
6043 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006044 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006045 kthread_bind(cpu_rq(cpu)->migration_thread,
6046 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 kthread_stop(cpu_rq(cpu)->migration_thread);
6048 cpu_rq(cpu)->migration_thread = NULL;
6049 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006050
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006052 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006053 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 migrate_live_tasks(cpu);
6055 rq = cpu_rq(cpu);
6056 kthread_stop(rq->migration_thread);
6057 rq->migration_thread = NULL;
6058 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006059 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006060 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006061 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006063 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6064 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006066 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006067 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 migrate_nr_uninterruptible(rq);
6069 BUG_ON(rq->nr_running != 0);
6070
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006071 /*
6072 * No need to migrate the tasks: it was best-effort if
6073 * they didn't take sched_hotcpu_mutex. Just wake up
6074 * the requestors.
6075 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 spin_lock_irq(&rq->lock);
6077 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006078 struct migration_req *req;
6079
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006081 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 list_del_init(&req->list);
6083 complete(&req->done);
6084 }
6085 spin_unlock_irq(&rq->lock);
6086 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006087
Gregory Haskins08f503b2008-03-10 17:59:11 -04006088 case CPU_DYING:
6089 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006090 /* Update our root-domain */
6091 rq = cpu_rq(cpu);
6092 spin_lock_irqsave(&rq->lock, flags);
6093 if (rq->rd) {
6094 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6095 cpu_clear(cpu, rq->rd->online);
6096 }
6097 spin_unlock_irqrestore(&rq->lock, flags);
6098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099#endif
6100 }
6101 return NOTIFY_OK;
6102}
6103
6104/* Register at highest priority so that task migration (migrate_all_tasks)
6105 * happens before everything else.
6106 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006107static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 .notifier_call = migration_call,
6109 .priority = 10
6110};
6111
Adrian Bunke6fe6642007-11-09 22:39:39 +01006112void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113{
6114 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006115 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006116
6117 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006118 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6119 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6121 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122}
6123#endif
6124
6125#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006126
6127/* Number of possible processor ids */
6128int nr_cpu_ids __read_mostly = NR_CPUS;
6129EXPORT_SYMBOL(nr_cpu_ids);
6130
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006131#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006132
6133static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level)
6134{
6135 struct sched_group *group = sd->groups;
6136 cpumask_t groupmask;
6137 char str[NR_CPUS];
6138
6139 cpumask_scnprintf(str, NR_CPUS, sd->span);
6140 cpus_clear(groupmask);
6141
6142 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6143
6144 if (!(sd->flags & SD_LOAD_BALANCE)) {
6145 printk("does not load-balance\n");
6146 if (sd->parent)
6147 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6148 " has parent");
6149 return -1;
6150 }
6151
6152 printk(KERN_CONT "span %s\n", str);
6153
6154 if (!cpu_isset(cpu, sd->span)) {
6155 printk(KERN_ERR "ERROR: domain->span does not contain "
6156 "CPU%d\n", cpu);
6157 }
6158 if (!cpu_isset(cpu, group->cpumask)) {
6159 printk(KERN_ERR "ERROR: domain->groups does not contain"
6160 " CPU%d\n", cpu);
6161 }
6162
6163 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6164 do {
6165 if (!group) {
6166 printk("\n");
6167 printk(KERN_ERR "ERROR: group is NULL\n");
6168 break;
6169 }
6170
6171 if (!group->__cpu_power) {
6172 printk(KERN_CONT "\n");
6173 printk(KERN_ERR "ERROR: domain->cpu_power not "
6174 "set\n");
6175 break;
6176 }
6177
6178 if (!cpus_weight(group->cpumask)) {
6179 printk(KERN_CONT "\n");
6180 printk(KERN_ERR "ERROR: empty group\n");
6181 break;
6182 }
6183
6184 if (cpus_intersects(groupmask, group->cpumask)) {
6185 printk(KERN_CONT "\n");
6186 printk(KERN_ERR "ERROR: repeated CPUs\n");
6187 break;
6188 }
6189
6190 cpus_or(groupmask, groupmask, group->cpumask);
6191
6192 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
6193 printk(KERN_CONT " %s", str);
6194
6195 group = group->next;
6196 } while (group != sd->groups);
6197 printk(KERN_CONT "\n");
6198
6199 if (!cpus_equal(sd->span, groupmask))
6200 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6201
6202 if (sd->parent && !cpus_subset(groupmask, sd->parent->span))
6203 printk(KERN_ERR "ERROR: parent span is not a superset "
6204 "of domain->span\n");
6205 return 0;
6206}
6207
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208static void sched_domain_debug(struct sched_domain *sd, int cpu)
6209{
6210 int level = 0;
6211
Nick Piggin41c7ce92005-06-25 14:57:24 -07006212 if (!sd) {
6213 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6214 return;
6215 }
6216
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6218
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006219 for (;;) {
6220 if (sched_domain_debug_one(sd, cpu, level))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 level++;
6223 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006224 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006225 break;
6226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227}
6228#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006229# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230#endif
6231
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006232static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006233{
6234 if (cpus_weight(sd->span) == 1)
6235 return 1;
6236
6237 /* Following flags need at least 2 groups */
6238 if (sd->flags & (SD_LOAD_BALANCE |
6239 SD_BALANCE_NEWIDLE |
6240 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006241 SD_BALANCE_EXEC |
6242 SD_SHARE_CPUPOWER |
6243 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006244 if (sd->groups != sd->groups->next)
6245 return 0;
6246 }
6247
6248 /* Following flags don't use groups */
6249 if (sd->flags & (SD_WAKE_IDLE |
6250 SD_WAKE_AFFINE |
6251 SD_WAKE_BALANCE))
6252 return 0;
6253
6254 return 1;
6255}
6256
Ingo Molnar48f24c42006-07-03 00:25:40 -07006257static int
6258sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006259{
6260 unsigned long cflags = sd->flags, pflags = parent->flags;
6261
6262 if (sd_degenerate(parent))
6263 return 1;
6264
6265 if (!cpus_equal(sd->span, parent->span))
6266 return 0;
6267
6268 /* Does parent contain flags not in child? */
6269 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6270 if (cflags & SD_WAKE_AFFINE)
6271 pflags &= ~SD_WAKE_BALANCE;
6272 /* Flags needing groups don't count if only 1 group in parent */
6273 if (parent->groups == parent->groups->next) {
6274 pflags &= ~(SD_LOAD_BALANCE |
6275 SD_BALANCE_NEWIDLE |
6276 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006277 SD_BALANCE_EXEC |
6278 SD_SHARE_CPUPOWER |
6279 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006280 }
6281 if (~cflags & pflags)
6282 return 0;
6283
6284 return 1;
6285}
6286
Gregory Haskins57d885f2008-01-25 21:08:18 +01006287static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6288{
6289 unsigned long flags;
6290 const struct sched_class *class;
6291
6292 spin_lock_irqsave(&rq->lock, flags);
6293
6294 if (rq->rd) {
6295 struct root_domain *old_rd = rq->rd;
6296
Ingo Molnar0eab9142008-01-25 21:08:19 +01006297 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006298 if (class->leave_domain)
6299 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006300 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006301
Gregory Haskinsdc938522008-01-25 21:08:26 +01006302 cpu_clear(rq->cpu, old_rd->span);
6303 cpu_clear(rq->cpu, old_rd->online);
6304
Gregory Haskins57d885f2008-01-25 21:08:18 +01006305 if (atomic_dec_and_test(&old_rd->refcount))
6306 kfree(old_rd);
6307 }
6308
6309 atomic_inc(&rd->refcount);
6310 rq->rd = rd;
6311
Gregory Haskinsdc938522008-01-25 21:08:26 +01006312 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006313 if (cpu_isset(rq->cpu, cpu_online_map))
6314 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006315
Ingo Molnar0eab9142008-01-25 21:08:19 +01006316 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006317 if (class->join_domain)
6318 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006319 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006320
6321 spin_unlock_irqrestore(&rq->lock, flags);
6322}
6323
Gregory Haskinsdc938522008-01-25 21:08:26 +01006324static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006325{
6326 memset(rd, 0, sizeof(*rd));
6327
Gregory Haskinsdc938522008-01-25 21:08:26 +01006328 cpus_clear(rd->span);
6329 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006330}
6331
6332static void init_defrootdomain(void)
6333{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006334 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006335 atomic_set(&def_root_domain.refcount, 1);
6336}
6337
Gregory Haskinsdc938522008-01-25 21:08:26 +01006338static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006339{
6340 struct root_domain *rd;
6341
6342 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6343 if (!rd)
6344 return NULL;
6345
Gregory Haskinsdc938522008-01-25 21:08:26 +01006346 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006347
6348 return rd;
6349}
6350
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006352 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 * hold the hotplug lock.
6354 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006355static void
6356cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006358 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006359 struct sched_domain *tmp;
6360
6361 /* Remove the sched domains which do not contribute to scheduling. */
6362 for (tmp = sd; tmp; tmp = tmp->parent) {
6363 struct sched_domain *parent = tmp->parent;
6364 if (!parent)
6365 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006366 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006367 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006368 if (parent->parent)
6369 parent->parent->child = tmp;
6370 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006371 }
6372
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006373 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006374 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006375 if (sd)
6376 sd->child = NULL;
6377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378
6379 sched_domain_debug(sd, cpu);
6380
Gregory Haskins57d885f2008-01-25 21:08:18 +01006381 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006382 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383}
6384
6385/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006386static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387
6388/* Setup the mask of cpus configured for isolated domains */
6389static int __init isolated_cpu_setup(char *str)
6390{
6391 int ints[NR_CPUS], i;
6392
6393 str = get_options(str, ARRAY_SIZE(ints), ints);
6394 cpus_clear(cpu_isolated_map);
6395 for (i = 1; i <= ints[0]; i++)
6396 if (ints[i] < NR_CPUS)
6397 cpu_set(ints[i], cpu_isolated_map);
6398 return 1;
6399}
6400
Ingo Molnar8927f492007-10-15 17:00:13 +02006401__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402
6403/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006404 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6405 * to a function which identifies what group(along with sched group) a CPU
6406 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6407 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 *
6409 * init_sched_build_groups will build a circular linked list of the groups
6410 * covered by the given span, and will set each group's ->cpumask correctly,
6411 * and ->cpu_power to 0.
6412 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006413static void
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006414init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
6415 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
6416 struct sched_group **sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417{
6418 struct sched_group *first = NULL, *last = NULL;
6419 cpumask_t covered = CPU_MASK_NONE;
6420 int i;
6421
6422 for_each_cpu_mask(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006423 struct sched_group *sg;
6424 int group = group_fn(i, cpu_map, &sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 int j;
6426
6427 if (cpu_isset(i, covered))
6428 continue;
6429
6430 sg->cpumask = CPU_MASK_NONE;
Eric Dumazet5517d862007-05-08 00:32:57 -07006431 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432
6433 for_each_cpu_mask(j, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006434 if (group_fn(j, cpu_map, NULL) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 continue;
6436
6437 cpu_set(j, covered);
6438 cpu_set(j, sg->cpumask);
6439 }
6440 if (!first)
6441 first = sg;
6442 if (last)
6443 last->next = sg;
6444 last = sg;
6445 }
6446 last->next = first;
6447}
6448
John Hawkes9c1cfda2005-09-06 15:18:14 -07006449#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
John Hawkes9c1cfda2005-09-06 15:18:14 -07006451#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006452
John Hawkes9c1cfda2005-09-06 15:18:14 -07006453/**
6454 * find_next_best_node - find the next node to include in a sched_domain
6455 * @node: node whose sched_domain we're building
6456 * @used_nodes: nodes already in the sched_domain
6457 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006458 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006459 * finds the closest node not already in the @used_nodes map.
6460 *
6461 * Should use nodemask_t.
6462 */
6463static int find_next_best_node(int node, unsigned long *used_nodes)
6464{
6465 int i, n, val, min_val, best_node = 0;
6466
6467 min_val = INT_MAX;
6468
6469 for (i = 0; i < MAX_NUMNODES; i++) {
6470 /* Start at @node */
6471 n = (node + i) % MAX_NUMNODES;
6472
6473 if (!nr_cpus_node(n))
6474 continue;
6475
6476 /* Skip already used nodes */
6477 if (test_bit(n, used_nodes))
6478 continue;
6479
6480 /* Simple min distance search */
6481 val = node_distance(node, n);
6482
6483 if (val < min_val) {
6484 min_val = val;
6485 best_node = n;
6486 }
6487 }
6488
6489 set_bit(best_node, used_nodes);
6490 return best_node;
6491}
6492
6493/**
6494 * sched_domain_node_span - get a cpumask for a node's sched_domain
6495 * @node: node whose cpumask we're constructing
6496 * @size: number of nodes to include in this span
6497 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006498 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006499 * should be one that prevents unnecessary balancing, but also spreads tasks
6500 * out optimally.
6501 */
6502static cpumask_t sched_domain_node_span(int node)
6503{
John Hawkes9c1cfda2005-09-06 15:18:14 -07006504 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006505 cpumask_t span, nodemask;
6506 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006507
6508 cpus_clear(span);
6509 bitmap_zero(used_nodes, MAX_NUMNODES);
6510
6511 nodemask = node_to_cpumask(node);
6512 cpus_or(span, span, nodemask);
6513 set_bit(node, used_nodes);
6514
6515 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
6516 int next_node = find_next_best_node(node, used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006517
John Hawkes9c1cfda2005-09-06 15:18:14 -07006518 nodemask = node_to_cpumask(next_node);
6519 cpus_or(span, span, nodemask);
6520 }
6521
6522 return span;
6523}
6524#endif
6525
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006526int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006527
John Hawkes9c1cfda2005-09-06 15:18:14 -07006528/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006529 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006530 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531#ifdef CONFIG_SCHED_SMT
6532static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006533static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006534
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006535static int
6536cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006538 if (sg)
6539 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 return cpu;
6541}
6542#endif
6543
Ingo Molnar48f24c42006-07-03 00:25:40 -07006544/*
6545 * multi-core sched-domains:
6546 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006547#ifdef CONFIG_SCHED_MC
6548static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006549static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006550#endif
6551
6552#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006553static int
6554cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006555{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006556 int group;
Mike Travisd5a74302007-10-16 01:24:05 -07006557 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006558 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006559 group = first_cpu(mask);
6560 if (sg)
6561 *sg = &per_cpu(sched_group_core, group);
6562 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006563}
6564#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006565static int
6566cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006567{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006568 if (sg)
6569 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006570 return cpu;
6571}
6572#endif
6573
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006575static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006576
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006577static int
6578cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006580 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006581#ifdef CONFIG_SCHED_MC
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006582 cpumask_t mask = cpu_coregroup_map(cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006583 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006584 group = first_cpu(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006585#elif defined(CONFIG_SCHED_SMT)
Mike Travisd5a74302007-10-16 01:24:05 -07006586 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006587 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006588 group = first_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006590 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006592 if (sg)
6593 *sg = &per_cpu(sched_group_phys, group);
6594 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595}
6596
6597#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006598/*
6599 * The init_sched_build_groups can't handle what we want to do with node
6600 * groups, so roll our own. Now each node has its own list of groups which
6601 * gets dynamically allocated.
6602 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603static DEFINE_PER_CPU(struct sched_domain, node_domains);
John Hawkesd1b55132005-09-06 15:18:14 -07006604static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
John Hawkes9c1cfda2005-09-06 15:18:14 -07006605
6606static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006607static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006608
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006609static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
6610 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006612 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
6613 int group;
6614
6615 cpus_and(nodemask, nodemask, *cpu_map);
6616 group = first_cpu(nodemask);
6617
6618 if (sg)
6619 *sg = &per_cpu(sched_group_allnodes, group);
6620 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006622
Siddha, Suresh B08069032006-03-27 01:15:23 -08006623static void init_numa_sched_groups_power(struct sched_group *group_head)
6624{
6625 struct sched_group *sg = group_head;
6626 int j;
6627
6628 if (!sg)
6629 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006630 do {
6631 for_each_cpu_mask(j, sg->cpumask) {
6632 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006633
Andi Kleen3a5c3592007-10-15 17:00:14 +02006634 sd = &per_cpu(phys_domains, j);
6635 if (j != first_cpu(sd->groups->cpumask)) {
6636 /*
6637 * Only add "power" once for each
6638 * physical package.
6639 */
6640 continue;
6641 }
6642
6643 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006644 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006645 sg = sg->next;
6646 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006647}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648#endif
6649
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006650#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006651/* Free memory allocated for various sched_group structures */
6652static void free_sched_groups(const cpumask_t *cpu_map)
6653{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006654 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006655
6656 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006657 struct sched_group **sched_group_nodes
6658 = sched_group_nodes_bycpu[cpu];
6659
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006660 if (!sched_group_nodes)
6661 continue;
6662
6663 for (i = 0; i < MAX_NUMNODES; i++) {
6664 cpumask_t nodemask = node_to_cpumask(i);
6665 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6666
6667 cpus_and(nodemask, nodemask, *cpu_map);
6668 if (cpus_empty(nodemask))
6669 continue;
6670
6671 if (sg == NULL)
6672 continue;
6673 sg = sg->next;
6674next_sg:
6675 oldsg = sg;
6676 sg = sg->next;
6677 kfree(oldsg);
6678 if (oldsg != sched_group_nodes[i])
6679 goto next_sg;
6680 }
6681 kfree(sched_group_nodes);
6682 sched_group_nodes_bycpu[cpu] = NULL;
6683 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006684}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006685#else
6686static void free_sched_groups(const cpumask_t *cpu_map)
6687{
6688}
6689#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006690
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006692 * Initialize sched groups cpu_power.
6693 *
6694 * cpu_power indicates the capacity of sched group, which is used while
6695 * distributing the load between different sched groups in a sched domain.
6696 * Typically cpu_power for all the groups in a sched domain will be same unless
6697 * there are asymmetries in the topology. If there are asymmetries, group
6698 * having more cpu_power will pickup more load compared to the group having
6699 * less cpu_power.
6700 *
6701 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
6702 * the maximum number of tasks a group can handle in the presence of other idle
6703 * or lightly loaded groups in the same sched domain.
6704 */
6705static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6706{
6707 struct sched_domain *child;
6708 struct sched_group *group;
6709
6710 WARN_ON(!sd || !sd->groups);
6711
6712 if (cpu != first_cpu(sd->groups->cpumask))
6713 return;
6714
6715 child = sd->child;
6716
Eric Dumazet5517d862007-05-08 00:32:57 -07006717 sd->groups->__cpu_power = 0;
6718
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006719 /*
6720 * For perf policy, if the groups in child domain share resources
6721 * (for example cores sharing some portions of the cache hierarchy
6722 * or SMT), then set this domain groups cpu_power such that each group
6723 * can handle only one task, when there are other idle groups in the
6724 * same sched domain.
6725 */
6726 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
6727 (child->flags &
6728 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07006729 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006730 return;
6731 }
6732
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006733 /*
6734 * add cpu_power of each child group to this groups cpu_power
6735 */
6736 group = child->groups;
6737 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07006738 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006739 group = group->next;
6740 } while (group != child->groups);
6741}
6742
6743/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006744 * Build sched domains for a given set of cpus and attach the sched domains
6745 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006747static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748{
6749 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006750 struct root_domain *rd;
John Hawkesd1b55132005-09-06 15:18:14 -07006751#ifdef CONFIG_NUMA
6752 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006753 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07006754
6755 /*
6756 * Allocate the per-node list of sched groups
6757 */
Milton Miller5cf9f062007-10-15 17:00:19 +02006758 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006759 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006760 if (!sched_group_nodes) {
6761 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006762 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006763 }
6764 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6765#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766
Gregory Haskinsdc938522008-01-25 21:08:26 +01006767 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01006768 if (!rd) {
6769 printk(KERN_WARNING "Cannot alloc root domain\n");
6770 return -ENOMEM;
6771 }
6772
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006774 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006776 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 struct sched_domain *sd = NULL, *p;
6778 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
6779
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006780 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781
6782#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006783 if (cpus_weight(*cpu_map) >
6784 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006785 sd = &per_cpu(allnodes_domains, i);
6786 *sd = SD_ALLNODES_INIT;
6787 sd->span = *cpu_map;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006788 cpu_to_allnodes_group(i, cpu_map, &sd->groups);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006789 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006790 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006791 } else
6792 p = NULL;
6793
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 sd = &per_cpu(node_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 *sd = SD_NODE_INIT;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006796 sd->span = sched_domain_node_span(cpu_to_node(i));
6797 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006798 if (p)
6799 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006800 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801#endif
6802
6803 p = sd;
6804 sd = &per_cpu(phys_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 *sd = SD_CPU_INIT;
6806 sd->span = nodemask;
6807 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006808 if (p)
6809 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006810 cpu_to_phys_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006812#ifdef CONFIG_SCHED_MC
6813 p = sd;
6814 sd = &per_cpu(core_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006815 *sd = SD_MC_INIT;
6816 sd->span = cpu_coregroup_map(i);
6817 cpus_and(sd->span, sd->span, *cpu_map);
6818 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006819 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006820 cpu_to_core_group(i, cpu_map, &sd->groups);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006821#endif
6822
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823#ifdef CONFIG_SCHED_SMT
6824 p = sd;
6825 sd = &per_cpu(cpu_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 *sd = SD_SIBLING_INIT;
Mike Travisd5a74302007-10-16 01:24:05 -07006827 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006828 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006830 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006831 cpu_to_cpu_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832#endif
6833 }
6834
6835#ifdef CONFIG_SCHED_SMT
6836 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006837 for_each_cpu_mask(i, *cpu_map) {
Mike Travisd5a74302007-10-16 01:24:05 -07006838 cpumask_t this_sibling_map = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006839 cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 if (i != first_cpu(this_sibling_map))
6841 continue;
6842
Ingo Molnardd41f592007-07-09 18:51:59 +02006843 init_sched_build_groups(this_sibling_map, cpu_map,
6844 &cpu_to_cpu_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 }
6846#endif
6847
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006848#ifdef CONFIG_SCHED_MC
6849 /* Set up multi-core groups */
6850 for_each_cpu_mask(i, *cpu_map) {
6851 cpumask_t this_core_map = cpu_coregroup_map(i);
6852 cpus_and(this_core_map, this_core_map, *cpu_map);
6853 if (i != first_cpu(this_core_map))
6854 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006855 init_sched_build_groups(this_core_map, cpu_map,
6856 &cpu_to_core_group);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006857 }
6858#endif
6859
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 /* Set up physical groups */
6861 for (i = 0; i < MAX_NUMNODES; i++) {
6862 cpumask_t nodemask = node_to_cpumask(i);
6863
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006864 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 if (cpus_empty(nodemask))
6866 continue;
6867
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006868 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 }
6870
6871#ifdef CONFIG_NUMA
6872 /* Set up node groups */
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006873 if (sd_allnodes)
Ingo Molnardd41f592007-07-09 18:51:59 +02006874 init_sched_build_groups(*cpu_map, cpu_map,
6875 &cpu_to_allnodes_group);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006876
6877 for (i = 0; i < MAX_NUMNODES; i++) {
6878 /* Set up node groups */
6879 struct sched_group *sg, *prev;
6880 cpumask_t nodemask = node_to_cpumask(i);
6881 cpumask_t domainspan;
6882 cpumask_t covered = CPU_MASK_NONE;
6883 int j;
6884
6885 cpus_and(nodemask, nodemask, *cpu_map);
John Hawkesd1b55132005-09-06 15:18:14 -07006886 if (cpus_empty(nodemask)) {
6887 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006888 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07006889 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006890
6891 domainspan = sched_domain_node_span(i);
6892 cpus_and(domainspan, domainspan, *cpu_map);
6893
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006894 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006895 if (!sg) {
6896 printk(KERN_WARNING "Can not alloc domain group for "
6897 "node %d\n", i);
6898 goto error;
6899 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006900 sched_group_nodes[i] = sg;
6901 for_each_cpu_mask(j, nodemask) {
6902 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02006903
John Hawkes9c1cfda2005-09-06 15:18:14 -07006904 sd = &per_cpu(node_domains, j);
6905 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006906 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006907 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006908 sg->cpumask = nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006909 sg->next = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006910 cpus_or(covered, covered, nodemask);
6911 prev = sg;
6912
6913 for (j = 0; j < MAX_NUMNODES; j++) {
6914 cpumask_t tmp, notcovered;
6915 int n = (i + j) % MAX_NUMNODES;
6916
6917 cpus_complement(notcovered, covered);
6918 cpus_and(tmp, notcovered, *cpu_map);
6919 cpus_and(tmp, tmp, domainspan);
6920 if (cpus_empty(tmp))
6921 break;
6922
6923 nodemask = node_to_cpumask(n);
6924 cpus_and(tmp, tmp, nodemask);
6925 if (cpus_empty(tmp))
6926 continue;
6927
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006928 sg = kmalloc_node(sizeof(struct sched_group),
6929 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006930 if (!sg) {
6931 printk(KERN_WARNING
6932 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006933 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006934 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006935 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006936 sg->cpumask = tmp;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006937 sg->next = prev->next;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006938 cpus_or(covered, covered, tmp);
6939 prev->next = sg;
6940 prev = sg;
6941 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943#endif
6944
6945 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006946#ifdef CONFIG_SCHED_SMT
6947 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006948 struct sched_domain *sd = &per_cpu(cpu_domains, i);
6949
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006950 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006951 }
6952#endif
6953#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006954 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006955 struct sched_domain *sd = &per_cpu(core_domains, i);
6956
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006957 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006958 }
6959#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006961 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006962 struct sched_domain *sd = &per_cpu(phys_domains, i);
6963
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006964 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 }
6966
John Hawkes9c1cfda2005-09-06 15:18:14 -07006967#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08006968 for (i = 0; i < MAX_NUMNODES; i++)
6969 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006970
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006971 if (sd_allnodes) {
6972 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07006973
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006974 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07006975 init_numa_sched_groups_power(sg);
6976 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006977#endif
6978
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006980 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 struct sched_domain *sd;
6982#ifdef CONFIG_SCHED_SMT
6983 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006984#elif defined(CONFIG_SCHED_MC)
6985 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986#else
6987 sd = &per_cpu(phys_domains, i);
6988#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01006989 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006991
6992 return 0;
6993
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006994#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006995error:
6996 free_sched_groups(cpu_map);
6997 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006998#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999}
Paul Jackson029190c2007-10-18 23:40:20 -07007000
7001static cpumask_t *doms_cur; /* current sched domains */
7002static int ndoms_cur; /* number of sched domains in 'doms_cur' */
7003
7004/*
7005 * Special case: If a kmalloc of a doms_cur partition (array of
7006 * cpumask_t) fails, then fallback to a single sched domain,
7007 * as determined by the single cpumask_t fallback_doms.
7008 */
7009static cpumask_t fallback_doms;
7010
Heiko Carstens22e52b02008-03-12 18:31:59 +01007011void __attribute__((weak)) arch_update_cpu_topology(void)
7012{
7013}
7014
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007015/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007016 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007017 * For now this just excludes isolated cpus, but could be used to
7018 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007019 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007020static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007021{
Milton Miller73785472007-10-24 18:23:48 +02007022 int err;
7023
Heiko Carstens22e52b02008-03-12 18:31:59 +01007024 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007025 ndoms_cur = 1;
7026 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7027 if (!doms_cur)
7028 doms_cur = &fallback_doms;
7029 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Milton Miller73785472007-10-24 18:23:48 +02007030 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007031 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007032
7033 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007034}
7035
7036static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037{
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007038 free_sched_groups(cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007041/*
7042 * Detach sched domains from a group of cpus specified in cpu_map
7043 * These cpus will now be attached to the NULL domain
7044 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007045static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007046{
7047 int i;
7048
Milton Miller6382bc92007-10-15 17:00:19 +02007049 unregister_sched_domain_sysctl();
7050
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007051 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007052 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007053 synchronize_sched();
7054 arch_destroy_sched_domains(cpu_map);
7055}
7056
Paul Jackson029190c2007-10-18 23:40:20 -07007057/*
7058 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007059 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007060 * doms_new[] to the current sched domain partitioning, doms_cur[].
7061 * It destroys each deleted domain and builds each new domain.
7062 *
7063 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007064 * The masks don't intersect (don't overlap.) We should setup one
7065 * sched domain for each mask. CPUs not in any of the cpumasks will
7066 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007067 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7068 * it as it is.
7069 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007070 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7071 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007072 * failed the kmalloc call, then it can pass in doms_new == NULL,
7073 * and partition_sched_domains() will fallback to the single partition
7074 * 'fallback_doms'.
7075 *
7076 * Call with hotplug lock held
7077 */
7078void partition_sched_domains(int ndoms_new, cpumask_t *doms_new)
7079{
7080 int i, j;
7081
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007082 lock_doms_cur();
7083
Milton Miller73785472007-10-24 18:23:48 +02007084 /* always unregister in case we don't destroy any domains */
7085 unregister_sched_domain_sysctl();
7086
Paul Jackson029190c2007-10-18 23:40:20 -07007087 if (doms_new == NULL) {
7088 ndoms_new = 1;
7089 doms_new = &fallback_doms;
7090 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
7091 }
7092
7093 /* Destroy deleted domains */
7094 for (i = 0; i < ndoms_cur; i++) {
7095 for (j = 0; j < ndoms_new; j++) {
7096 if (cpus_equal(doms_cur[i], doms_new[j]))
7097 goto match1;
7098 }
7099 /* no match - a current sched domain not in new doms_new[] */
7100 detach_destroy_domains(doms_cur + i);
7101match1:
7102 ;
7103 }
7104
7105 /* Build new domains */
7106 for (i = 0; i < ndoms_new; i++) {
7107 for (j = 0; j < ndoms_cur; j++) {
7108 if (cpus_equal(doms_new[i], doms_cur[j]))
7109 goto match2;
7110 }
7111 /* no match - add a new doms_new */
7112 build_sched_domains(doms_new + i);
7113match2:
7114 ;
7115 }
7116
7117 /* Remember the new sched domains */
7118 if (doms_cur != &fallback_doms)
7119 kfree(doms_cur);
7120 doms_cur = doms_new;
7121 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007122
7123 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007124
7125 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007126}
7127
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007128#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007129int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007130{
7131 int err;
7132
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007133 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007134 detach_destroy_domains(&cpu_online_map);
7135 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007136 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007137
7138 return err;
7139}
7140
7141static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7142{
7143 int ret;
7144
7145 if (buf[0] != '0' && buf[0] != '1')
7146 return -EINVAL;
7147
7148 if (smt)
7149 sched_smt_power_savings = (buf[0] == '1');
7150 else
7151 sched_mc_power_savings = (buf[0] == '1');
7152
7153 ret = arch_reinit_sched_domains();
7154
7155 return ret ? ret : count;
7156}
7157
Adrian Bunk6707de002007-08-12 18:08:19 +02007158#ifdef CONFIG_SCHED_MC
7159static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7160{
7161 return sprintf(page, "%u\n", sched_mc_power_savings);
7162}
7163static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7164 const char *buf, size_t count)
7165{
7166 return sched_power_savings_store(buf, count, 0);
7167}
7168static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7169 sched_mc_power_savings_store);
7170#endif
7171
7172#ifdef CONFIG_SCHED_SMT
7173static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7174{
7175 return sprintf(page, "%u\n", sched_smt_power_savings);
7176}
7177static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7178 const char *buf, size_t count)
7179{
7180 return sched_power_savings_store(buf, count, 1);
7181}
7182static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7183 sched_smt_power_savings_store);
7184#endif
7185
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007186int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7187{
7188 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007189
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007190#ifdef CONFIG_SCHED_SMT
7191 if (smt_capable())
7192 err = sysfs_create_file(&cls->kset.kobj,
7193 &attr_sched_smt_power_savings.attr);
7194#endif
7195#ifdef CONFIG_SCHED_MC
7196 if (!err && mc_capable())
7197 err = sysfs_create_file(&cls->kset.kobj,
7198 &attr_sched_mc_power_savings.attr);
7199#endif
7200 return err;
7201}
7202#endif
7203
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007205 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007207 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 * which will prevent rebalancing while the sched domains are recalculated.
7209 */
7210static int update_sched_domains(struct notifier_block *nfb,
7211 unsigned long action, void *hcpu)
7212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 switch (action) {
7214 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007215 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007217 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007218 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 return NOTIFY_OK;
7220
7221 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007222 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007224 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007226 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007228 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 /*
7230 * Fall through and re-initialise the domains.
7231 */
7232 break;
7233 default:
7234 return NOTIFY_DONE;
7235 }
7236
7237 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007238 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
7240 return NOTIFY_OK;
7241}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242
7243void __init sched_init_smp(void)
7244{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007245 cpumask_t non_isolated_cpus;
7246
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007247 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007248 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007249 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007250 if (cpus_empty(non_isolated_cpus))
7251 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007252 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 /* XXX: Theoretical race here - CPU may be hotplugged now */
7254 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007255
7256 /* Move init over to a non-isolated CPU */
7257 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
7258 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007259 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260}
7261#else
7262void __init sched_init_smp(void)
7263{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007264 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265}
7266#endif /* CONFIG_SMP */
7267
7268int in_sched_functions(unsigned long addr)
7269{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270 return in_lock_functions(addr) ||
7271 (addr >= (unsigned long)__sched_text_start
7272 && addr < (unsigned long)__sched_text_end);
7273}
7274
Alexey Dobriyana9957442007-10-15 17:00:13 +02007275static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007276{
7277 cfs_rq->tasks_timeline = RB_ROOT;
Ingo Molnardd41f592007-07-09 18:51:59 +02007278#ifdef CONFIG_FAIR_GROUP_SCHED
7279 cfs_rq->rq = rq;
7280#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007281 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007282}
7283
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007284static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7285{
7286 struct rt_prio_array *array;
7287 int i;
7288
7289 array = &rt_rq->active;
7290 for (i = 0; i < MAX_RT_PRIO; i++) {
7291 INIT_LIST_HEAD(array->queue + i);
7292 __clear_bit(i, array->bitmap);
7293 }
7294 /* delimiter for bitsearch: */
7295 __set_bit(MAX_RT_PRIO, array->bitmap);
7296
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007297#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007298 rt_rq->highest_prio = MAX_RT_PRIO;
7299#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007300#ifdef CONFIG_SMP
7301 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007302 rt_rq->overloaded = 0;
7303#endif
7304
7305 rt_rq->rt_time = 0;
7306 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007307 rt_rq->rt_runtime = 0;
7308 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007309
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007310#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007311 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007312 rt_rq->rq = rq;
7313#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007314}
7315
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007316#ifdef CONFIG_FAIR_GROUP_SCHED
7317static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg,
7318 struct cfs_rq *cfs_rq, struct sched_entity *se,
7319 int cpu, int add)
7320{
7321 tg->cfs_rq[cpu] = cfs_rq;
7322 init_cfs_rq(cfs_rq, rq);
7323 cfs_rq->tg = tg;
7324 if (add)
7325 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7326
7327 tg->se[cpu] = se;
7328 se->cfs_rq = &rq->cfs;
7329 se->my_q = cfs_rq;
7330 se->load.weight = tg->shares;
7331 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
7332 se->parent = NULL;
7333}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007334#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007335
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007336#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007337static void init_tg_rt_entry(struct rq *rq, struct task_group *tg,
7338 struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
7339 int cpu, int add)
7340{
7341 tg->rt_rq[cpu] = rt_rq;
7342 init_rt_rq(rt_rq, rq);
7343 rt_rq->tg = tg;
7344 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007345 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007346 if (add)
7347 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7348
7349 tg->rt_se[cpu] = rt_se;
7350 rt_se->rt_rq = &rq->rt;
7351 rt_se->my_q = rt_rq;
7352 rt_se->parent = NULL;
7353 INIT_LIST_HEAD(&rt_se->run_list);
7354}
7355#endif
7356
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357void __init sched_init(void)
7358{
Christoph Lameter476f3532007-05-06 14:48:58 -07007359 int highest_cpu = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007360 int i, j;
7361
Gregory Haskins57d885f2008-01-25 21:08:18 +01007362#ifdef CONFIG_SMP
7363 init_defrootdomain();
7364#endif
7365
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007366 init_rt_bandwidth(&def_rt_bandwidth,
7367 global_rt_period(), global_rt_runtime());
7368
7369#ifdef CONFIG_RT_GROUP_SCHED
7370 init_rt_bandwidth(&init_task_group.rt_bandwidth,
7371 global_rt_period(), global_rt_runtime());
7372#endif
7373
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007374#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007375 list_add(&init_task_group.list, &task_groups);
7376#endif
7377
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007378 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007379 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380
7381 rq = cpu_rq(i);
7382 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07007383 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07007384 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007385 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02007386 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02007387 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007388 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007389#ifdef CONFIG_FAIR_GROUP_SCHED
7390 init_task_group.shares = init_task_group_load;
7391 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
7392 init_tg_cfs_entry(rq, &init_task_group,
7393 &per_cpu(init_cfs_rq, i),
7394 &per_cpu(init_sched_entity, i), i, 1);
7395
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007396#endif
7397#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007398 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
7399 init_tg_rt_entry(rq, &init_task_group,
7400 &per_cpu(init_rt_rq, i),
7401 &per_cpu(init_sched_rt_entity, i), i, 1);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007402#else
7403 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007404#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405
Ingo Molnardd41f592007-07-09 18:51:59 +02007406 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7407 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007409 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007410 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007412 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007414 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 rq->migration_thread = NULL;
7416 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007417 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007419 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420 atomic_set(&rq->nr_iowait, 0);
Christoph Lameter476f3532007-05-06 14:48:58 -07007421 highest_cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422 }
7423
Peter Williams2dd73a42006-06-27 02:54:34 -07007424 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007425
Avi Kivitye107be32007-07-26 13:40:43 +02007426#ifdef CONFIG_PREEMPT_NOTIFIERS
7427 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7428#endif
7429
Christoph Lameterc9819f42006-12-10 02:20:25 -08007430#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07007431 nr_cpu_ids = highest_cpu + 1;
Christoph Lameterc9819f42006-12-10 02:20:25 -08007432 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
7433#endif
7434
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007435#ifdef CONFIG_RT_MUTEXES
7436 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
7437#endif
7438
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 /*
7440 * The boot idle thread does lazy MMU switching as well:
7441 */
7442 atomic_inc(&init_mm.mm_count);
7443 enter_lazy_tlb(&init_mm, current);
7444
7445 /*
7446 * Make us the idle thread. Technically, schedule() should not be
7447 * called from this thread, however somewhere below it might be,
7448 * but because we are the idle thread, we just pick up running again
7449 * when this runqueue becomes "idle".
7450 */
7451 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02007452 /*
7453 * During early bootup we pretend to be a normal task:
7454 */
7455 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01007456
7457 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458}
7459
7460#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
7461void __might_sleep(char *file, int line)
7462{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007463#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 static unsigned long prev_jiffy; /* ratelimiting */
7465
7466 if ((in_atomic() || irqs_disabled()) &&
7467 system_state == SYSTEM_RUNNING && !oops_in_progress) {
7468 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7469 return;
7470 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08007471 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 " context at %s:%d\n", file, line);
7473 printk("in_atomic():%d, irqs_disabled():%d\n",
7474 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08007475 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08007476 if (irqs_disabled())
7477 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 dump_stack();
7479 }
7480#endif
7481}
7482EXPORT_SYMBOL(__might_sleep);
7483#endif
7484
7485#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007486static void normalize_task(struct rq *rq, struct task_struct *p)
7487{
7488 int on_rq;
7489 update_rq_clock(rq);
7490 on_rq = p->se.on_rq;
7491 if (on_rq)
7492 deactivate_task(rq, p, 0);
7493 __setscheduler(rq, p, SCHED_NORMAL, 0);
7494 if (on_rq) {
7495 activate_task(rq, p, 0);
7496 resched_task(rq->curr);
7497 }
7498}
7499
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500void normalize_rt_tasks(void)
7501{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007502 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007504 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007506 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007507 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007508 /*
7509 * Only normalize user tasks:
7510 */
7511 if (!p->mm)
7512 continue;
7513
Ingo Molnardd41f592007-07-09 18:51:59 +02007514 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007515#ifdef CONFIG_SCHEDSTATS
7516 p->se.wait_start = 0;
7517 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007518 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007519#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007520 task_rq(p)->clock = 0;
7521
7522 if (!rt_task(p)) {
7523 /*
7524 * Renice negative nice level userspace
7525 * tasks back to 0:
7526 */
7527 if (TASK_NICE(p) < 0 && p->mm)
7528 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007532 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007533 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534
Ingo Molnar178be792007-10-15 17:00:18 +02007535 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007536
Ingo Molnarb29739f2006-06-27 02:54:51 -07007537 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007538 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007539 } while_each_thread(g, p);
7540
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007541 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542}
7543
7544#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007545
7546#ifdef CONFIG_IA64
7547/*
7548 * These functions are only useful for the IA64 MCA handling.
7549 *
7550 * They can only be called when the whole system has been
7551 * stopped - every CPU needs to be quiescent, and no scheduling
7552 * activity can take place. Using them for anything else would
7553 * be a serious bug, and as a result, they aren't even visible
7554 * under any other configuration.
7555 */
7556
7557/**
7558 * curr_task - return the current task for a given cpu.
7559 * @cpu: the processor in question.
7560 *
7561 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7562 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007563struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007564{
7565 return cpu_curr(cpu);
7566}
7567
7568/**
7569 * set_curr_task - set the current task for a given cpu.
7570 * @cpu: the processor in question.
7571 * @p: the task pointer to set.
7572 *
7573 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007574 * are serviced on a separate stack. It allows the architecture to switch the
7575 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007576 * must be called with all CPU's synchronized, and interrupts disabled, the
7577 * and caller must save the original value of the current task (see
7578 * curr_task() above) and restore that value before reenabling interrupts and
7579 * re-starting the system.
7580 *
7581 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7582 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007583void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007584{
7585 cpu_curr(cpu) = p;
7586}
7587
7588#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007589
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007590#ifdef CONFIG_FAIR_GROUP_SCHED
7591static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007592{
7593 int i;
7594
7595 for_each_possible_cpu(i) {
7596 if (tg->cfs_rq)
7597 kfree(tg->cfs_rq[i]);
7598 if (tg->se)
7599 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007600 }
7601
7602 kfree(tg->cfs_rq);
7603 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007604}
7605
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007606static int alloc_fair_sched_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007607{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007608 struct cfs_rq *cfs_rq;
7609 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007610 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007611 int i;
7612
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007613 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007614 if (!tg->cfs_rq)
7615 goto err;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007616 tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007617 if (!tg->se)
7618 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007619
7620 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007621
7622 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007623 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007624
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007625 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
7626 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007627 if (!cfs_rq)
7628 goto err;
7629
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007630 se = kmalloc_node(sizeof(struct sched_entity),
7631 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007632 if (!se)
7633 goto err;
7634
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007635 init_tg_cfs_entry(rq, tg, cfs_rq, se, i, 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007636 }
7637
7638 return 1;
7639
7640 err:
7641 return 0;
7642}
7643
7644static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7645{
7646 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
7647 &cpu_rq(cpu)->leaf_cfs_rq_list);
7648}
7649
7650static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7651{
7652 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
7653}
7654#else
7655static inline void free_fair_sched_group(struct task_group *tg)
7656{
7657}
7658
7659static inline int alloc_fair_sched_group(struct task_group *tg)
7660{
7661 return 1;
7662}
7663
7664static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7665{
7666}
7667
7668static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7669{
7670}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007671#endif
7672
7673#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007674static void free_rt_sched_group(struct task_group *tg)
7675{
7676 int i;
7677
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007678 destroy_rt_bandwidth(&tg->rt_bandwidth);
7679
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007680 for_each_possible_cpu(i) {
7681 if (tg->rt_rq)
7682 kfree(tg->rt_rq[i]);
7683 if (tg->rt_se)
7684 kfree(tg->rt_se[i]);
7685 }
7686
7687 kfree(tg->rt_rq);
7688 kfree(tg->rt_se);
7689}
7690
7691static int alloc_rt_sched_group(struct task_group *tg)
7692{
7693 struct rt_rq *rt_rq;
7694 struct sched_rt_entity *rt_se;
7695 struct rq *rq;
7696 int i;
7697
7698 tg->rt_rq = kzalloc(sizeof(rt_rq) * NR_CPUS, GFP_KERNEL);
7699 if (!tg->rt_rq)
7700 goto err;
7701 tg->rt_se = kzalloc(sizeof(rt_se) * NR_CPUS, GFP_KERNEL);
7702 if (!tg->rt_se)
7703 goto err;
7704
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007705 init_rt_bandwidth(&tg->rt_bandwidth,
7706 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007707
7708 for_each_possible_cpu(i) {
7709 rq = cpu_rq(i);
7710
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007711 rt_rq = kmalloc_node(sizeof(struct rt_rq),
7712 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7713 if (!rt_rq)
7714 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007715
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007716 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
7717 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7718 if (!rt_se)
7719 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007720
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007721 init_tg_rt_entry(rq, tg, rt_rq, rt_se, i, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007722 }
7723
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007724 return 1;
7725
7726 err:
7727 return 0;
7728}
7729
7730static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7731{
7732 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
7733 &cpu_rq(cpu)->leaf_rt_rq_list);
7734}
7735
7736static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7737{
7738 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
7739}
7740#else
7741static inline void free_rt_sched_group(struct task_group *tg)
7742{
7743}
7744
7745static inline int alloc_rt_sched_group(struct task_group *tg)
7746{
7747 return 1;
7748}
7749
7750static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7751{
7752}
7753
7754static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7755{
7756}
7757#endif
7758
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007759#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007760static void free_sched_group(struct task_group *tg)
7761{
7762 free_fair_sched_group(tg);
7763 free_rt_sched_group(tg);
7764 kfree(tg);
7765}
7766
7767/* allocate runqueue etc for a new task group */
7768struct task_group *sched_create_group(void)
7769{
7770 struct task_group *tg;
7771 unsigned long flags;
7772 int i;
7773
7774 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7775 if (!tg)
7776 return ERR_PTR(-ENOMEM);
7777
7778 if (!alloc_fair_sched_group(tg))
7779 goto err;
7780
7781 if (!alloc_rt_sched_group(tg))
7782 goto err;
7783
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007784 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007785 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007786 register_fair_sched_group(tg, i);
7787 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007788 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007789 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007790 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007791
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007792 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007793
7794err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007795 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007796 return ERR_PTR(-ENOMEM);
7797}
7798
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007799/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007800static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007801{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007802 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007803 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007804}
7805
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007806/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007807void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007808{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007809 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007810 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007811
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007812 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007813 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007814 unregister_fair_sched_group(tg, i);
7815 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007816 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007817 list_del_rcu(&tg->list);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007818 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007819
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007820 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007821 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007822}
7823
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007824/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007825 * The caller of this function should have put the task in its new group
7826 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7827 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007828 */
7829void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007830{
7831 int on_rq, running;
7832 unsigned long flags;
7833 struct rq *rq;
7834
7835 rq = task_rq_lock(tsk, &flags);
7836
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007837 update_rq_clock(rq);
7838
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007839 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007840 on_rq = tsk->se.on_rq;
7841
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007842 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007843 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007844 if (unlikely(running))
7845 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007846
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007847 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007848
Peter Zijlstra810b3812008-02-29 15:21:01 -05007849#ifdef CONFIG_FAIR_GROUP_SCHED
7850 if (tsk->sched_class->moved_group)
7851 tsk->sched_class->moved_group(tsk);
7852#endif
7853
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007854 if (unlikely(running))
7855 tsk->sched_class->set_curr_task(rq);
7856 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02007857 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007858
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007859 task_rq_unlock(rq, &flags);
7860}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007861#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007862
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007863#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007864static void set_se_shares(struct sched_entity *se, unsigned long shares)
7865{
7866 struct cfs_rq *cfs_rq = se->cfs_rq;
7867 struct rq *rq = cfs_rq->rq;
7868 int on_rq;
7869
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007870 spin_lock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007871
7872 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007873 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007874 dequeue_entity(cfs_rq, se, 0);
7875
7876 se->load.weight = shares;
7877 se->load.inv_weight = div64_64((1ULL<<32), shares);
7878
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007879 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007880 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007881
7882 spin_unlock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007883}
7884
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007885static DEFINE_MUTEX(shares_mutex);
7886
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007887int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007888{
7889 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007890 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01007891
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007892 /*
7893 * A weight of 0 or 1 can cause arithmetics problems.
7894 * (The default weight is 1024 - so there's no practical
7895 * limitation from this.)
7896 */
7897 if (shares < 2)
7898 shares = 2;
7899
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007900 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007901 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007902 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007903
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007904 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007905 for_each_possible_cpu(i)
7906 unregister_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007907 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007908
7909 /* wait for any ongoing reference to this group to finish */
7910 synchronize_sched();
7911
7912 /*
7913 * Now we are free to modify the group's share on each cpu
7914 * w/o tripping rebalance_share or load_balance_fair.
7915 */
7916 tg->shares = shares;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007917 for_each_possible_cpu(i)
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007918 set_se_shares(tg->se[i], shares);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007919
7920 /*
7921 * Enable load balance activity on this group, by inserting it back on
7922 * each cpu's rq->leaf_cfs_rq_list.
7923 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007924 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007925 for_each_possible_cpu(i)
7926 register_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007927 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007928done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007929 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007930 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007931}
7932
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007933unsigned long sched_group_shares(struct task_group *tg)
7934{
7935 return tg->shares;
7936}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007937#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007938
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007939#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007940/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007941 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007942 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007943static DEFINE_MUTEX(rt_constraints_mutex);
7944
7945static unsigned long to_ratio(u64 period, u64 runtime)
7946{
7947 if (runtime == RUNTIME_INF)
7948 return 1ULL << 16;
7949
Peter Zijlstra2692a242008-02-27 12:00:46 +01007950 return div64_64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007951}
7952
7953static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007954{
7955 struct task_group *tgi;
7956 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007957 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007958 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007959
7960 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007961 list_for_each_entry_rcu(tgi, &task_groups, list) {
7962 if (tgi == tg)
7963 continue;
7964
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007965 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
7966 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007967 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007968 rcu_read_unlock();
7969
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007970 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007971}
7972
Dhaval Giani521f1a242008-02-28 15:21:56 +05307973/* Must be called with tasklist_lock held */
7974static inline int tg_has_rt_tasks(struct task_group *tg)
7975{
7976 struct task_struct *g, *p;
7977 do_each_thread(g, p) {
7978 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
7979 return 1;
7980 } while_each_thread(g, p);
7981 return 0;
7982}
7983
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007984static int tg_set_bandwidth(struct task_group *tg,
7985 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007986{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007987 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007988
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007989 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05307990 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007991 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05307992 err = -EBUSY;
7993 goto unlock;
7994 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007995 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
7996 err = -EINVAL;
7997 goto unlock;
7998 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007999
8000 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008001 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8002 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008003
8004 for_each_possible_cpu(i) {
8005 struct rt_rq *rt_rq = tg->rt_rq[i];
8006
8007 spin_lock(&rt_rq->rt_runtime_lock);
8008 rt_rq->rt_runtime = rt_runtime;
8009 spin_unlock(&rt_rq->rt_runtime_lock);
8010 }
8011 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008012 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308013 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008014 mutex_unlock(&rt_constraints_mutex);
8015
8016 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008017}
8018
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008019int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8020{
8021 u64 rt_runtime, rt_period;
8022
8023 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8024 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8025 if (rt_runtime_us < 0)
8026 rt_runtime = RUNTIME_INF;
8027
8028 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8029}
8030
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008031long sched_group_rt_runtime(struct task_group *tg)
8032{
8033 u64 rt_runtime_us;
8034
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008035 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008036 return -1;
8037
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008038 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008039 do_div(rt_runtime_us, NSEC_PER_USEC);
8040 return rt_runtime_us;
8041}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008042
8043int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8044{
8045 u64 rt_runtime, rt_period;
8046
8047 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8048 rt_runtime = tg->rt_bandwidth.rt_runtime;
8049
8050 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8051}
8052
8053long sched_group_rt_period(struct task_group *tg)
8054{
8055 u64 rt_period_us;
8056
8057 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8058 do_div(rt_period_us, NSEC_PER_USEC);
8059 return rt_period_us;
8060}
8061
8062static int sched_rt_global_constraints(void)
8063{
8064 int ret = 0;
8065
8066 mutex_lock(&rt_constraints_mutex);
8067 if (!__rt_schedulable(NULL, 1, 0))
8068 ret = -EINVAL;
8069 mutex_unlock(&rt_constraints_mutex);
8070
8071 return ret;
8072}
8073#else
8074static int sched_rt_global_constraints(void)
8075{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008076 unsigned long flags;
8077 int i;
8078
8079 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
8080 for_each_possible_cpu(i) {
8081 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8082
8083 spin_lock(&rt_rq->rt_runtime_lock);
8084 rt_rq->rt_runtime = global_rt_runtime();
8085 spin_unlock(&rt_rq->rt_runtime_lock);
8086 }
8087 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
8088
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008089 return 0;
8090}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008091#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008092
8093int sched_rt_handler(struct ctl_table *table, int write,
8094 struct file *filp, void __user *buffer, size_t *lenp,
8095 loff_t *ppos)
8096{
8097 int ret;
8098 int old_period, old_runtime;
8099 static DEFINE_MUTEX(mutex);
8100
8101 mutex_lock(&mutex);
8102 old_period = sysctl_sched_rt_period;
8103 old_runtime = sysctl_sched_rt_runtime;
8104
8105 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8106
8107 if (!ret && write) {
8108 ret = sched_rt_global_constraints();
8109 if (ret) {
8110 sysctl_sched_rt_period = old_period;
8111 sysctl_sched_rt_runtime = old_runtime;
8112 } else {
8113 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8114 def_rt_bandwidth.rt_period =
8115 ns_to_ktime(global_rt_period());
8116 }
8117 }
8118 mutex_unlock(&mutex);
8119
8120 return ret;
8121}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008122
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008123#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008124
8125/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008126static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008127{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008128 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8129 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008130}
8131
8132static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008133cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008134{
8135 struct task_group *tg;
8136
Paul Menage2b01dfe2007-10-24 18:23:50 +02008137 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008138 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008139 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008140 return &init_task_group.css;
8141 }
8142
8143 /* we support only 1-level deep hierarchical scheduler atm */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008144 if (cgrp->parent->parent)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008145 return ERR_PTR(-EINVAL);
8146
8147 tg = sched_create_group();
8148 if (IS_ERR(tg))
8149 return ERR_PTR(-ENOMEM);
8150
8151 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008152 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008153
8154 return &tg->css;
8155}
8156
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008157static void
8158cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008159{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008160 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008161
8162 sched_destroy_group(tg);
8163}
8164
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008165static int
8166cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8167 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008168{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008169#ifdef CONFIG_RT_GROUP_SCHED
8170 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008171 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008172 return -EINVAL;
8173#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008174 /* We don't support RT-tasks being in separate groups */
8175 if (tsk->sched_class != &fair_sched_class)
8176 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008177#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008178
8179 return 0;
8180}
8181
8182static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008183cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008184 struct cgroup *old_cont, struct task_struct *tsk)
8185{
8186 sched_move_task(tsk);
8187}
8188
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008189#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menage2b01dfe2007-10-24 18:23:50 +02008190static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8191 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008192{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008193 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008194}
8195
Paul Menage2b01dfe2007-10-24 18:23:50 +02008196static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008197{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008198 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008199
8200 return (u64) tg->shares;
8201}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008202#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008203
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008204#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008205static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008206 struct file *file,
8207 const char __user *userbuf,
8208 size_t nbytes, loff_t *unused_ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008209{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008210 char buffer[64];
8211 int retval = 0;
8212 s64 val;
8213 char *end;
8214
8215 if (!nbytes)
8216 return -EINVAL;
8217 if (nbytes >= sizeof(buffer))
8218 return -E2BIG;
8219 if (copy_from_user(buffer, userbuf, nbytes))
8220 return -EFAULT;
8221
8222 buffer[nbytes] = 0; /* nul-terminate */
8223
8224 /* strip newline if necessary */
8225 if (nbytes && (buffer[nbytes-1] == '\n'))
8226 buffer[nbytes-1] = 0;
8227 val = simple_strtoll(buffer, &end, 0);
8228 if (*end)
8229 return -EINVAL;
8230
8231 /* Pass to subsystem */
8232 retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
8233 if (!retval)
8234 retval = nbytes;
8235 return retval;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008236}
8237
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008238static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
8239 struct file *file,
8240 char __user *buf, size_t nbytes,
8241 loff_t *ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008242{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008243 char tmp[64];
8244 long val = sched_group_rt_runtime(cgroup_tg(cgrp));
8245 int len = sprintf(tmp, "%ld\n", val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008246
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008247 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008248}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008249
8250static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8251 u64 rt_period_us)
8252{
8253 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
8254}
8255
8256static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
8257{
8258 return sched_group_rt_period(cgroup_tg(cgrp));
8259}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008260#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008261
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008262static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008263#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008264 {
8265 .name = "shares",
8266 .read_uint = cpu_shares_read_uint,
8267 .write_uint = cpu_shares_write_uint,
8268 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008269#endif
8270#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008271 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008272 .name = "rt_runtime_us",
8273 .read = cpu_rt_runtime_read,
8274 .write = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008275 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008276 {
8277 .name = "rt_period_us",
8278 .read_uint = cpu_rt_period_read_uint,
8279 .write_uint = cpu_rt_period_write_uint,
8280 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008281#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008282};
8283
8284static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8285{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008286 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008287}
8288
8289struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008290 .name = "cpu",
8291 .create = cpu_cgroup_create,
8292 .destroy = cpu_cgroup_destroy,
8293 .can_attach = cpu_cgroup_can_attach,
8294 .attach = cpu_cgroup_attach,
8295 .populate = cpu_cgroup_populate,
8296 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008297 .early_init = 1,
8298};
8299
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008300#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008301
8302#ifdef CONFIG_CGROUP_CPUACCT
8303
8304/*
8305 * CPU accounting code for task groups.
8306 *
8307 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8308 * (balbir@in.ibm.com).
8309 */
8310
8311/* track cpu usage of a group of tasks */
8312struct cpuacct {
8313 struct cgroup_subsys_state css;
8314 /* cpuusage holds pointer to a u64-type object on every cpu */
8315 u64 *cpuusage;
8316};
8317
8318struct cgroup_subsys cpuacct_subsys;
8319
8320/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308321static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008322{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308323 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008324 struct cpuacct, css);
8325}
8326
8327/* return cpu accounting group to which this task belongs */
8328static inline struct cpuacct *task_ca(struct task_struct *tsk)
8329{
8330 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8331 struct cpuacct, css);
8332}
8333
8334/* create a new cpu accounting group */
8335static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05308336 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008337{
8338 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
8339
8340 if (!ca)
8341 return ERR_PTR(-ENOMEM);
8342
8343 ca->cpuusage = alloc_percpu(u64);
8344 if (!ca->cpuusage) {
8345 kfree(ca);
8346 return ERR_PTR(-ENOMEM);
8347 }
8348
8349 return &ca->css;
8350}
8351
8352/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008353static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05308354cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008355{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308356 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008357
8358 free_percpu(ca->cpuusage);
8359 kfree(ca);
8360}
8361
8362/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308363static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008364{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308365 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008366 u64 totalcpuusage = 0;
8367 int i;
8368
8369 for_each_possible_cpu(i) {
8370 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
8371
8372 /*
8373 * Take rq->lock to make 64-bit addition safe on 32-bit
8374 * platforms.
8375 */
8376 spin_lock_irq(&cpu_rq(i)->lock);
8377 totalcpuusage += *cpuusage;
8378 spin_unlock_irq(&cpu_rq(i)->lock);
8379 }
8380
8381 return totalcpuusage;
8382}
8383
8384static struct cftype files[] = {
8385 {
8386 .name = "usage",
8387 .read_uint = cpuusage_read,
8388 },
8389};
8390
Dhaval Giani32cd7562008-02-29 10:02:43 +05308391static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008392{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308393 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008394}
8395
8396/*
8397 * charge this task's execution time to its accounting group.
8398 *
8399 * called with rq->lock held.
8400 */
8401static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
8402{
8403 struct cpuacct *ca;
8404
8405 if (!cpuacct_subsys.active)
8406 return;
8407
8408 ca = task_ca(tsk);
8409 if (ca) {
8410 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
8411
8412 *cpuusage += cputime;
8413 }
8414}
8415
8416struct cgroup_subsys cpuacct_subsys = {
8417 .name = "cpuacct",
8418 .create = cpuacct_create,
8419 .destroy = cpuacct_destroy,
8420 .populate = cpuacct_populate,
8421 .subsys_id = cpuacct_subsys_id,
8422};
8423#endif /* CONFIG_CGROUP_CPUACCT */