blob: bb20323f7d09f4a8a28649dcb26c743242407e38 [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;
167 struct hrtimer rt_period_timer;
168};
169
170static struct rt_bandwidth def_rt_bandwidth;
171
172static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
173
174static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
175{
176 struct rt_bandwidth *rt_b =
177 container_of(timer, struct rt_bandwidth, rt_period_timer);
178 ktime_t now;
179 int overrun;
180 int idle = 0;
181
182 for (;;) {
183 now = hrtimer_cb_get_time(timer);
184 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
185
186 if (!overrun)
187 break;
188
189 idle = do_sched_rt_period_timer(rt_b, overrun);
190 }
191
192 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
193}
194
195static
196void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
197{
198 rt_b->rt_period = ns_to_ktime(period);
199 rt_b->rt_runtime = runtime;
200
201 hrtimer_init(&rt_b->rt_period_timer,
202 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
203 rt_b->rt_period_timer.function = sched_rt_period_timer;
204 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
205}
206
207static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
208{
209 ktime_t now;
210
211 if (rt_b->rt_runtime == RUNTIME_INF)
212 return;
213
214 if (hrtimer_active(&rt_b->rt_period_timer))
215 return;
216
217 spin_lock(&rt_b->rt_runtime_lock);
218 for (;;) {
219 if (hrtimer_active(&rt_b->rt_period_timer))
220 break;
221
222 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
223 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
224 hrtimer_start(&rt_b->rt_period_timer,
225 rt_b->rt_period_timer.expires,
226 HRTIMER_MODE_ABS);
227 }
228 spin_unlock(&rt_b->rt_runtime_lock);
229}
230
231#ifdef CONFIG_RT_GROUP_SCHED
232static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
233{
234 hrtimer_cancel(&rt_b->rt_period_timer);
235}
236#endif
237
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100238#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200239
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700240#include <linux/cgroup.h>
241
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200242struct cfs_rq;
243
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100244static LIST_HEAD(task_groups);
245
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200246/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200247struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100248#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700249 struct cgroup_subsys_state css;
250#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100251
252#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200253 /* schedulable entities of this group on each cpu */
254 struct sched_entity **se;
255 /* runqueue "owned" by this group on each cpu */
256 struct cfs_rq **cfs_rq;
257 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100258#endif
259
260#ifdef CONFIG_RT_GROUP_SCHED
261 struct sched_rt_entity **rt_se;
262 struct rt_rq **rt_rq;
263
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200264 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100265#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100266
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100267 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100268 struct list_head list;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200269};
270
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100271#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200272/* Default task group's sched entity on each cpu */
273static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
274/* Default task group's cfs_rq on each cpu */
275static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
276
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100277static struct sched_entity *init_sched_entity_p[NR_CPUS];
278static struct cfs_rq *init_cfs_rq_p[NR_CPUS];
279#endif
280
281#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100282static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
283static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
284
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100285static struct sched_rt_entity *init_sched_rt_entity_p[NR_CPUS];
286static struct rt_rq *init_rt_rq_p[NR_CPUS];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100287#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100288
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100289/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100290 * a task group's cpu shares.
291 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100292static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100293
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100294/* doms_cur_mutex serializes access to doms_cur[] array */
295static DEFINE_MUTEX(doms_cur_mutex);
296
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100297#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100298#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100299# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200300#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100301# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200302#endif
303
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100304static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100305#endif
306
307/* Default task group.
308 * Every task in system belong to this group at bootup.
309 */
310struct task_group init_task_group = {
311#ifdef CONFIG_FAIR_GROUP_SCHED
312 .se = init_sched_entity_p,
313 .cfs_rq = init_cfs_rq_p,
314#endif
315
316#ifdef CONFIG_RT_GROUP_SCHED
317 .rt_se = init_sched_rt_entity_p,
318 .rt_rq = init_rt_rq_p,
319#endif
320};
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200321
322/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200323static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200324{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200325 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200326
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100327#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200328 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100329#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700330 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
331 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200332#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100333 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200334#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200335 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200336}
337
338/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100339static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200340{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100341#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100342 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
343 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100344#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100345
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100346#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100347 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
348 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100349#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200350}
351
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100352static inline void lock_doms_cur(void)
353{
354 mutex_lock(&doms_cur_mutex);
355}
356
357static inline void unlock_doms_cur(void)
358{
359 mutex_unlock(&doms_cur_mutex);
360}
361
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200362#else
363
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100364static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100365static inline void lock_doms_cur(void) { }
366static inline void unlock_doms_cur(void) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200367
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100368#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200369
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200370/* CFS-related fields in a runqueue */
371struct cfs_rq {
372 struct load_weight load;
373 unsigned long nr_running;
374
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200375 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200376 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200377
378 struct rb_root tasks_timeline;
379 struct rb_node *rb_leftmost;
380 struct rb_node *rb_load_balance_curr;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200381 /* 'curr' points to currently running entity on this cfs_rq.
382 * It is set to NULL otherwise (i.e when none are currently running).
383 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100384 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200385
386 unsigned long nr_spread_over;
387
Ingo Molnar62160e3f2007-10-15 17:00:03 +0200388#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200389 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
390
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100391 /*
392 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200393 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
394 * (like users, containers etc.)
395 *
396 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
397 * list is used during load balance.
398 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100399 struct list_head leaf_cfs_rq_list;
400 struct task_group *tg; /* group that "owns" this runqueue */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200401#endif
402};
403
404/* Real-Time classes' related field in a runqueue: */
405struct rt_rq {
406 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100407 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100408#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100409 int highest_prio; /* highest queued rt task prio */
410#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100411#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100412 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100413 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100414#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100415 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100416 u64 rt_time;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100417
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100418#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100419 unsigned long rt_nr_boosted;
420
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100421 struct rq *rq;
422 struct list_head leaf_rt_rq_list;
423 struct task_group *tg;
424 struct sched_rt_entity *rt_se;
425#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200426};
427
Gregory Haskins57d885f2008-01-25 21:08:18 +0100428#ifdef CONFIG_SMP
429
430/*
431 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100432 * variables. Each exclusive cpuset essentially defines an island domain by
433 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100434 * exclusive cpuset is created, we also create and attach a new root-domain
435 * object.
436 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100437 */
438struct root_domain {
439 atomic_t refcount;
440 cpumask_t span;
441 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100442
Ingo Molnar0eab9142008-01-25 21:08:19 +0100443 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100444 * The "RT overload" flag: it gets set if a CPU has more than
445 * one runnable RT task.
446 */
447 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100448 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100449};
450
Gregory Haskinsdc938522008-01-25 21:08:26 +0100451/*
452 * By default the system creates a single root-domain with all cpus as
453 * members (mimicking the global state we have today).
454 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100455static struct root_domain def_root_domain;
456
457#endif
458
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200459/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * This is the main, per-CPU runqueue data structure.
461 *
462 * Locking rule: those places that want to lock multiple runqueues
463 * (such as the load balancing or the thread migration code), lock
464 * acquire operations must be ordered by ascending &runqueue.
465 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700466struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200467 /* runqueue lock: */
468 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 /*
471 * nr_running and cpu_load should be in the same cacheline because
472 * remote CPUs use both these fields when doing load calculation.
473 */
474 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200475 #define CPU_LOAD_IDX_MAX 5
476 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700477 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700478#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200479 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700480 unsigned char in_nohz_recently;
481#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200482 /* capture load from *all* tasks on this cpu: */
483 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200484 unsigned long nr_load_updates;
485 u64 nr_switches;
486
487 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100488 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100489
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200490#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200491 /* list of leaf cfs_rq on this cpu: */
492 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100493#endif
494#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100495 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /*
499 * This is part of a global counter where only the total sum
500 * over all CPUs matters. A task can increase this counter on
501 * one CPU and if it got migrated afterwards it may decrease
502 * it on another CPU. Always updated under the runqueue lock:
503 */
504 unsigned long nr_uninterruptible;
505
Ingo Molnar36c8b582006-07-03 00:25:41 -0700506 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800507 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200509
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200510 u64 clock, prev_clock_raw;
511 s64 clock_max_delta;
512
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100513 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200514 u64 idle_clock;
515 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200516 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 atomic_t nr_iowait;
519
520#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100521 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct sched_domain *sd;
523
524 /* For active balancing */
525 int active_balance;
526 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200527 /* cpu of this runqueue: */
528 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Ingo Molnar36c8b582006-07-03 00:25:41 -0700530 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct list_head migration_queue;
532#endif
533
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100534#ifdef CONFIG_SCHED_HRTICK
535 unsigned long hrtick_flags;
536 ktime_t hrtick_expire;
537 struct hrtimer hrtick_timer;
538#endif
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#ifdef CONFIG_SCHEDSTATS
541 /* latency stats */
542 struct sched_info rq_sched_info;
543
544 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200545 unsigned int yld_exp_empty;
546 unsigned int yld_act_empty;
547 unsigned int yld_both_empty;
548 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200551 unsigned int sched_switch;
552 unsigned int sched_count;
553 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200556 unsigned int ttwu_count;
557 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200558
559 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200560 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700562 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563};
564
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700565static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Ingo Molnardd41f592007-07-09 18:51:59 +0200567static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
568{
569 rq->curr->sched_class->check_preempt_curr(rq, p);
570}
571
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700572static inline int cpu_of(struct rq *rq)
573{
574#ifdef CONFIG_SMP
575 return rq->cpu;
576#else
577 return 0;
578#endif
579}
580
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200581#ifdef CONFIG_NO_HZ
582static inline bool nohz_on(int cpu)
583{
584 return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE;
585}
586
587static inline u64 max_skipped_ticks(struct rq *rq)
588{
589 return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1;
590}
591
592static inline void update_last_tick_seen(struct rq *rq)
593{
594 rq->last_tick_seen = jiffies;
595}
596#else
597static inline u64 max_skipped_ticks(struct rq *rq)
598{
599 return 1;
600}
601
602static inline void update_last_tick_seen(struct rq *rq)
603{
604}
605#endif
606
Nick Piggin674311d2005-06-25 14:57:27 -0700607/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200608 * Update the per-runqueue clock, as finegrained as the platform can give
609 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200610 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200611static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200612{
613 u64 prev_raw = rq->prev_clock_raw;
614 u64 now = sched_clock();
615 s64 delta = now - prev_raw;
616 u64 clock = rq->clock;
617
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200618#ifdef CONFIG_SCHED_DEBUG
619 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
620#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200621 /*
622 * Protect against sched_clock() occasionally going backwards:
623 */
624 if (unlikely(delta < 0)) {
625 clock++;
626 rq->clock_warps++;
627 } else {
628 /*
629 * Catch too large forward jumps too:
630 */
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200631 u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC;
632 u64 max_time = rq->tick_timestamp + max_jump;
633
634 if (unlikely(clock + delta > max_time)) {
635 if (clock < max_time)
636 clock = max_time;
Ingo Molnar529c7722007-08-10 23:05:11 +0200637 else
638 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200639 rq->clock_overflows++;
640 } else {
641 if (unlikely(delta > rq->clock_max_delta))
642 rq->clock_max_delta = delta;
643 clock += delta;
644 }
645 }
646
647 rq->prev_clock_raw = now;
648 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200649}
650
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200651static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200652{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200653 if (likely(smp_processor_id() == cpu_of(rq)))
654 __update_rq_clock(rq);
655}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200656
Ingo Molnar20d315d2007-07-09 18:51:58 +0200657/*
Nick Piggin674311d2005-06-25 14:57:27 -0700658 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700659 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700660 *
661 * The domain tree of any CPU may only be accessed from within
662 * preempt-disabled sections.
663 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700664#define for_each_domain(cpu, __sd) \
665 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
668#define this_rq() (&__get_cpu_var(runqueues))
669#define task_rq(p) cpu_rq(task_cpu(p))
670#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
671
Ingo Molnare436d802007-07-19 21:28:35 +0200672/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200673 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
674 */
675#ifdef CONFIG_SCHED_DEBUG
676# define const_debug __read_mostly
677#else
678# define const_debug static const
679#endif
680
681/*
682 * Debugging: various feature bits
683 */
684enum {
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200685 SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
Ingo Molnar96126332007-11-15 20:57:40 +0100686 SCHED_FEAT_WAKEUP_PREEMPT = 2,
687 SCHED_FEAT_START_DEBIT = 4,
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100688 SCHED_FEAT_AFFINE_WAKEUPS = 8,
689 SCHED_FEAT_CACHE_HOT_BUDDY = 16,
Ingo Molnar02e2b832008-03-19 01:37:10 +0100690 SCHED_FEAT_SYNC_WAKEUPS = 32,
691 SCHED_FEAT_HRTICK = 64,
692 SCHED_FEAT_DOUBLE_TICK = 128,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200693};
694
695const_debug unsigned int sysctl_sched_features =
Ingo Molnar8401f772007-10-18 21:32:55 +0200696 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
Ingo Molnar96126332007-11-15 20:57:40 +0100697 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
Ingo Molnar8401f772007-10-18 21:32:55 +0200698 SCHED_FEAT_START_DEBIT * 1 |
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100699 SCHED_FEAT_AFFINE_WAKEUPS * 1 |
700 SCHED_FEAT_CACHE_HOT_BUDDY * 1 |
Ingo Molnar02e2b832008-03-19 01:37:10 +0100701 SCHED_FEAT_SYNC_WAKEUPS * 1 |
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100702 SCHED_FEAT_HRTICK * 1 |
Ingo Molnar02e2b832008-03-19 01:37:10 +0100703 SCHED_FEAT_DOUBLE_TICK * 0;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200704
705#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
706
707/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100708 * Number of tasks to iterate in a single balance run.
709 * Limited because this is done with IRQs disabled.
710 */
711const_debug unsigned int sysctl_sched_nr_migrate = 32;
712
713/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100714 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100715 * default: 1s
716 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100717unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100718
Ingo Molnar6892b752008-02-13 14:02:36 +0100719static __read_mostly int scheduler_running;
720
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100721/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100722 * part of the period that we allow rt tasks to run in us.
723 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100724 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100725int sysctl_sched_rt_runtime = 950000;
726
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200727static inline u64 global_rt_period(void)
728{
729 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
730}
731
732static inline u64 global_rt_runtime(void)
733{
734 if (sysctl_sched_rt_period < 0)
735 return RUNTIME_INF;
736
737 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
738}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100739
Ingo Molnar27ec4402008-02-28 21:00:21 +0100740static const unsigned long long time_sync_thresh = 100000;
741
742static DEFINE_PER_CPU(unsigned long long, time_offset);
743static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
744
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100745/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100746 * Global lock which we take every now and then to synchronize
747 * the CPUs time. This method is not warp-safe, but it's good
748 * enough to synchronize slowly diverging time sources and thus
749 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200750 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100751static DEFINE_SPINLOCK(time_sync_lock);
752static unsigned long long prev_global_time;
753
754static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
755{
756 unsigned long flags;
757
758 spin_lock_irqsave(&time_sync_lock, flags);
759
760 if (time < prev_global_time) {
761 per_cpu(time_offset, cpu) += prev_global_time - time;
762 time = prev_global_time;
763 } else {
764 prev_global_time = time;
765 }
766
767 spin_unlock_irqrestore(&time_sync_lock, flags);
768
769 return time;
770}
771
772static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200773{
Ingo Molnare436d802007-07-19 21:28:35 +0200774 unsigned long long now;
775 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200776 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200777
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100778 /*
779 * Only call sched_clock() if the scheduler has already been
780 * initialized (some code might call cpu_clock() very early):
781 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100782 if (unlikely(!scheduler_running))
783 return 0;
784
785 local_irq_save(flags);
786 rq = cpu_rq(cpu);
787 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200788 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200789 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200790
791 return now;
792}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100793
794/*
795 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
796 * clock constructed from sched_clock():
797 */
798unsigned long long cpu_clock(int cpu)
799{
800 unsigned long long prev_cpu_time, time, delta_time;
801
802 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
803 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
804 delta_time = time-prev_cpu_time;
805
806 if (unlikely(delta_time > time_sync_thresh))
807 time = __sync_cpu_clock(time, cpu);
808
809 return time;
810}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200811EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700814# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700816#ifndef finish_arch_switch
817# define finish_arch_switch(prev) do { } while (0)
818#endif
819
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100820static inline int task_current(struct rq *rq, struct task_struct *p)
821{
822 return rq->curr == p;
823}
824
Nick Piggin4866cde2005-06-25 14:57:23 -0700825#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700826static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700827{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100828 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700829}
830
Ingo Molnar70b97a72006-07-03 00:25:42 -0700831static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700832{
833}
834
Ingo Molnar70b97a72006-07-03 00:25:42 -0700835static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700836{
Ingo Molnarda04c032005-09-13 11:17:59 +0200837#ifdef CONFIG_DEBUG_SPINLOCK
838 /* this is a valid case when another task releases the spinlock */
839 rq->lock.owner = current;
840#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700841 /*
842 * If we are tracking spinlock dependencies then we have to
843 * fix up the runqueue lock - which gets 'carried over' from
844 * prev into current:
845 */
846 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
847
Nick Piggin4866cde2005-06-25 14:57:23 -0700848 spin_unlock_irq(&rq->lock);
849}
850
851#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700852static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700853{
854#ifdef CONFIG_SMP
855 return p->oncpu;
856#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100857 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700858#endif
859}
860
Ingo Molnar70b97a72006-07-03 00:25:42 -0700861static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700862{
863#ifdef CONFIG_SMP
864 /*
865 * We can optimise this out completely for !SMP, because the
866 * SMP rebalancing from interrupt is the only thing that cares
867 * here.
868 */
869 next->oncpu = 1;
870#endif
871#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
872 spin_unlock_irq(&rq->lock);
873#else
874 spin_unlock(&rq->lock);
875#endif
876}
877
Ingo Molnar70b97a72006-07-03 00:25:42 -0700878static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700879{
880#ifdef CONFIG_SMP
881 /*
882 * After ->oncpu is cleared, the task can be moved to a different CPU.
883 * We must ensure this doesn't happen until the switch is completely
884 * finished.
885 */
886 smp_wmb();
887 prev->oncpu = 0;
888#endif
889#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
890 local_irq_enable();
891#endif
892}
893#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700896 * __task_rq_lock - lock the runqueue a given task resides on.
897 * Must be called interrupts disabled.
898 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700899static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700900 __acquires(rq->lock)
901{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200902 for (;;) {
903 struct rq *rq = task_rq(p);
904 spin_lock(&rq->lock);
905 if (likely(rq == task_rq(p)))
906 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700907 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700908 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700909}
910
911/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100913 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 * explicitly disabling preemption.
915 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700916static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 __acquires(rq->lock)
918{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700919 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Andi Kleen3a5c3592007-10-15 17:00:14 +0200921 for (;;) {
922 local_irq_save(*flags);
923 rq = task_rq(p);
924 spin_lock(&rq->lock);
925 if (likely(rq == task_rq(p)))
926 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Alexey Dobriyana9957442007-10-15 17:00:13 +0200931static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700932 __releases(rq->lock)
933{
934 spin_unlock(&rq->lock);
935}
936
Ingo Molnar70b97a72006-07-03 00:25:42 -0700937static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 __releases(rq->lock)
939{
940 spin_unlock_irqrestore(&rq->lock, *flags);
941}
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800944 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200946static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 __acquires(rq->lock)
948{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700949 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 local_irq_disable();
952 rq = this_rq();
953 spin_lock(&rq->lock);
954
955 return rq;
956}
957
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200958/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200959 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200960 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200961void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200962{
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200963 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200964
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200965 spin_lock(&rq->lock);
966 __update_rq_clock(rq);
967 spin_unlock(&rq->lock);
968 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200969}
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200970EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
971
972/*
973 * We just idled delta nanoseconds (called with irqs disabled):
974 */
975void sched_clock_idle_wakeup_event(u64 delta_ns)
976{
977 struct rq *rq = cpu_rq(smp_processor_id());
978 u64 now = sched_clock();
979
980 rq->idle_clock += delta_ns;
981 /*
982 * Override the previous timestamp and ignore all
983 * sched_clock() deltas that occured while we idled,
984 * and use the PM-provided delta_ns to advance the
985 * rq clock:
986 */
987 spin_lock(&rq->lock);
988 rq->prev_clock_raw = now;
989 rq->clock += delta_ns;
990 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +0100991 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200992}
993EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200994
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100995static void __resched_task(struct task_struct *p, int tif_bit);
996
997static inline void resched_task(struct task_struct *p)
998{
999 __resched_task(p, TIF_NEED_RESCHED);
1000}
1001
1002#ifdef CONFIG_SCHED_HRTICK
1003/*
1004 * Use HR-timers to deliver accurate preemption points.
1005 *
1006 * Its all a bit involved since we cannot program an hrt while holding the
1007 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1008 * reschedule event.
1009 *
1010 * When we get rescheduled we reprogram the hrtick_timer outside of the
1011 * rq->lock.
1012 */
1013static inline void resched_hrt(struct task_struct *p)
1014{
1015 __resched_task(p, TIF_HRTICK_RESCHED);
1016}
1017
1018static inline void resched_rq(struct rq *rq)
1019{
1020 unsigned long flags;
1021
1022 spin_lock_irqsave(&rq->lock, flags);
1023 resched_task(rq->curr);
1024 spin_unlock_irqrestore(&rq->lock, flags);
1025}
1026
1027enum {
1028 HRTICK_SET, /* re-programm hrtick_timer */
1029 HRTICK_RESET, /* not a new slice */
1030};
1031
1032/*
1033 * Use hrtick when:
1034 * - enabled by features
1035 * - hrtimer is actually high res
1036 */
1037static inline int hrtick_enabled(struct rq *rq)
1038{
1039 if (!sched_feat(HRTICK))
1040 return 0;
1041 return hrtimer_is_hres_active(&rq->hrtick_timer);
1042}
1043
1044/*
1045 * Called to set the hrtick timer state.
1046 *
1047 * called with rq->lock held and irqs disabled
1048 */
1049static void hrtick_start(struct rq *rq, u64 delay, int reset)
1050{
1051 assert_spin_locked(&rq->lock);
1052
1053 /*
1054 * preempt at: now + delay
1055 */
1056 rq->hrtick_expire =
1057 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1058 /*
1059 * indicate we need to program the timer
1060 */
1061 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1062 if (reset)
1063 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1064
1065 /*
1066 * New slices are called from the schedule path and don't need a
1067 * forced reschedule.
1068 */
1069 if (reset)
1070 resched_hrt(rq->curr);
1071}
1072
1073static void hrtick_clear(struct rq *rq)
1074{
1075 if (hrtimer_active(&rq->hrtick_timer))
1076 hrtimer_cancel(&rq->hrtick_timer);
1077}
1078
1079/*
1080 * Update the timer from the possible pending state.
1081 */
1082static void hrtick_set(struct rq *rq)
1083{
1084 ktime_t time;
1085 int set, reset;
1086 unsigned long flags;
1087
1088 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1089
1090 spin_lock_irqsave(&rq->lock, flags);
1091 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1092 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1093 time = rq->hrtick_expire;
1094 clear_thread_flag(TIF_HRTICK_RESCHED);
1095 spin_unlock_irqrestore(&rq->lock, flags);
1096
1097 if (set) {
1098 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1099 if (reset && !hrtimer_active(&rq->hrtick_timer))
1100 resched_rq(rq);
1101 } else
1102 hrtick_clear(rq);
1103}
1104
1105/*
1106 * High-resolution timer tick.
1107 * Runs from hardirq context with interrupts disabled.
1108 */
1109static enum hrtimer_restart hrtick(struct hrtimer *timer)
1110{
1111 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1112
1113 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1114
1115 spin_lock(&rq->lock);
1116 __update_rq_clock(rq);
1117 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1118 spin_unlock(&rq->lock);
1119
1120 return HRTIMER_NORESTART;
1121}
1122
1123static inline void init_rq_hrtick(struct rq *rq)
1124{
1125 rq->hrtick_flags = 0;
1126 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1127 rq->hrtick_timer.function = hrtick;
1128 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1129}
1130
1131void hrtick_resched(void)
1132{
1133 struct rq *rq;
1134 unsigned long flags;
1135
1136 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1137 return;
1138
1139 local_irq_save(flags);
1140 rq = cpu_rq(smp_processor_id());
1141 hrtick_set(rq);
1142 local_irq_restore(flags);
1143}
1144#else
1145static inline void hrtick_clear(struct rq *rq)
1146{
1147}
1148
1149static inline void hrtick_set(struct rq *rq)
1150{
1151}
1152
1153static inline void init_rq_hrtick(struct rq *rq)
1154{
1155}
1156
1157void hrtick_resched(void)
1158{
1159}
1160#endif
1161
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001162/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001163 * resched_task - mark a task 'to be rescheduled now'.
1164 *
1165 * On UP this means the setting of the need_resched flag, on SMP it
1166 * might also involve a cross-CPU call to trigger the scheduler on
1167 * the target CPU.
1168 */
1169#ifdef CONFIG_SMP
1170
1171#ifndef tsk_is_polling
1172#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1173#endif
1174
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001175static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001176{
1177 int cpu;
1178
1179 assert_spin_locked(&task_rq(p)->lock);
1180
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001181 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001182 return;
1183
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001184 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001185
1186 cpu = task_cpu(p);
1187 if (cpu == smp_processor_id())
1188 return;
1189
1190 /* NEED_RESCHED must be visible before we test polling */
1191 smp_mb();
1192 if (!tsk_is_polling(p))
1193 smp_send_reschedule(cpu);
1194}
1195
1196static void resched_cpu(int cpu)
1197{
1198 struct rq *rq = cpu_rq(cpu);
1199 unsigned long flags;
1200
1201 if (!spin_trylock_irqsave(&rq->lock, flags))
1202 return;
1203 resched_task(cpu_curr(cpu));
1204 spin_unlock_irqrestore(&rq->lock, flags);
1205}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001206
1207#ifdef CONFIG_NO_HZ
1208/*
1209 * When add_timer_on() enqueues a timer into the timer wheel of an
1210 * idle CPU then this timer might expire before the next timer event
1211 * which is scheduled to wake up that CPU. In case of a completely
1212 * idle system the next event might even be infinite time into the
1213 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1214 * leaves the inner idle loop so the newly added timer is taken into
1215 * account when the CPU goes back to idle and evaluates the timer
1216 * wheel for the next timer event.
1217 */
1218void wake_up_idle_cpu(int cpu)
1219{
1220 struct rq *rq = cpu_rq(cpu);
1221
1222 if (cpu == smp_processor_id())
1223 return;
1224
1225 /*
1226 * This is safe, as this function is called with the timer
1227 * wheel base lock of (cpu) held. When the CPU is on the way
1228 * to idle and has not yet set rq->curr to idle then it will
1229 * be serialized on the timer wheel base lock and take the new
1230 * timer into account automatically.
1231 */
1232 if (rq->curr != rq->idle)
1233 return;
1234
1235 /*
1236 * We can set TIF_RESCHED on the idle task of the other CPU
1237 * lockless. The worst case is that the other CPU runs the
1238 * idle task through an additional NOOP schedule()
1239 */
1240 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1241
1242 /* NEED_RESCHED must be visible before we test polling */
1243 smp_mb();
1244 if (!tsk_is_polling(rq->idle))
1245 smp_send_reschedule(cpu);
1246}
1247#endif
1248
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001249#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001250static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001251{
1252 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001253 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001254}
1255#endif
1256
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001257#if BITS_PER_LONG == 32
1258# define WMULT_CONST (~0UL)
1259#else
1260# define WMULT_CONST (1UL << 32)
1261#endif
1262
1263#define WMULT_SHIFT 32
1264
Ingo Molnar194081e2007-08-09 11:16:51 +02001265/*
1266 * Shift right and round:
1267 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001268#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001269
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001270static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001271calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1272 struct load_weight *lw)
1273{
1274 u64 tmp;
1275
1276 if (unlikely(!lw->inv_weight))
Ingo Molnar27d11722008-03-14 22:20:01 +01001277 lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001278
1279 tmp = (u64)delta_exec * weight;
1280 /*
1281 * Check whether we'd overflow the 64-bit multiplication:
1282 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001283 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001284 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001285 WMULT_SHIFT/2);
1286 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001287 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001288
Ingo Molnarecf691d2007-08-02 17:41:40 +02001289 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001290}
1291
1292static inline unsigned long
1293calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1294{
1295 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1296}
1297
Ingo Molnar10919852007-10-15 17:00:04 +02001298static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001299{
1300 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001301 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001302}
1303
Ingo Molnar10919852007-10-15 17:00:04 +02001304static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001305{
1306 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001307 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001311 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1312 * of tasks with abnormal "nice" values across CPUs the contribution that
1313 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001314 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001315 * scaled version of the new time slice allocation that they receive on time
1316 * slice expiry etc.
1317 */
1318
Ingo Molnardd41f592007-07-09 18:51:59 +02001319#define WEIGHT_IDLEPRIO 2
1320#define WMULT_IDLEPRIO (1 << 31)
1321
1322/*
1323 * Nice levels are multiplicative, with a gentle 10% change for every
1324 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1325 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1326 * that remained on nice 0.
1327 *
1328 * The "10% effect" is relative and cumulative: from _any_ nice level,
1329 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001330 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1331 * If a task goes up by ~10% and another task goes down by ~10% then
1332 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001333 */
1334static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001335 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1336 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1337 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1338 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1339 /* 0 */ 1024, 820, 655, 526, 423,
1340 /* 5 */ 335, 272, 215, 172, 137,
1341 /* 10 */ 110, 87, 70, 56, 45,
1342 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001343};
1344
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001345/*
1346 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1347 *
1348 * In cases where the weight does not change often, we can use the
1349 * precalculated inverse to speed up arithmetics by turning divisions
1350 * into multiplications:
1351 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001352static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001353 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1354 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1355 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1356 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1357 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1358 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1359 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1360 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001361};
Peter Williams2dd73a42006-06-27 02:54:34 -07001362
Ingo Molnardd41f592007-07-09 18:51:59 +02001363static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1364
1365/*
1366 * runqueue iterator, to support SMP load-balancing between different
1367 * scheduling classes, without having to expose their internal data
1368 * structures to the load-balancing proper:
1369 */
1370struct rq_iterator {
1371 void *arg;
1372 struct task_struct *(*start)(void *);
1373 struct task_struct *(*next)(void *);
1374};
1375
Peter Williamse1d14842007-10-24 18:23:51 +02001376#ifdef CONFIG_SMP
1377static unsigned long
1378balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1379 unsigned long max_load_move, struct sched_domain *sd,
1380 enum cpu_idle_type idle, int *all_pinned,
1381 int *this_best_prio, struct rq_iterator *iterator);
1382
1383static int
1384iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1385 struct sched_domain *sd, enum cpu_idle_type idle,
1386 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001387#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001388
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001389#ifdef CONFIG_CGROUP_CPUACCT
1390static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1391#else
1392static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1393#endif
1394
Gregory Haskinse7693a32008-01-25 21:08:09 +01001395#ifdef CONFIG_SMP
1396static unsigned long source_load(int cpu, int type);
1397static unsigned long target_load(int cpu, int type);
1398static unsigned long cpu_avg_load_per_task(int cpu);
1399static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1400#endif /* CONFIG_SMP */
1401
Ingo Molnardd41f592007-07-09 18:51:59 +02001402#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001403#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001404#include "sched_fair.c"
1405#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001406#ifdef CONFIG_SCHED_DEBUG
1407# include "sched_debug.c"
1408#endif
1409
1410#define sched_class_highest (&rt_sched_class)
1411
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001412static inline void inc_load(struct rq *rq, const struct task_struct *p)
Ingo Molnar9c217242007-08-02 17:41:40 +02001413{
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001414 update_load_add(&rq->load, p->se.load.weight);
Ingo Molnar9c217242007-08-02 17:41:40 +02001415}
1416
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001417static inline void dec_load(struct rq *rq, const struct task_struct *p)
1418{
1419 update_load_sub(&rq->load, p->se.load.weight);
1420}
1421
1422static void inc_nr_running(struct task_struct *p, struct rq *rq)
1423{
1424 rq->nr_running++;
1425 inc_load(rq, p);
1426}
1427
1428static void dec_nr_running(struct task_struct *p, struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001429{
1430 rq->nr_running--;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001431 dec_load(rq, p);
Ingo Molnar9c217242007-08-02 17:41:40 +02001432}
1433
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001434static void set_load_weight(struct task_struct *p)
1435{
1436 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001437 p->se.load.weight = prio_to_weight[0] * 2;
1438 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1439 return;
1440 }
1441
1442 /*
1443 * SCHED_IDLE tasks get minimal weight:
1444 */
1445 if (p->policy == SCHED_IDLE) {
1446 p->se.load.weight = WEIGHT_IDLEPRIO;
1447 p->se.load.inv_weight = WMULT_IDLEPRIO;
1448 return;
1449 }
1450
1451 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1452 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001453}
1454
Ingo Molnar8159f872007-08-09 11:16:49 +02001455static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001456{
1457 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001458 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001459 p->se.on_rq = 1;
1460}
1461
Ingo Molnar69be72c2007-08-09 11:16:49 +02001462static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001463{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001464 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001465 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001466}
1467
1468/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001469 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001470 */
Ingo Molnar14531182007-07-09 18:51:59 +02001471static inline int __normal_prio(struct task_struct *p)
1472{
Ingo Molnardd41f592007-07-09 18:51:59 +02001473 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001474}
1475
1476/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001477 * Calculate the expected normal priority: i.e. priority
1478 * without taking RT-inheritance into account. Might be
1479 * boosted by interactivity modifiers. Changes upon fork,
1480 * setprio syscalls, and whenever the interactivity
1481 * estimator recalculates.
1482 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001483static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001484{
1485 int prio;
1486
Ingo Molnare05606d2007-07-09 18:51:59 +02001487 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001488 prio = MAX_RT_PRIO-1 - p->rt_priority;
1489 else
1490 prio = __normal_prio(p);
1491 return prio;
1492}
1493
1494/*
1495 * Calculate the current priority, i.e. the priority
1496 * taken into account by the scheduler. This value might
1497 * be boosted by RT tasks, or might be boosted by
1498 * interactivity modifiers. Will be RT if the task got
1499 * RT-boosted. If not then it returns p->normal_prio.
1500 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001501static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001502{
1503 p->normal_prio = normal_prio(p);
1504 /*
1505 * If we are RT tasks or we were boosted to RT priority,
1506 * keep the priority unchanged. Otherwise, update priority
1507 * to the normal priority:
1508 */
1509 if (!rt_prio(p->prio))
1510 return p->normal_prio;
1511 return p->prio;
1512}
1513
1514/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001515 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001517static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001519 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001520 rq->nr_uninterruptible--;
1521
Ingo Molnar8159f872007-08-09 11:16:49 +02001522 enqueue_task(rq, p, wakeup);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001523 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 * deactivate_task - remove a task from the runqueue.
1528 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001529static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001531 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001532 rq->nr_uninterruptible++;
1533
Ingo Molnar69be72c2007-08-09 11:16:49 +02001534 dequeue_task(rq, p, sleep);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001535 dec_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538/**
1539 * task_curr - is this task currently executing on a CPU?
1540 * @p: the task in question.
1541 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001542inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 return cpu_curr(task_cpu(p)) == p;
1545}
1546
Peter Williams2dd73a42006-06-27 02:54:34 -07001547/* Used instead of source_load when we know the type == 0 */
1548unsigned long weighted_cpuload(const int cpu)
1549{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001550 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001551}
1552
1553static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1554{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001555 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001556#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001557 /*
1558 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1559 * successfuly executed on another CPU. We must ensure that updates of
1560 * per-task data have been completed by this moment.
1561 */
1562 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001563 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001564#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001565}
1566
Steven Rostedtcb469842008-01-25 21:08:22 +01001567static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1568 const struct sched_class *prev_class,
1569 int oldprio, int running)
1570{
1571 if (prev_class != p->sched_class) {
1572 if (prev_class->switched_from)
1573 prev_class->switched_from(rq, p, running);
1574 p->sched_class->switched_to(rq, p, running);
1575 } else
1576 p->sched_class->prio_changed(rq, p, oldprio, running);
1577}
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001580
Ingo Molnarcc367732007-10-15 17:00:18 +02001581/*
1582 * Is this task likely cache-hot:
1583 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001584static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001585task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1586{
1587 s64 delta;
1588
Ingo Molnarf540a602008-03-15 17:10:34 +01001589 /*
1590 * Buddy candidates are cache hot:
1591 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01001592 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01001593 return 1;
1594
Ingo Molnarcc367732007-10-15 17:00:18 +02001595 if (p->sched_class != &fair_sched_class)
1596 return 0;
1597
Ingo Molnar6bc16652007-10-15 17:00:18 +02001598 if (sysctl_sched_migration_cost == -1)
1599 return 1;
1600 if (sysctl_sched_migration_cost == 0)
1601 return 0;
1602
Ingo Molnarcc367732007-10-15 17:00:18 +02001603 delta = now - p->se.exec_start;
1604
1605 return delta < (s64)sysctl_sched_migration_cost;
1606}
1607
1608
Ingo Molnardd41f592007-07-09 18:51:59 +02001609void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001610{
Ingo Molnardd41f592007-07-09 18:51:59 +02001611 int old_cpu = task_cpu(p);
1612 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001613 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1614 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001615 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001616
1617 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001618
1619#ifdef CONFIG_SCHEDSTATS
1620 if (p->se.wait_start)
1621 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001622 if (p->se.sleep_start)
1623 p->se.sleep_start -= clock_offset;
1624 if (p->se.block_start)
1625 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001626 if (old_cpu != new_cpu) {
1627 schedstat_inc(p, se.nr_migrations);
1628 if (task_hot(p, old_rq->clock, NULL))
1629 schedstat_inc(p, se.nr_forced2_migrations);
1630 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001631#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001632 p->se.vruntime -= old_cfsrq->min_vruntime -
1633 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001634
1635 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001636}
1637
Ingo Molnar70b97a72006-07-03 00:25:42 -07001638struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Ingo Molnar36c8b582006-07-03 00:25:41 -07001641 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 int dest_cpu;
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001645};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647/*
1648 * The task's runqueue lock must be held.
1649 * Returns true if you have to wait for migration thread.
1650 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001651static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001652migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001654 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 /*
1657 * If the task is not on a runqueue (and not running), then
1658 * it is sufficient to simply update the task's cpu field.
1659 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001660 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 set_task_cpu(p, dest_cpu);
1662 return 0;
1663 }
1664
1665 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 req->task = p;
1667 req->dest_cpu = dest_cpu;
1668 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 1;
1671}
1672
1673/*
1674 * wait_task_inactive - wait for a thread to unschedule.
1675 *
1676 * The caller must ensure that the task *will* unschedule sometime soon,
1677 * else this function might spin for a *long* time. This function can't
1678 * be called with interrupts off, or it may introduce deadlock with
1679 * smp_call_function() if an IPI is sent by the same process we are
1680 * waiting to become inactive.
1681 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001682void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
1684 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001685 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001686 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Andi Kleen3a5c3592007-10-15 17:00:14 +02001688 for (;;) {
1689 /*
1690 * We do the initial early heuristics without holding
1691 * any task-queue locks at all. We'll only try to get
1692 * the runqueue lock when things look like they will
1693 * work out!
1694 */
1695 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001696
Andi Kleen3a5c3592007-10-15 17:00:14 +02001697 /*
1698 * If the task is actively running on another CPU
1699 * still, just relax and busy-wait without holding
1700 * any locks.
1701 *
1702 * NOTE! Since we don't hold any locks, it's not
1703 * even sure that "rq" stays as the right runqueue!
1704 * But we don't care, since "task_running()" will
1705 * return false if the runqueue has changed and p
1706 * is actually now running somewhere else!
1707 */
1708 while (task_running(rq, p))
1709 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001710
Andi Kleen3a5c3592007-10-15 17:00:14 +02001711 /*
1712 * Ok, time to look more closely! We need the rq
1713 * lock now, to be *sure*. If we're wrong, we'll
1714 * just go back and repeat.
1715 */
1716 rq = task_rq_lock(p, &flags);
1717 running = task_running(rq, p);
1718 on_rq = p->se.on_rq;
1719 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001720
Andi Kleen3a5c3592007-10-15 17:00:14 +02001721 /*
1722 * Was it really running after all now that we
1723 * checked with the proper locks actually held?
1724 *
1725 * Oops. Go back and try again..
1726 */
1727 if (unlikely(running)) {
1728 cpu_relax();
1729 continue;
1730 }
1731
1732 /*
1733 * It's not enough that it's not actively running,
1734 * it must be off the runqueue _entirely_, and not
1735 * preempted!
1736 *
1737 * So if it wa still runnable (but just not actively
1738 * running right now), it's preempted, and we should
1739 * yield - it could be a while.
1740 */
1741 if (unlikely(on_rq)) {
1742 schedule_timeout_uninterruptible(1);
1743 continue;
1744 }
1745
1746 /*
1747 * Ahh, all good. It wasn't running, and it wasn't
1748 * runnable, which means that it will never become
1749 * running in the future either. We're all done!
1750 */
1751 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
1755/***
1756 * kick_process - kick a running thread to enter/exit the kernel
1757 * @p: the to-be-kicked thread
1758 *
1759 * Cause a process which is running on another CPU to enter
1760 * kernel-mode, without any delay. (to get signals handled.)
1761 *
1762 * NOTE: this function doesnt have to take the runqueue lock,
1763 * because all it wants to ensure is that the remote task enters
1764 * the kernel. If the IPI races and the task has been migrated
1765 * to another CPU then no harm is done and the purpose has been
1766 * achieved as well.
1767 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001768void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 int cpu;
1771
1772 preempt_disable();
1773 cpu = task_cpu(p);
1774 if ((cpu != smp_processor_id()) && task_curr(p))
1775 smp_send_reschedule(cpu);
1776 preempt_enable();
1777}
1778
1779/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001780 * Return a low guess at the load of a migration-source cpu weighted
1781 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 *
1783 * We want to under-estimate the load of migration sources, to
1784 * balance conservatively.
1785 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001786static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001787{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001788 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001789 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001790
Peter Williams2dd73a42006-06-27 02:54:34 -07001791 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001792 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001793
Ingo Molnardd41f592007-07-09 18:51:59 +02001794 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
1797/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001798 * Return a high guess at the load of a migration-target cpu weighted
1799 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001801static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001802{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001803 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001804 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001805
Peter Williams2dd73a42006-06-27 02:54:34 -07001806 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001807 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001808
Ingo Molnardd41f592007-07-09 18:51:59 +02001809 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001810}
1811
1812/*
1813 * Return the average load per task on the cpu's run queue
1814 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001815static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07001816{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001817 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001818 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001819 unsigned long n = rq->nr_running;
1820
Ingo Molnardd41f592007-07-09 18:51:59 +02001821 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
Nick Piggin147cbb42005-06-25 14:57:19 -07001824/*
1825 * find_idlest_group finds and returns the least busy CPU group within the
1826 * domain.
1827 */
1828static struct sched_group *
1829find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1830{
1831 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1832 unsigned long min_load = ULONG_MAX, this_load = 0;
1833 int load_idx = sd->forkexec_idx;
1834 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1835
1836 do {
1837 unsigned long load, avg_load;
1838 int local_group;
1839 int i;
1840
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001841 /* Skip over this group if it has no CPUs allowed */
1842 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001843 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001844
Nick Piggin147cbb42005-06-25 14:57:19 -07001845 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001846
1847 /* Tally up the load of all CPUs in the group */
1848 avg_load = 0;
1849
1850 for_each_cpu_mask(i, group->cpumask) {
1851 /* Bias balancing toward cpus of our domain */
1852 if (local_group)
1853 load = source_load(i, load_idx);
1854 else
1855 load = target_load(i, load_idx);
1856
1857 avg_load += load;
1858 }
1859
1860 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001861 avg_load = sg_div_cpu_power(group,
1862 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001863
1864 if (local_group) {
1865 this_load = avg_load;
1866 this = group;
1867 } else if (avg_load < min_load) {
1868 min_load = avg_load;
1869 idlest = group;
1870 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02001871 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07001872
1873 if (!idlest || 100*this_load < imbalance*min_load)
1874 return NULL;
1875 return idlest;
1876}
1877
1878/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001879 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001880 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001881static int
1882find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07001883{
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001884 cpumask_t tmp;
Nick Piggin147cbb42005-06-25 14:57:19 -07001885 unsigned long load, min_load = ULONG_MAX;
1886 int idlest = -1;
1887 int i;
1888
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001889 /* Traverse only the allowed CPUs */
1890 cpus_and(tmp, group->cpumask, p->cpus_allowed);
1891
1892 for_each_cpu_mask(i, tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001893 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001894
1895 if (load < min_load || (load == min_load && i == this_cpu)) {
1896 min_load = load;
1897 idlest = i;
1898 }
1899 }
1900
1901 return idlest;
1902}
1903
Nick Piggin476d1392005-06-25 14:57:29 -07001904/*
1905 * sched_balance_self: balance the current task (running on cpu) in domains
1906 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1907 * SD_BALANCE_EXEC.
1908 *
1909 * Balance, ie. select the least loaded group.
1910 *
1911 * Returns the target CPU number, or the same CPU if no balancing is needed.
1912 *
1913 * preempt must be disabled.
1914 */
1915static int sched_balance_self(int cpu, int flag)
1916{
1917 struct task_struct *t = current;
1918 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07001919
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001920 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02001921 /*
1922 * If power savings logic is enabled for a domain, stop there.
1923 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07001924 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1925 break;
Nick Piggin476d1392005-06-25 14:57:29 -07001926 if (tmp->flags & flag)
1927 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001928 }
Nick Piggin476d1392005-06-25 14:57:29 -07001929
1930 while (sd) {
1931 cpumask_t span;
1932 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001933 int new_cpu, weight;
1934
1935 if (!(sd->flags & flag)) {
1936 sd = sd->child;
1937 continue;
1938 }
Nick Piggin476d1392005-06-25 14:57:29 -07001939
1940 span = sd->span;
1941 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001942 if (!group) {
1943 sd = sd->child;
1944 continue;
1945 }
Nick Piggin476d1392005-06-25 14:57:29 -07001946
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001947 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001948 if (new_cpu == -1 || new_cpu == cpu) {
1949 /* Now try balancing at a lower domain level of cpu */
1950 sd = sd->child;
1951 continue;
1952 }
Nick Piggin476d1392005-06-25 14:57:29 -07001953
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001954 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07001955 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07001956 sd = NULL;
1957 weight = cpus_weight(span);
1958 for_each_domain(cpu, tmp) {
1959 if (weight <= cpus_weight(tmp->span))
1960 break;
1961 if (tmp->flags & flag)
1962 sd = tmp;
1963 }
1964 /* while loop will break here if sd == NULL */
1965 }
1966
1967 return cpu;
1968}
1969
1970#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972/***
1973 * try_to_wake_up - wake up a thread
1974 * @p: the to-be-woken-up thread
1975 * @state: the mask of task states that can be woken
1976 * @sync: do a synchronous wakeup?
1977 *
1978 * Put it on the run-queue if it's not already there. The "current"
1979 * thread is always on the run-queue (except when the actual
1980 * re-schedule is in progress), and as such you're allowed to do
1981 * the simpler "current->state = TASK_RUNNING" to mark yourself
1982 * runnable without the overhead of this.
1983 *
1984 * returns failure only if the task is already active.
1985 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001986static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Ingo Molnarcc367732007-10-15 17:00:18 +02001988 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 unsigned long flags;
1990 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001991 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Ingo Molnarb85d0662008-03-16 20:03:22 +01001993 if (!sched_feat(SYNC_WAKEUPS))
1994 sync = 0;
1995
Linus Torvalds04e2f172008-02-23 18:05:03 -08001996 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 rq = task_rq_lock(p, &flags);
1998 old_state = p->state;
1999 if (!(old_state & state))
2000 goto out;
2001
Ingo Molnardd41f592007-07-09 18:51:59 +02002002 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 goto out_running;
2004
2005 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002006 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 this_cpu = smp_processor_id();
2008
2009#ifdef CONFIG_SMP
2010 if (unlikely(task_running(rq, p)))
2011 goto out_activate;
2012
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002013 cpu = p->sched_class->select_task_rq(p, sync);
2014 if (cpu != orig_cpu) {
2015 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 task_rq_unlock(rq, &flags);
2017 /* might preempt at this point */
2018 rq = task_rq_lock(p, &flags);
2019 old_state = p->state;
2020 if (!(old_state & state))
2021 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002022 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 goto out_running;
2024
2025 this_cpu = smp_processor_id();
2026 cpu = task_cpu(p);
2027 }
2028
Gregory Haskinse7693a32008-01-25 21:08:09 +01002029#ifdef CONFIG_SCHEDSTATS
2030 schedstat_inc(rq, ttwu_count);
2031 if (cpu == this_cpu)
2032 schedstat_inc(rq, ttwu_local);
2033 else {
2034 struct sched_domain *sd;
2035 for_each_domain(this_cpu, sd) {
2036 if (cpu_isset(cpu, sd->span)) {
2037 schedstat_inc(sd, ttwu_wake_remote);
2038 break;
2039 }
2040 }
2041 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002042#endif
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044out_activate:
2045#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002046 schedstat_inc(p, se.nr_wakeups);
2047 if (sync)
2048 schedstat_inc(p, se.nr_wakeups_sync);
2049 if (orig_cpu != cpu)
2050 schedstat_inc(p, se.nr_wakeups_migrate);
2051 if (cpu == this_cpu)
2052 schedstat_inc(p, se.nr_wakeups_local);
2053 else
2054 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002055 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002056 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 success = 1;
2058
2059out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002060 check_preempt_curr(rq, p);
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002063#ifdef CONFIG_SMP
2064 if (p->sched_class->task_wake_up)
2065 p->sched_class->task_wake_up(rq, p);
2066#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067out:
2068 task_rq_unlock(rq, &flags);
2069
2070 return success;
2071}
2072
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002073int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002075 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077EXPORT_SYMBOL(wake_up_process);
2078
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002079int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
2081 return try_to_wake_up(p, state, 0);
2082}
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084/*
2085 * Perform scheduler related setup for a newly forked process p.
2086 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002087 *
2088 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002090static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Ingo Molnardd41f592007-07-09 18:51:59 +02002092 p->se.exec_start = 0;
2093 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002094 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002095 p->se.last_wakeup = 0;
2096 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002097
2098#ifdef CONFIG_SCHEDSTATS
2099 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002100 p->se.sum_sleep_runtime = 0;
2101 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002102 p->se.block_start = 0;
2103 p->se.sleep_max = 0;
2104 p->se.block_max = 0;
2105 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002106 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002107 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002108#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002109
Peter Zijlstrafa717062008-01-25 21:08:27 +01002110 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002111 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07002112
Avi Kivitye107be32007-07-26 13:40:43 +02002113#ifdef CONFIG_PREEMPT_NOTIFIERS
2114 INIT_HLIST_HEAD(&p->preempt_notifiers);
2115#endif
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 /*
2118 * We mark the process as running here, but have not actually
2119 * inserted it onto the runqueue yet. This guarantees that
2120 * nobody will actually run it, and a signal or other external
2121 * event cannot wake it up and insert it on the runqueue either.
2122 */
2123 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002124}
2125
2126/*
2127 * fork()/clone()-time setup:
2128 */
2129void sched_fork(struct task_struct *p, int clone_flags)
2130{
2131 int cpu = get_cpu();
2132
2133 __sched_fork(p);
2134
2135#ifdef CONFIG_SMP
2136 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2137#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02002138 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002139
2140 /*
2141 * Make sure we do not leak PI boosting priority to the child:
2142 */
2143 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002144 if (!rt_prio(p->prio))
2145 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002146
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002147#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002148 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002149 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002151#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002152 p->oncpu = 0;
2153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002155 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002156 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002158 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
2161/*
2162 * wake_up_new_task - wake up a newly created task for the first time.
2163 *
2164 * This function will do some initial scheduler statistics housekeeping
2165 * that must be done for every newly created context, then puts the task
2166 * on the runqueue and wakes it.
2167 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002168void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002171 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002175 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 p->prio = effective_prio(p);
2178
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002179 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002180 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002183 * Let the scheduling class do new task startup
2184 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002186 p->sched_class->task_new(rq, p);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002187 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002189 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002190#ifdef CONFIG_SMP
2191 if (p->sched_class->task_wake_up)
2192 p->sched_class->task_wake_up(rq, p);
2193#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002194 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
Avi Kivitye107be32007-07-26 13:40:43 +02002197#ifdef CONFIG_PREEMPT_NOTIFIERS
2198
2199/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002200 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2201 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002202 */
2203void preempt_notifier_register(struct preempt_notifier *notifier)
2204{
2205 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2206}
2207EXPORT_SYMBOL_GPL(preempt_notifier_register);
2208
2209/**
2210 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002211 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002212 *
2213 * This is safe to call from within a preemption notifier.
2214 */
2215void preempt_notifier_unregister(struct preempt_notifier *notifier)
2216{
2217 hlist_del(&notifier->link);
2218}
2219EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2220
2221static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2222{
2223 struct preempt_notifier *notifier;
2224 struct hlist_node *node;
2225
2226 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2227 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2228}
2229
2230static void
2231fire_sched_out_preempt_notifiers(struct task_struct *curr,
2232 struct task_struct *next)
2233{
2234 struct preempt_notifier *notifier;
2235 struct hlist_node *node;
2236
2237 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2238 notifier->ops->sched_out(notifier, next);
2239}
2240
2241#else
2242
2243static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2244{
2245}
2246
2247static void
2248fire_sched_out_preempt_notifiers(struct task_struct *curr,
2249 struct task_struct *next)
2250{
2251}
2252
2253#endif
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002256 * prepare_task_switch - prepare to switch tasks
2257 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002258 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002259 * @next: the task we are going to switch to.
2260 *
2261 * This is called with the rq lock held and interrupts off. It must
2262 * be paired with a subsequent finish_task_switch after the context
2263 * switch.
2264 *
2265 * prepare_task_switch sets up locking and calls architecture specific
2266 * hooks.
2267 */
Avi Kivitye107be32007-07-26 13:40:43 +02002268static inline void
2269prepare_task_switch(struct rq *rq, struct task_struct *prev,
2270 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002271{
Avi Kivitye107be32007-07-26 13:40:43 +02002272 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002273 prepare_lock_switch(rq, next);
2274 prepare_arch_switch(next);
2275}
2276
2277/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002279 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 * @prev: the thread we just switched away from.
2281 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002282 * finish_task_switch must be called after the context switch, paired
2283 * with a prepare_task_switch call before the context switch.
2284 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2285 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 *
2287 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002288 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 * with the lock held can cause deadlocks; see schedule() for
2290 * details.)
2291 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002292static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 __releases(rq->lock)
2294{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002296 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 rq->prev_mm = NULL;
2299
2300 /*
2301 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002302 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002303 * schedule one last time. The schedule call will never return, and
2304 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002305 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 * still held, otherwise prev could be scheduled on another cpu, die
2307 * there before we look at prev->state, and then the reference would
2308 * be dropped twice.
2309 * Manfred Spraul <manfred@colorfullife.com>
2310 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002311 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002312 finish_arch_switch(prev);
2313 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002314#ifdef CONFIG_SMP
2315 if (current->sched_class->post_schedule)
2316 current->sched_class->post_schedule(rq);
2317#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002318
Avi Kivitye107be32007-07-26 13:40:43 +02002319 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (mm)
2321 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002322 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002323 /*
2324 * Remove function-return probe instances associated with this
2325 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002326 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002327 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
2332/**
2333 * schedule_tail - first thing a freshly forked thread must call.
2334 * @prev: the thread we just switched away from.
2335 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002336asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 __releases(rq->lock)
2338{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002339 struct rq *rq = this_rq();
2340
Nick Piggin4866cde2005-06-25 14:57:23 -07002341 finish_task_switch(rq, prev);
2342#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2343 /* In this case, finish_task_switch does not reenable preemption */
2344 preempt_enable();
2345#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002347 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
2350/*
2351 * context_switch - switch to the new MM and the new
2352 * thread's register state.
2353 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002354static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002355context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002356 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Ingo Molnardd41f592007-07-09 18:51:59 +02002358 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Avi Kivitye107be32007-07-26 13:40:43 +02002360 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002361 mm = next->mm;
2362 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002363 /*
2364 * For paravirt, this is coupled with an exit in switch_to to
2365 * combine the page table reload and the switch backend into
2366 * one hypercall.
2367 */
2368 arch_enter_lazy_cpu_mode();
2369
Ingo Molnardd41f592007-07-09 18:51:59 +02002370 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 next->active_mm = oldmm;
2372 atomic_inc(&oldmm->mm_count);
2373 enter_lazy_tlb(oldmm, next);
2374 } else
2375 switch_mm(oldmm, mm, next);
2376
Ingo Molnardd41f592007-07-09 18:51:59 +02002377 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 rq->prev_mm = oldmm;
2380 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002381 /*
2382 * Since the runqueue lock will be released by the next
2383 * task (which is an invalid locking op but in the case
2384 * of the scheduler it's an obvious special-case), so we
2385 * do an early lockdep release here:
2386 */
2387#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002388 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002389#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 /* Here we just switch the register state and the stack. */
2392 switch_to(prev, next, prev);
2393
Ingo Molnardd41f592007-07-09 18:51:59 +02002394 barrier();
2395 /*
2396 * this_rq must be evaluated again because prev may have moved
2397 * CPUs since it called schedule(), thus the 'rq' on its stack
2398 * frame will be invalid.
2399 */
2400 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
2402
2403/*
2404 * nr_running, nr_uninterruptible and nr_context_switches:
2405 *
2406 * externally visible scheduler statistics: current number of runnable
2407 * threads, current number of uninterruptible-sleeping threads, total
2408 * number of context switches performed since bootup.
2409 */
2410unsigned long nr_running(void)
2411{
2412 unsigned long i, sum = 0;
2413
2414 for_each_online_cpu(i)
2415 sum += cpu_rq(i)->nr_running;
2416
2417 return sum;
2418}
2419
2420unsigned long nr_uninterruptible(void)
2421{
2422 unsigned long i, sum = 0;
2423
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002424 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 sum += cpu_rq(i)->nr_uninterruptible;
2426
2427 /*
2428 * Since we read the counters lockless, it might be slightly
2429 * inaccurate. Do not allow it to go below zero though:
2430 */
2431 if (unlikely((long)sum < 0))
2432 sum = 0;
2433
2434 return sum;
2435}
2436
2437unsigned long long nr_context_switches(void)
2438{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002439 int i;
2440 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002442 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 sum += cpu_rq(i)->nr_switches;
2444
2445 return sum;
2446}
2447
2448unsigned long nr_iowait(void)
2449{
2450 unsigned long i, sum = 0;
2451
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002452 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2454
2455 return sum;
2456}
2457
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002458unsigned long nr_active(void)
2459{
2460 unsigned long i, running = 0, uninterruptible = 0;
2461
2462 for_each_online_cpu(i) {
2463 running += cpu_rq(i)->nr_running;
2464 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2465 }
2466
2467 if (unlikely((long)uninterruptible < 0))
2468 uninterruptible = 0;
2469
2470 return running + uninterruptible;
2471}
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002474 * Update rq->cpu_load[] statistics. This function is usually called every
2475 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002476 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002477static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002478{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002479 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002480 int i, scale;
2481
2482 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002483
2484 /* Update our load: */
2485 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2486 unsigned long old_load, new_load;
2487
2488 /* scale is effectively 1 << i now, and >> i divides by scale */
2489
2490 old_load = this_rq->cpu_load[i];
2491 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002492 /*
2493 * Round up the averaging division if load is increasing. This
2494 * prevents us from getting stuck on 9 if the load is 10, for
2495 * example.
2496 */
2497 if (new_load > old_load)
2498 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002499 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2500 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002501}
2502
Ingo Molnardd41f592007-07-09 18:51:59 +02002503#ifdef CONFIG_SMP
2504
Ingo Molnar48f24c42006-07-03 00:25:40 -07002505/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 * double_rq_lock - safely lock two runqueues
2507 *
2508 * Note this does not disable interrupts like task_rq_lock,
2509 * you need to do so manually before calling.
2510 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002511static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 __acquires(rq1->lock)
2513 __acquires(rq2->lock)
2514{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002515 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (rq1 == rq2) {
2517 spin_lock(&rq1->lock);
2518 __acquire(rq2->lock); /* Fake it out ;) */
2519 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002520 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 spin_lock(&rq1->lock);
2522 spin_lock(&rq2->lock);
2523 } else {
2524 spin_lock(&rq2->lock);
2525 spin_lock(&rq1->lock);
2526 }
2527 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002528 update_rq_clock(rq1);
2529 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
2532/*
2533 * double_rq_unlock - safely unlock two runqueues
2534 *
2535 * Note this does not restore interrupts like task_rq_unlock,
2536 * you need to do so manually after calling.
2537 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002538static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 __releases(rq1->lock)
2540 __releases(rq2->lock)
2541{
2542 spin_unlock(&rq1->lock);
2543 if (rq1 != rq2)
2544 spin_unlock(&rq2->lock);
2545 else
2546 __release(rq2->lock);
2547}
2548
2549/*
2550 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2551 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002552static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 __releases(this_rq->lock)
2554 __acquires(busiest->lock)
2555 __acquires(this_rq->lock)
2556{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002557 int ret = 0;
2558
Kirill Korotaev054b9102006-12-10 02:20:11 -08002559 if (unlikely(!irqs_disabled())) {
2560 /* printk() doesn't work good under rq->lock */
2561 spin_unlock(&this_rq->lock);
2562 BUG_ON(1);
2563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002565 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 spin_unlock(&this_rq->lock);
2567 spin_lock(&busiest->lock);
2568 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002569 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 } else
2571 spin_lock(&busiest->lock);
2572 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002573 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
2576/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 * If dest_cpu is allowed for this process, migrate the task to it.
2578 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002579 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 * the cpu_allowed mask is restored.
2581 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002582static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002584 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002586 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 rq = task_rq_lock(p, &flags);
2589 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2590 || unlikely(cpu_is_offline(dest_cpu)))
2591 goto out;
2592
2593 /* force the process onto the specified CPU */
2594 if (migrate_task(p, dest_cpu, &req)) {
2595 /* Need to wait for migration thread (might exit: take ref). */
2596 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 get_task_struct(mt);
2599 task_rq_unlock(rq, &flags);
2600 wake_up_process(mt);
2601 put_task_struct(mt);
2602 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return;
2605 }
2606out:
2607 task_rq_unlock(rq, &flags);
2608}
2609
2610/*
Nick Piggin476d1392005-06-25 14:57:29 -07002611 * sched_exec - execve() is a valuable balancing opportunity, because at
2612 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 */
2614void sched_exec(void)
2615{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002617 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002619 if (new_cpu != this_cpu)
2620 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
2623/*
2624 * pull_task - move a task from a remote runqueue to the local runqueue.
2625 * Both runqueues must be locked.
2626 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002627static void pull_task(struct rq *src_rq, struct task_struct *p,
2628 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002630 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002632 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 /*
2634 * Note that idle threads have a prio of MAX_PRIO, for this test
2635 * to be always true for them.
2636 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002637 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
2640/*
2641 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2642 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002643static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002644int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002645 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002646 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
2648 /*
2649 * We do not migrate tasks that are:
2650 * 1) running (obviously), or
2651 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2652 * 3) are cache-hot on their current CPU.
2653 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002654 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2655 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002657 }
Nick Piggin81026792005-06-25 14:57:07 -07002658 *all_pinned = 0;
2659
Ingo Molnarcc367732007-10-15 17:00:18 +02002660 if (task_running(rq, p)) {
2661 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002662 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Ingo Molnarda84d962007-10-15 17:00:18 +02002665 /*
2666 * Aggressive migration if:
2667 * 1) task is cache cold, or
2668 * 2) too many balance attempts have failed.
2669 */
2670
Ingo Molnar6bc16652007-10-15 17:00:18 +02002671 if (!task_hot(p, rq->clock, sd) ||
2672 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002673#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002674 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002675 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002676 schedstat_inc(p, se.nr_forced_migrations);
2677 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002678#endif
2679 return 1;
2680 }
2681
Ingo Molnarcc367732007-10-15 17:00:18 +02002682 if (task_hot(p, rq->clock, sd)) {
2683 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002684 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return 1;
2687}
2688
Peter Williamse1d14842007-10-24 18:23:51 +02002689static unsigned long
2690balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2691 unsigned long max_load_move, struct sched_domain *sd,
2692 enum cpu_idle_type idle, int *all_pinned,
2693 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002694{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002695 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02002696 struct task_struct *p;
2697 long rem_load_move = max_load_move;
2698
Peter Williamse1d14842007-10-24 18:23:51 +02002699 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002700 goto out;
2701
2702 pinned = 1;
2703
2704 /*
2705 * Start the load-balancing iterator:
2706 */
2707 p = iterator->start(iterator->arg);
2708next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002709 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002710 goto out;
2711 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002712 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02002713 * skip a task if it will be the highest priority task (i.e. smallest
2714 * prio value) on its new queue regardless of its load weight
2715 */
2716 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2717 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002718 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002719 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002720 p = iterator->next(iterator->arg);
2721 goto next;
2722 }
2723
2724 pull_task(busiest, p, this_rq, this_cpu);
2725 pulled++;
2726 rem_load_move -= p->se.load.weight;
2727
2728 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002729 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002730 */
Peter Williamse1d14842007-10-24 18:23:51 +02002731 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002732 if (p->prio < *this_best_prio)
2733 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002734 p = iterator->next(iterator->arg);
2735 goto next;
2736 }
2737out:
2738 /*
Peter Williamse1d14842007-10-24 18:23:51 +02002739 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02002740 * so we can safely collect pull_task() stats here rather than
2741 * inside pull_task().
2742 */
2743 schedstat_add(sd, lb_gained[idle], pulled);
2744
2745 if (all_pinned)
2746 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02002747
2748 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02002749}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751/*
Peter Williams43010652007-08-09 11:16:46 +02002752 * move_tasks tries to move up to max_load_move weighted load from busiest to
2753 * this_rq, as part of a balancing operation within domain "sd".
2754 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 *
2756 * Called with both runqueues locked.
2757 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002758static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002759 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002760 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002761 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002763 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002764 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002765 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Ingo Molnardd41f592007-07-09 18:51:59 +02002767 do {
Peter Williams43010652007-08-09 11:16:46 +02002768 total_load_moved +=
2769 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02002770 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002771 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002772 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002773 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Peter Williams43010652007-08-09 11:16:46 +02002775 return total_load_moved > 0;
2776}
2777
Peter Williamse1d14842007-10-24 18:23:51 +02002778static int
2779iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2780 struct sched_domain *sd, enum cpu_idle_type idle,
2781 struct rq_iterator *iterator)
2782{
2783 struct task_struct *p = iterator->start(iterator->arg);
2784 int pinned = 0;
2785
2786 while (p) {
2787 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
2788 pull_task(busiest, p, this_rq, this_cpu);
2789 /*
2790 * Right now, this is only the second place pull_task()
2791 * is called, so we can safely collect pull_task()
2792 * stats here rather than inside pull_task().
2793 */
2794 schedstat_inc(sd, lb_gained[idle]);
2795
2796 return 1;
2797 }
2798 p = iterator->next(iterator->arg);
2799 }
2800
2801 return 0;
2802}
2803
Peter Williams43010652007-08-09 11:16:46 +02002804/*
2805 * move_one_task tries to move exactly one task from busiest to this_rq, as
2806 * part of active balancing operations within "domain".
2807 * Returns 1 if successful and 0 otherwise.
2808 *
2809 * Called with both runqueues locked.
2810 */
2811static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2812 struct sched_domain *sd, enum cpu_idle_type idle)
2813{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002814 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02002815
2816 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02002817 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02002818 return 1;
2819
2820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822
2823/*
2824 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002825 * domain. It calculates and returns the amount of weighted load which
2826 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 */
2828static struct sched_group *
2829find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002830 unsigned long *imbalance, enum cpu_idle_type idle,
2831 int *sd_idle, cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
2833 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2834 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002835 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002836 unsigned long busiest_load_per_task, busiest_nr_running;
2837 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002838 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002839#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2840 int power_savings_balance = 1;
2841 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2842 unsigned long min_nr_running = ULONG_MAX;
2843 struct sched_group *group_min = NULL, *group_leader = NULL;
2844#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002847 busiest_load_per_task = busiest_nr_running = 0;
2848 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002849 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002850 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002851 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002852 load_idx = sd->newidle_idx;
2853 else
2854 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 do {
Ken Chen908a7c12007-10-17 16:55:11 +02002857 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 int local_group;
2859 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02002860 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002861 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002862 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 local_group = cpu_isset(this_cpu, group->cpumask);
2865
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002866 if (local_group)
2867 balance_cpu = first_cpu(group->cpumask);
2868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002870 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02002871 max_cpu_load = 0;
2872 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002875 struct rq *rq;
2876
2877 if (!cpu_isset(i, *cpus))
2878 continue;
2879
2880 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002881
Suresh Siddha9439aab2007-07-19 21:28:35 +02002882 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002883 *sd_idle = 0;
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002886 if (local_group) {
2887 if (idle_cpu(i) && !first_idle_cpu) {
2888 first_idle_cpu = 1;
2889 balance_cpu = i;
2890 }
2891
Nick Piggina2000572006-02-10 01:51:02 -08002892 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002893 } else {
Nick Piggina2000572006-02-10 01:51:02 -08002894 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002895 if (load > max_cpu_load)
2896 max_cpu_load = load;
2897 if (min_cpu_load > load)
2898 min_cpu_load = load;
2899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002902 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002903 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
2905
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002906 /*
2907 * First idle cpu or the first cpu(busiest) in this sched group
2908 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002909 * domains. In the newly idle case, we will allow all the cpu's
2910 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002911 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002912 if (idle != CPU_NEWLY_IDLE && local_group &&
2913 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002914 *balance = 0;
2915 goto ret;
2916 }
2917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002919 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
2921 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002922 avg_load = sg_div_cpu_power(group,
2923 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Ken Chen908a7c12007-10-17 16:55:11 +02002925 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
2926 __group_imb = 1;
2927
Eric Dumazet5517d862007-05-08 00:32:57 -07002928 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (local_group) {
2931 this_load = avg_load;
2932 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002933 this_nr_running = sum_nr_running;
2934 this_load_per_task = sum_weighted_load;
2935 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02002936 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 max_load = avg_load;
2938 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002939 busiest_nr_running = sum_nr_running;
2940 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02002941 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002943
2944#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2945 /*
2946 * Busy processors will not participate in power savings
2947 * balance.
2948 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002949 if (idle == CPU_NOT_IDLE ||
2950 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2951 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002952
2953 /*
2954 * If the local group is idle or completely loaded
2955 * no need to do power savings balance at this domain
2956 */
2957 if (local_group && (this_nr_running >= group_capacity ||
2958 !this_nr_running))
2959 power_savings_balance = 0;
2960
Ingo Molnardd41f592007-07-09 18:51:59 +02002961 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002962 * If a group is already running at full capacity or idle,
2963 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002964 */
2965 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002966 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002967 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002968
Ingo Molnardd41f592007-07-09 18:51:59 +02002969 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002970 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002971 * This is the group from where we need to pick up the load
2972 * for saving power
2973 */
2974 if ((sum_nr_running < min_nr_running) ||
2975 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002976 first_cpu(group->cpumask) <
2977 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002978 group_min = group;
2979 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002980 min_load_per_task = sum_weighted_load /
2981 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002982 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002983
Ingo Molnardd41f592007-07-09 18:51:59 +02002984 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002985 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002986 * capacity but still has some space to pick up some load
2987 * from other group and save more power
2988 */
2989 if (sum_nr_running <= group_capacity - 1) {
2990 if (sum_nr_running > leader_nr_running ||
2991 (sum_nr_running == leader_nr_running &&
2992 first_cpu(group->cpumask) >
2993 first_cpu(group_leader->cpumask))) {
2994 group_leader = group;
2995 leader_nr_running = sum_nr_running;
2996 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002997 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002998group_next:
2999#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 group = group->next;
3001 } while (group != sd->groups);
3002
Peter Williams2dd73a42006-06-27 02:54:34 -07003003 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 goto out_balanced;
3005
3006 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3007
3008 if (this_load >= avg_load ||
3009 100*max_load <= sd->imbalance_pct*this_load)
3010 goto out_balanced;
3011
Peter Williams2dd73a42006-06-27 02:54:34 -07003012 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003013 if (group_imb)
3014 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 /*
3017 * We're trying to get all the cpus to the average_load, so we don't
3018 * want to push ourselves above the average load, nor do we wish to
3019 * reduce the max loaded cpu below the average load, as either of these
3020 * actions would just result in more rebalancing later, and ping-pong
3021 * tasks around. Thus we look for the minimum possible imbalance.
3022 * Negative imbalances (*we* are more loaded than anyone else) will
3023 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003024 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 * appear as very large values with unsigned longs.
3026 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003027 if (max_load <= busiest_load_per_task)
3028 goto out_balanced;
3029
3030 /*
3031 * In the presence of smp nice balancing, certain scenarios can have
3032 * max load less than avg load(as we skip the groups at or below
3033 * its cpu_power, while calculating max_load..)
3034 */
3035 if (max_load < avg_load) {
3036 *imbalance = 0;
3037 goto small_imbalance;
3038 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003039
3040 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003041 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003042
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003044 *imbalance = min(max_pull * busiest->__cpu_power,
3045 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 / SCHED_LOAD_SCALE;
3047
Peter Williams2dd73a42006-06-27 02:54:34 -07003048 /*
3049 * if *imbalance is less than the average load per runnable task
3050 * there is no gaurantee that any tasks will be moved so we'll have
3051 * a think about bumping its value to force at least one task to be
3052 * moved
3053 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003054 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003055 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003056 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Peter Williams2dd73a42006-06-27 02:54:34 -07003058small_imbalance:
3059 pwr_move = pwr_now = 0;
3060 imbn = 2;
3061 if (this_nr_running) {
3062 this_load_per_task /= this_nr_running;
3063 if (busiest_load_per_task > this_load_per_task)
3064 imbn = 1;
3065 } else
3066 this_load_per_task = SCHED_LOAD_SCALE;
3067
Ingo Molnardd41f592007-07-09 18:51:59 +02003068 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3069 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003070 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 return busiest;
3072 }
3073
3074 /*
3075 * OK, we don't have enough imbalance to justify moving tasks,
3076 * however we may be able to increase total CPU power used by
3077 * moving them.
3078 */
3079
Eric Dumazet5517d862007-05-08 00:32:57 -07003080 pwr_now += busiest->__cpu_power *
3081 min(busiest_load_per_task, max_load);
3082 pwr_now += this->__cpu_power *
3083 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 pwr_now /= SCHED_LOAD_SCALE;
3085
3086 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003087 tmp = sg_div_cpu_power(busiest,
3088 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003090 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003091 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003094 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003095 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003096 tmp = sg_div_cpu_power(this,
3097 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003099 tmp = sg_div_cpu_power(this,
3100 busiest_load_per_task * SCHED_LOAD_SCALE);
3101 pwr_move += this->__cpu_power *
3102 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 pwr_move /= SCHED_LOAD_SCALE;
3104
3105 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003106 if (pwr_move > pwr_now)
3107 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 return busiest;
3111
3112out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003113#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003114 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003115 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003117 if (this == group_leader && group_leader != group_min) {
3118 *imbalance = min_load_per_task;
3119 return group_min;
3120 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003121#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003122ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 *imbalance = 0;
3124 return NULL;
3125}
3126
3127/*
3128 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3129 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003130static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003131find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003132 unsigned long imbalance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003134 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003135 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 int i;
3137
3138 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003139 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003140
3141 if (!cpu_isset(i, *cpus))
3142 continue;
3143
Ingo Molnar48f24c42006-07-03 00:25:40 -07003144 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003145 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Ingo Molnardd41f592007-07-09 18:51:59 +02003147 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003148 continue;
3149
Ingo Molnardd41f592007-07-09 18:51:59 +02003150 if (wl > max_load) {
3151 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003152 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 }
3154 }
3155
3156 return busiest;
3157}
3158
3159/*
Nick Piggin77391d72005-06-25 14:57:30 -07003160 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3161 * so long as it is large enough.
3162 */
3163#define MAX_PINNED_INTERVAL 512
3164
3165/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3167 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003169static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003170 struct sched_domain *sd, enum cpu_idle_type idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003171 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
Peter Williams43010652007-08-09 11:16:46 +02003173 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003176 struct rq *busiest;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003177 cpumask_t cpus = CPU_MASK_ALL;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003178 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003179
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003180 /*
3181 * When power savings policy is enabled for the parent domain, idle
3182 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003183 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003184 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003185 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003186 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003187 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003188 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Ingo Molnar2d723762007-10-15 17:00:12 +02003190 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003192redo:
3193 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003194 &cpus, balance);
3195
Chen, Kenneth W06066712006-12-10 02:20:35 -08003196 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003197 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if (!group) {
3200 schedstat_inc(sd, lb_nobusyg[idle]);
3201 goto out_balanced;
3202 }
3203
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003204 busiest = find_busiest_queue(group, idle, imbalance, &cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 if (!busiest) {
3206 schedstat_inc(sd, lb_nobusyq[idle]);
3207 goto out_balanced;
3208 }
3209
Nick Piggindb935db2005-06-25 14:57:11 -07003210 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
3212 schedstat_add(sd, lb_imbalance[idle], imbalance);
3213
Peter Williams43010652007-08-09 11:16:46 +02003214 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (busiest->nr_running > 1) {
3216 /*
3217 * Attempt to move tasks. If find_busiest_group has found
3218 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003219 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 * correctly treated as an imbalance.
3221 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003222 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003223 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003224 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003225 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003226 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003227 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003228
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003229 /*
3230 * some other cpu did the load balance for us.
3231 */
Peter Williams43010652007-08-09 11:16:46 +02003232 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003233 resched_cpu(this_cpu);
3234
Nick Piggin81026792005-06-25 14:57:07 -07003235 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003236 if (unlikely(all_pinned)) {
3237 cpu_clear(cpu_of(busiest), cpus);
3238 if (!cpus_empty(cpus))
3239 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003240 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
Nick Piggin81026792005-06-25 14:57:07 -07003243
Peter Williams43010652007-08-09 11:16:46 +02003244 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 schedstat_inc(sd, lb_failed[idle]);
3246 sd->nr_balance_failed++;
3247
3248 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003250 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003251
3252 /* don't kick the migration_thread, if the curr
3253 * task on busiest cpu can't be moved to this_cpu
3254 */
3255 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003256 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003257 all_pinned = 1;
3258 goto out_one_pinned;
3259 }
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 if (!busiest->active_balance) {
3262 busiest->active_balance = 1;
3263 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003264 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003266 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003267 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 wake_up_process(busiest->migration_thread);
3269
3270 /*
3271 * We've kicked active balancing, reset the failure
3272 * counter.
3273 */
Nick Piggin39507452005-06-25 14:57:09 -07003274 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
Nick Piggin81026792005-06-25 14:57:07 -07003276 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 sd->nr_balance_failed = 0;
3278
Nick Piggin81026792005-06-25 14:57:07 -07003279 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 /* We were unbalanced, so reset the balancing interval */
3281 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003282 } else {
3283 /*
3284 * If we've begun active balancing, start to back off. This
3285 * case may not be covered by the all_pinned logic if there
3286 * is only 1 task on the busy runqueue (because we don't call
3287 * move_tasks).
3288 */
3289 if (sd->balance_interval < sd->max_interval)
3290 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 }
3292
Peter Williams43010652007-08-09 11:16:46 +02003293 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003294 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003295 return -1;
Peter Williams43010652007-08-09 11:16:46 +02003296 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
3298out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 schedstat_inc(sd, lb_balanced[idle]);
3300
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003301 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003302
3303out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003305 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3306 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 sd->balance_interval *= 2;
3308
Ingo Molnar48f24c42006-07-03 00:25:40 -07003309 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003310 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003311 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 return 0;
3313}
3314
3315/*
3316 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3317 * tasks if there is an imbalance.
3318 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003319 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 * this_rq is locked.
3321 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003322static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07003323load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324{
3325 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003326 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003328 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003329 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003330 int all_pinned = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003331 cpumask_t cpus = CPU_MASK_ALL;
Nick Piggin5969fe02005-09-10 00:26:19 -07003332
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003333 /*
3334 * When power savings policy is enabled for the parent domain, idle
3335 * sibling can pick up load irrespective of busy siblings. In this case,
3336 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003337 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003338 */
3339 if (sd->flags & SD_SHARE_CPUPOWER &&
3340 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003341 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
Ingo Molnar2d723762007-10-15 17:00:12 +02003343 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003344redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003345 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003346 &sd_idle, &cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003348 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003349 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 }
3351
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003352 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003353 &cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003354 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003355 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003356 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 }
3358
Nick Piggindb935db2005-06-25 14:57:11 -07003359 BUG_ON(busiest == this_rq);
3360
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003361 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003362
Peter Williams43010652007-08-09 11:16:46 +02003363 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003364 if (busiest->nr_running > 1) {
3365 /* Attempt to move tasks */
3366 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003367 /* this_rq->clock is already updated */
3368 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003369 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003370 imbalance, sd, CPU_NEWLY_IDLE,
3371 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003372 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003373
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003374 if (unlikely(all_pinned)) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003375 cpu_clear(cpu_of(busiest), cpus);
3376 if (!cpus_empty(cpus))
3377 goto redo;
3378 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003379 }
3380
Peter Williams43010652007-08-09 11:16:46 +02003381 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003382 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003383 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3384 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003385 return -1;
3386 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003387 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Peter Williams43010652007-08-09 11:16:46 +02003389 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003390
3391out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003392 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003393 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003394 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003395 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003396 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003397
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399}
3400
3401/*
3402 * idle_balance is called by schedule() if this_cpu is about to become
3403 * idle. Attempts to pull tasks from other CPUs.
3404 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003405static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
3407 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003408 int pulled_task = -1;
3409 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
3411 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003412 unsigned long interval;
3413
3414 if (!(sd->flags & SD_LOAD_BALANCE))
3415 continue;
3416
3417 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003418 /* If we've pulled tasks over stop searching: */
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003419 pulled_task = load_balance_newidle(this_cpu,
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003420 this_rq, sd);
3421
3422 interval = msecs_to_jiffies(sd->balance_interval);
3423 if (time_after(next_balance, sd->last_balance + interval))
3424 next_balance = sd->last_balance + interval;
3425 if (pulled_task)
3426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003428 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003429 /*
3430 * We are going idle. next_balance may be set based on
3431 * a busy processor. So reset next_balance.
3432 */
3433 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
3437/*
3438 * active_load_balance is run by migration threads. It pushes running tasks
3439 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3440 * running on each physical CPU where possible, and avoids physical /
3441 * logical imbalances.
3442 *
3443 * Called with busiest_rq locked.
3444 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003445static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
Nick Piggin39507452005-06-25 14:57:09 -07003447 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003448 struct sched_domain *sd;
3449 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003450
Ingo Molnar48f24c42006-07-03 00:25:40 -07003451 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003452 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003453 return;
3454
3455 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 /*
Nick Piggin39507452005-06-25 14:57:09 -07003458 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003459 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003460 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 */
Nick Piggin39507452005-06-25 14:57:09 -07003462 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Nick Piggin39507452005-06-25 14:57:09 -07003464 /* move a task from busiest_rq to target_rq */
3465 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003466 update_rq_clock(busiest_rq);
3467 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Nick Piggin39507452005-06-25 14:57:09 -07003469 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003470 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003471 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003472 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003473 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Ingo Molnar48f24c42006-07-03 00:25:40 -07003476 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003477 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Peter Williams43010652007-08-09 11:16:46 +02003479 if (move_one_task(target_rq, target_cpu, busiest_rq,
3480 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003481 schedstat_inc(sd, alb_pushed);
3482 else
3483 schedstat_inc(sd, alb_failed);
3484 }
Nick Piggin39507452005-06-25 14:57:09 -07003485 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003488#ifdef CONFIG_NO_HZ
3489static struct {
3490 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003491 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003492} nohz ____cacheline_aligned = {
3493 .load_balancer = ATOMIC_INIT(-1),
3494 .cpu_mask = CPU_MASK_NONE,
3495};
3496
Christoph Lameter7835b982006-12-10 02:20:22 -08003497/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003498 * This routine will try to nominate the ilb (idle load balancing)
3499 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3500 * load balancing on behalf of all those cpus. If all the cpus in the system
3501 * go into this tickless mode, then there will be no ilb owner (as there is
3502 * no need for one) and all the cpus will sleep till the next wakeup event
3503 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003504 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003505 * For the ilb owner, tick is not stopped. And this tick will be used
3506 * for idle load balancing. ilb owner will still be part of
3507 * nohz.cpu_mask..
3508 *
3509 * While stopping the tick, this cpu will become the ilb owner if there
3510 * is no other owner. And will be the owner till that cpu becomes busy
3511 * or if all cpus in the system stop their ticks at which point
3512 * there is no need for ilb owner.
3513 *
3514 * When the ilb owner becomes busy, it nominates another owner, during the
3515 * next busy scheduler_tick()
3516 */
3517int select_nohz_load_balancer(int stop_tick)
3518{
3519 int cpu = smp_processor_id();
3520
3521 if (stop_tick) {
3522 cpu_set(cpu, nohz.cpu_mask);
3523 cpu_rq(cpu)->in_nohz_recently = 1;
3524
3525 /*
3526 * If we are going offline and still the leader, give up!
3527 */
3528 if (cpu_is_offline(cpu) &&
3529 atomic_read(&nohz.load_balancer) == cpu) {
3530 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3531 BUG();
3532 return 0;
3533 }
3534
3535 /* time for ilb owner also to sleep */
3536 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3537 if (atomic_read(&nohz.load_balancer) == cpu)
3538 atomic_set(&nohz.load_balancer, -1);
3539 return 0;
3540 }
3541
3542 if (atomic_read(&nohz.load_balancer) == -1) {
3543 /* make me the ilb owner */
3544 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3545 return 1;
3546 } else if (atomic_read(&nohz.load_balancer) == cpu)
3547 return 1;
3548 } else {
3549 if (!cpu_isset(cpu, nohz.cpu_mask))
3550 return 0;
3551
3552 cpu_clear(cpu, nohz.cpu_mask);
3553
3554 if (atomic_read(&nohz.load_balancer) == cpu)
3555 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3556 BUG();
3557 }
3558 return 0;
3559}
3560#endif
3561
3562static DEFINE_SPINLOCK(balancing);
3563
3564/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003565 * It checks each scheduling domain to see if it is due to be balanced,
3566 * and initiates a balancing operation if so.
3567 *
3568 * Balancing parameters are set up in arch_init_sched_domains.
3569 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003570static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003571{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003572 int balance = 1;
3573 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003574 unsigned long interval;
3575 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003576 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003577 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003578 int update_next_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003580 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 if (!(sd->flags & SD_LOAD_BALANCE))
3582 continue;
3583
3584 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003585 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 interval *= sd->busy_factor;
3587
3588 /* scale ms to jiffies */
3589 interval = msecs_to_jiffies(interval);
3590 if (unlikely(!interval))
3591 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003592 if (interval > HZ*NR_CPUS/10)
3593 interval = HZ*NR_CPUS/10;
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Christoph Lameter08c183f2006-12-10 02:20:29 -08003596 if (sd->flags & SD_SERIALIZE) {
3597 if (!spin_trylock(&balancing))
3598 goto out;
3599 }
3600
Christoph Lameterc9819f42006-12-10 02:20:25 -08003601 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003602 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003603 /*
3604 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003605 * longer idle, or one of our SMT siblings is
3606 * not idle.
3607 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003608 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003610 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003612 if (sd->flags & SD_SERIALIZE)
3613 spin_unlock(&balancing);
3614out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003615 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003616 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003617 update_next_balance = 1;
3618 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003619
3620 /*
3621 * Stop the load balance at this level. There is another
3622 * CPU in our sched group which is doing load balancing more
3623 * actively.
3624 */
3625 if (!balance)
3626 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003628
3629 /*
3630 * next_balance will be updated only when there is a need.
3631 * When the cpu is attached to null domain for ex, it will not be
3632 * updated.
3633 */
3634 if (likely(update_next_balance))
3635 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003636}
3637
3638/*
3639 * run_rebalance_domains is triggered when needed from the scheduler tick.
3640 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3641 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3642 */
3643static void run_rebalance_domains(struct softirq_action *h)
3644{
Ingo Molnardd41f592007-07-09 18:51:59 +02003645 int this_cpu = smp_processor_id();
3646 struct rq *this_rq = cpu_rq(this_cpu);
3647 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3648 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003649
Ingo Molnardd41f592007-07-09 18:51:59 +02003650 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003651
3652#ifdef CONFIG_NO_HZ
3653 /*
3654 * If this cpu is the owner for idle load balancing, then do the
3655 * balancing on behalf of the other idle cpus whose ticks are
3656 * stopped.
3657 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003658 if (this_rq->idle_at_tick &&
3659 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003660 cpumask_t cpus = nohz.cpu_mask;
3661 struct rq *rq;
3662 int balance_cpu;
3663
Ingo Molnardd41f592007-07-09 18:51:59 +02003664 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003665 for_each_cpu_mask(balance_cpu, cpus) {
3666 /*
3667 * If this cpu gets work to do, stop the load balancing
3668 * work being done for other cpus. Next load
3669 * balancing owner will pick it up.
3670 */
3671 if (need_resched())
3672 break;
3673
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003674 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003675
3676 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003677 if (time_after(this_rq->next_balance, rq->next_balance))
3678 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003679 }
3680 }
3681#endif
3682}
3683
3684/*
3685 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3686 *
3687 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3688 * idle load balancing owner or decide to stop the periodic load balancing,
3689 * if the whole system is idle.
3690 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003691static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003692{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003693#ifdef CONFIG_NO_HZ
3694 /*
3695 * If we were in the nohz mode recently and busy at the current
3696 * scheduler tick, then check if we need to nominate new idle
3697 * load balancer.
3698 */
3699 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3700 rq->in_nohz_recently = 0;
3701
3702 if (atomic_read(&nohz.load_balancer) == cpu) {
3703 cpu_clear(cpu, nohz.cpu_mask);
3704 atomic_set(&nohz.load_balancer, -1);
3705 }
3706
3707 if (atomic_read(&nohz.load_balancer) == -1) {
3708 /*
3709 * simple selection for now: Nominate the
3710 * first cpu in the nohz list to be the next
3711 * ilb owner.
3712 *
3713 * TBD: Traverse the sched domains and nominate
3714 * the nearest cpu in the nohz.cpu_mask.
3715 */
3716 int ilb = first_cpu(nohz.cpu_mask);
3717
3718 if (ilb != NR_CPUS)
3719 resched_cpu(ilb);
3720 }
3721 }
3722
3723 /*
3724 * If this cpu is idle and doing idle load balancing for all the
3725 * cpus with ticks stopped, is it time for that to stop?
3726 */
3727 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3728 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3729 resched_cpu(cpu);
3730 return;
3731 }
3732
3733 /*
3734 * If this cpu is idle and the idle load balancing is done by
3735 * someone else, then no need raise the SCHED_SOFTIRQ
3736 */
3737 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3738 cpu_isset(cpu, nohz.cpu_mask))
3739 return;
3740#endif
3741 if (time_after_eq(jiffies, rq->next_balance))
3742 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743}
Ingo Molnardd41f592007-07-09 18:51:59 +02003744
3745#else /* CONFIG_SMP */
3746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747/*
3748 * on UP we do not need to balance between CPUs:
3749 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003750static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751{
3752}
Ingo Molnardd41f592007-07-09 18:51:59 +02003753
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754#endif
3755
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756DEFINE_PER_CPU(struct kernel_stat, kstat);
3757
3758EXPORT_PER_CPU_SYMBOL(kstat);
3759
3760/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003761 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3762 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003764unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003767 u64 ns, delta_exec;
3768 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003769
Ingo Molnar41b86e92007-07-09 18:51:58 +02003770 rq = task_rq_lock(p, &flags);
3771 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003772 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003773 update_rq_clock(rq);
3774 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003775 if ((s64)delta_exec > 0)
3776 ns += delta_exec;
3777 }
3778 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003779
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return ns;
3781}
3782
3783/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 * Account user cpu time to a process.
3785 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 * @cputime: the cpu time spent in user space since the last update
3787 */
3788void account_user_time(struct task_struct *p, cputime_t cputime)
3789{
3790 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3791 cputime64_t tmp;
3792
3793 p->utime = cputime_add(p->utime, cputime);
3794
3795 /* Add user time to cpustat. */
3796 tmp = cputime_to_cputime64(cputime);
3797 if (TASK_NICE(p) > 0)
3798 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3799 else
3800 cpustat->user = cputime64_add(cpustat->user, tmp);
3801}
3802
3803/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003804 * Account guest cpu time to a process.
3805 * @p: the process that the cpu time gets accounted to
3806 * @cputime: the cpu time spent in virtual machine since the last update
3807 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01003808static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02003809{
3810 cputime64_t tmp;
3811 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3812
3813 tmp = cputime_to_cputime64(cputime);
3814
3815 p->utime = cputime_add(p->utime, cputime);
3816 p->gtime = cputime_add(p->gtime, cputime);
3817
3818 cpustat->user = cputime64_add(cpustat->user, tmp);
3819 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3820}
3821
3822/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003823 * Account scaled user cpu time to a process.
3824 * @p: the process that the cpu time gets accounted to
3825 * @cputime: the cpu time spent in user space since the last update
3826 */
3827void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
3828{
3829 p->utimescaled = cputime_add(p->utimescaled, cputime);
3830}
3831
3832/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 * Account system cpu time to a process.
3834 * @p: the process that the cpu time gets accounted to
3835 * @hardirq_offset: the offset to subtract from hardirq_count()
3836 * @cputime: the cpu time spent in kernel space since the last update
3837 */
3838void account_system_time(struct task_struct *p, int hardirq_offset,
3839 cputime_t cputime)
3840{
3841 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003842 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 cputime64_t tmp;
3844
Christian Borntraeger97783852007-11-15 20:57:39 +01003845 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
3846 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003847
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 p->stime = cputime_add(p->stime, cputime);
3849
3850 /* Add system time to cpustat. */
3851 tmp = cputime_to_cputime64(cputime);
3852 if (hardirq_count() - hardirq_offset)
3853 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3854 else if (softirq_count())
3855 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003856 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003858 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3860 else
3861 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3862 /* Account for system time used */
3863 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864}
3865
3866/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003867 * Account scaled system cpu time to a process.
3868 * @p: the process that the cpu time gets accounted to
3869 * @hardirq_offset: the offset to subtract from hardirq_count()
3870 * @cputime: the cpu time spent in kernel space since the last update
3871 */
3872void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
3873{
3874 p->stimescaled = cputime_add(p->stimescaled, cputime);
3875}
3876
3877/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 * Account for involuntary wait time.
3879 * @p: the process from which the cpu time has been stolen
3880 * @steal: the cpu time spent in involuntary wait
3881 */
3882void account_steal_time(struct task_struct *p, cputime_t steal)
3883{
3884 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3885 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003886 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
3888 if (p == rq->idle) {
3889 p->stime = cputime_add(p->stime, steal);
3890 if (atomic_read(&rq->nr_iowait) > 0)
3891 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3892 else
3893 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003894 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3896}
3897
Christoph Lameter7835b982006-12-10 02:20:22 -08003898/*
3899 * This function gets called by the timer code, with HZ frequency.
3900 * We call it with interrupts disabled.
3901 *
3902 * It also gets called by the fork code, when changing the parent's
3903 * timeslices.
3904 */
3905void scheduler_tick(void)
3906{
Christoph Lameter7835b982006-12-10 02:20:22 -08003907 int cpu = smp_processor_id();
3908 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003909 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02003910 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08003911
Ingo Molnardd41f592007-07-09 18:51:59 +02003912 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02003913 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02003914 /*
3915 * Let rq->clock advance by at least TICK_NSEC:
3916 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003917 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02003918 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003919 rq->clock_underflows++;
3920 }
Ingo Molnar529c7722007-08-10 23:05:11 +02003921 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02003922 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003923 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003924 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02003925 spin_unlock(&rq->lock);
3926
Christoph Lametere418e1c2006-12-10 02:20:23 -08003927#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003928 rq->idle_at_tick = idle_cpu(cpu);
3929 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003930#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931}
3932
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3934
Srinivasa Ds43627582008-02-23 15:24:04 -08003935void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936{
3937 /*
3938 * Underflow?
3939 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003940 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3941 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 preempt_count() += val;
3943 /*
3944 * Spinlock count overflowing soon?
3945 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003946 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3947 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948}
3949EXPORT_SYMBOL(add_preempt_count);
3950
Srinivasa Ds43627582008-02-23 15:24:04 -08003951void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952{
3953 /*
3954 * Underflow?
3955 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003956 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3957 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 /*
3959 * Is the spinlock portion underflowing?
3960 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003961 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3962 !(preempt_count() & PREEMPT_MASK)))
3963 return;
3964
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 preempt_count() -= val;
3966}
3967EXPORT_SYMBOL(sub_preempt_count);
3968
3969#endif
3970
3971/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003972 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003974static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975{
Satyam Sharma838225b2007-10-24 18:23:50 +02003976 struct pt_regs *regs = get_irq_regs();
3977
3978 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3979 prev->comm, prev->pid, preempt_count());
3980
Ingo Molnardd41f592007-07-09 18:51:59 +02003981 debug_show_held_locks(prev);
3982 if (irqs_disabled())
3983 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02003984
3985 if (regs)
3986 show_regs(regs);
3987 else
3988 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02003989}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
Ingo Molnardd41f592007-07-09 18:51:59 +02003991/*
3992 * Various schedule()-time debugging checks and statistics:
3993 */
3994static inline void schedule_debug(struct task_struct *prev)
3995{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003997 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 * schedule() atomically, we ignore that path for now.
3999 * Otherwise, whine if we are scheduling when we should not be.
4000 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004001 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4002 __schedule_bug(prev);
4003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4005
Ingo Molnar2d723762007-10-15 17:00:12 +02004006 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004007#ifdef CONFIG_SCHEDSTATS
4008 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004009 schedstat_inc(this_rq(), bkl_count);
4010 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004011 }
4012#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004013}
4014
4015/*
4016 * Pick up the highest-prio task:
4017 */
4018static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004019pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004020{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004021 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004022 struct task_struct *p;
4023
4024 /*
4025 * Optimization: we know that if all tasks are in
4026 * the fair class we can call that function directly:
4027 */
4028 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004029 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004030 if (likely(p))
4031 return p;
4032 }
4033
4034 class = sched_class_highest;
4035 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004036 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004037 if (p)
4038 return p;
4039 /*
4040 * Will never be NULL as the idle class always
4041 * returns a non-NULL p:
4042 */
4043 class = class->next;
4044 }
4045}
4046
4047/*
4048 * schedule() is the main scheduler function.
4049 */
4050asmlinkage void __sched schedule(void)
4051{
4052 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004053 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004054 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004055 int cpu;
4056
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057need_resched:
4058 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004059 cpu = smp_processor_id();
4060 rq = cpu_rq(cpu);
4061 rcu_qsctr_inc(cpu);
4062 prev = rq->curr;
4063 switch_count = &prev->nivcsw;
4064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 release_kernel_lock(prev);
4066need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Ingo Molnardd41f592007-07-09 18:51:59 +02004068 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004070 hrtick_clear(rq);
4071
Ingo Molnar1e819952007-10-15 17:00:13 +02004072 /*
4073 * Do the rq-clock update outside the rq lock:
4074 */
4075 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004076 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004077 spin_lock(&rq->lock);
4078 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Ingo Molnardd41f592007-07-09 18:51:59 +02004080 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4081 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004082 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004083 prev->state = TASK_RUNNING;
4084 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004085 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004086 }
4087 switch_count = &prev->nvcsw;
4088 }
4089
Steven Rostedt9a897c52008-01-25 21:08:22 +01004090#ifdef CONFIG_SMP
4091 if (prev->sched_class->pre_schedule)
4092 prev->sched_class->pre_schedule(rq, prev);
4093#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004094
Ingo Molnardd41f592007-07-09 18:51:59 +02004095 if (unlikely(!rq->nr_running))
4096 idle_balance(cpu, rq);
4097
Ingo Molnar31ee5292007-08-09 11:16:49 +02004098 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004099 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
4101 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02004102
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 rq->nr_switches++;
4105 rq->curr = next;
4106 ++*switch_count;
4107
Ingo Molnardd41f592007-07-09 18:51:59 +02004108 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004109 /*
4110 * the context switch might have flipped the stack from under
4111 * us, hence refresh the local variables.
4112 */
4113 cpu = smp_processor_id();
4114 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 } else
4116 spin_unlock_irq(&rq->lock);
4117
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004118 hrtick_set(rq);
4119
4120 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004122
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 preempt_enable_no_resched();
4124 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4125 goto need_resched;
4126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127EXPORT_SYMBOL(schedule);
4128
4129#ifdef CONFIG_PREEMPT
4130/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004131 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004132 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 * occur there and call schedule directly.
4134 */
4135asmlinkage void __sched preempt_schedule(void)
4136{
4137 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 struct task_struct *task = current;
4139 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004140
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 /*
4142 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004143 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004145 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 return;
4147
Andi Kleen3a5c3592007-10-15 17:00:14 +02004148 do {
4149 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Andi Kleen3a5c3592007-10-15 17:00:14 +02004151 /*
4152 * We keep the big kernel semaphore locked, but we
4153 * clear ->lock_depth so that schedule() doesnt
4154 * auto-release the semaphore:
4155 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004156 saved_lock_depth = task->lock_depth;
4157 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004158 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004159 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004160 sub_preempt_count(PREEMPT_ACTIVE);
4161
4162 /*
4163 * Check again in case we missed a preemption opportunity
4164 * between schedule and now.
4165 */
4166 barrier();
4167 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169EXPORT_SYMBOL(preempt_schedule);
4170
4171/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004172 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 * off of irq context.
4174 * Note, that this is called and return with irqs disabled. This will
4175 * protect us against recursive calling from irq.
4176 */
4177asmlinkage void __sched preempt_schedule_irq(void)
4178{
4179 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 struct task_struct *task = current;
4181 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004182
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004183 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 BUG_ON(ti->preempt_count || !irqs_disabled());
4185
Andi Kleen3a5c3592007-10-15 17:00:14 +02004186 do {
4187 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Andi Kleen3a5c3592007-10-15 17:00:14 +02004189 /*
4190 * We keep the big kernel semaphore locked, but we
4191 * clear ->lock_depth so that schedule() doesnt
4192 * auto-release the semaphore:
4193 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004194 saved_lock_depth = task->lock_depth;
4195 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004196 local_irq_enable();
4197 schedule();
4198 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004199 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004200 sub_preempt_count(PREEMPT_ACTIVE);
4201
4202 /*
4203 * Check again in case we missed a preemption opportunity
4204 * between schedule and now.
4205 */
4206 barrier();
4207 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209
4210#endif /* CONFIG_PREEMPT */
4211
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004212int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4213 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004215 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217EXPORT_SYMBOL(default_wake_function);
4218
4219/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004220 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4221 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 * number) then we wake all the non-exclusive tasks and one exclusive task.
4223 *
4224 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004225 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4227 */
4228static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4229 int nr_exclusive, int sync, void *key)
4230{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004231 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004233 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004234 unsigned flags = curr->flags;
4235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004237 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 break;
4239 }
4240}
4241
4242/**
4243 * __wake_up - wake up threads blocked on a waitqueue.
4244 * @q: the waitqueue
4245 * @mode: which threads
4246 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004247 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004249void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004250 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251{
4252 unsigned long flags;
4253
4254 spin_lock_irqsave(&q->lock, flags);
4255 __wake_up_common(q, mode, nr_exclusive, 0, key);
4256 spin_unlock_irqrestore(&q->lock, flags);
4257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258EXPORT_SYMBOL(__wake_up);
4259
4260/*
4261 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4262 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004263void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264{
4265 __wake_up_common(q, mode, 1, 0, NULL);
4266}
4267
4268/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004269 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 * @q: the waitqueue
4271 * @mode: which threads
4272 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4273 *
4274 * The sync wakeup differs that the waker knows that it will schedule
4275 * away soon, so while the target thread will be woken up, it will not
4276 * be migrated to another CPU - ie. the two threads are 'synchronized'
4277 * with each other. This can prevent needless bouncing between CPUs.
4278 *
4279 * On UP it can prevent extra preemption.
4280 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004281void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004282__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
4284 unsigned long flags;
4285 int sync = 1;
4286
4287 if (unlikely(!q))
4288 return;
4289
4290 if (unlikely(!nr_exclusive))
4291 sync = 0;
4292
4293 spin_lock_irqsave(&q->lock, flags);
4294 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4295 spin_unlock_irqrestore(&q->lock, flags);
4296}
4297EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4298
Ingo Molnarb15136e2007-10-24 18:23:48 +02004299void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
4301 unsigned long flags;
4302
4303 spin_lock_irqsave(&x->wait.lock, flags);
4304 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004305 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 spin_unlock_irqrestore(&x->wait.lock, flags);
4307}
4308EXPORT_SYMBOL(complete);
4309
Ingo Molnarb15136e2007-10-24 18:23:48 +02004310void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311{
4312 unsigned long flags;
4313
4314 spin_lock_irqsave(&x->wait.lock, flags);
4315 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004316 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 spin_unlock_irqrestore(&x->wait.lock, flags);
4318}
4319EXPORT_SYMBOL(complete_all);
4320
Andi Kleen8cbbe862007-10-15 17:00:14 +02004321static inline long __sched
4322do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 if (!x->done) {
4325 DECLARE_WAITQUEUE(wait, current);
4326
4327 wait.flags |= WQ_FLAG_EXCLUSIVE;
4328 __add_wait_queue_tail(&x->wait, &wait);
4329 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004330 if ((state == TASK_INTERRUPTIBLE &&
4331 signal_pending(current)) ||
4332 (state == TASK_KILLABLE &&
4333 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004334 __remove_wait_queue(&x->wait, &wait);
4335 return -ERESTARTSYS;
4336 }
4337 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004339 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004341 if (!timeout) {
4342 __remove_wait_queue(&x->wait, &wait);
4343 return timeout;
4344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 } while (!x->done);
4346 __remove_wait_queue(&x->wait, &wait);
4347 }
4348 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004349 return timeout;
4350}
4351
4352static long __sched
4353wait_for_common(struct completion *x, long timeout, int state)
4354{
4355 might_sleep();
4356
4357 spin_lock_irq(&x->wait.lock);
4358 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004360 return timeout;
4361}
4362
Ingo Molnarb15136e2007-10-24 18:23:48 +02004363void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004364{
4365 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366}
4367EXPORT_SYMBOL(wait_for_completion);
4368
Ingo Molnarb15136e2007-10-24 18:23:48 +02004369unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4371{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004372 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373}
4374EXPORT_SYMBOL(wait_for_completion_timeout);
4375
Andi Kleen8cbbe862007-10-15 17:00:14 +02004376int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377{
Andi Kleen51e97992007-10-18 21:32:55 +02004378 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4379 if (t == -ERESTARTSYS)
4380 return t;
4381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382}
4383EXPORT_SYMBOL(wait_for_completion_interruptible);
4384
Ingo Molnarb15136e2007-10-24 18:23:48 +02004385unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386wait_for_completion_interruptible_timeout(struct completion *x,
4387 unsigned long timeout)
4388{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004389 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390}
4391EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4392
Matthew Wilcox009e5772007-12-06 12:29:54 -05004393int __sched wait_for_completion_killable(struct completion *x)
4394{
4395 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4396 if (t == -ERESTARTSYS)
4397 return t;
4398 return 0;
4399}
4400EXPORT_SYMBOL(wait_for_completion_killable);
4401
Andi Kleen8cbbe862007-10-15 17:00:14 +02004402static long __sched
4403sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004404{
4405 unsigned long flags;
4406 wait_queue_t wait;
4407
4408 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Andi Kleen8cbbe862007-10-15 17:00:14 +02004410 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
Andi Kleen8cbbe862007-10-15 17:00:14 +02004412 spin_lock_irqsave(&q->lock, flags);
4413 __add_wait_queue(q, &wait);
4414 spin_unlock(&q->lock);
4415 timeout = schedule_timeout(timeout);
4416 spin_lock_irq(&q->lock);
4417 __remove_wait_queue(q, &wait);
4418 spin_unlock_irqrestore(&q->lock, flags);
4419
4420 return timeout;
4421}
4422
4423void __sched interruptible_sleep_on(wait_queue_head_t *q)
4424{
4425 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427EXPORT_SYMBOL(interruptible_sleep_on);
4428
Ingo Molnar0fec1712007-07-09 18:52:01 +02004429long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004430interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004432 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4435
Ingo Molnar0fec1712007-07-09 18:52:01 +02004436void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004438 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440EXPORT_SYMBOL(sleep_on);
4441
Ingo Molnar0fec1712007-07-09 18:52:01 +02004442long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004444 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446EXPORT_SYMBOL(sleep_on_timeout);
4447
Ingo Molnarb29739f2006-06-27 02:54:51 -07004448#ifdef CONFIG_RT_MUTEXES
4449
4450/*
4451 * rt_mutex_setprio - set the current priority of a task
4452 * @p: task
4453 * @prio: prio value (kernel-internal form)
4454 *
4455 * This function changes the 'effective' priority of a task. It does
4456 * not touch ->normal_prio like __setscheduler().
4457 *
4458 * Used by the rt_mutex code to implement priority inheritance logic.
4459 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004460void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004461{
4462 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004463 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004464 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004465 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004466
4467 BUG_ON(prio < 0 || prio > MAX_PRIO);
4468
4469 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004470 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004471
Andrew Mortond5f9f942007-05-08 20:27:06 -07004472 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004473 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004474 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004475 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004476 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004477 if (running)
4478 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004479
4480 if (rt_prio(prio))
4481 p->sched_class = &rt_sched_class;
4482 else
4483 p->sched_class = &fair_sched_class;
4484
Ingo Molnarb29739f2006-06-27 02:54:51 -07004485 p->prio = prio;
4486
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004487 if (running)
4488 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004489 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004490 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004491
4492 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004493 }
4494 task_rq_unlock(rq, &flags);
4495}
4496
4497#endif
4498
Ingo Molnar36c8b582006-07-03 00:25:41 -07004499void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
Ingo Molnardd41f592007-07-09 18:51:59 +02004501 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004503 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
4505 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4506 return;
4507 /*
4508 * We have to be careful, if called from sys_setpriority(),
4509 * the task might be in the middle of scheduling on another CPU.
4510 */
4511 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004512 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 /*
4514 * The RT priorities are set via sched_setscheduler(), but we still
4515 * allow the 'normal' nice value to be set - but as expected
4516 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004517 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004519 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 p->static_prio = NICE_TO_PRIO(nice);
4521 goto out_unlock;
4522 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004523 on_rq = p->se.on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004524 if (on_rq) {
Ingo Molnar69be72c2007-08-09 11:16:49 +02004525 dequeue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004526 dec_load(rq, p);
4527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004530 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004531 old_prio = p->prio;
4532 p->prio = effective_prio(p);
4533 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Ingo Molnardd41f592007-07-09 18:51:59 +02004535 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004536 enqueue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004537 inc_load(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004539 * If the task increased its priority or is running and
4540 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004542 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 resched_task(rq->curr);
4544 }
4545out_unlock:
4546 task_rq_unlock(rq, &flags);
4547}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548EXPORT_SYMBOL(set_user_nice);
4549
Matt Mackalle43379f2005-05-01 08:59:00 -07004550/*
4551 * can_nice - check if a task can reduce its nice value
4552 * @p: task
4553 * @nice: nice value
4554 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004555int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004556{
Matt Mackall024f4742005-08-18 11:24:19 -07004557 /* convert nice value [19,-20] to rlimit style value [1,40] */
4558 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004559
Matt Mackalle43379f2005-05-01 08:59:00 -07004560 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4561 capable(CAP_SYS_NICE));
4562}
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564#ifdef __ARCH_WANT_SYS_NICE
4565
4566/*
4567 * sys_nice - change the priority of the current process.
4568 * @increment: priority increment
4569 *
4570 * sys_setpriority is a more generic, but much slower function that
4571 * does similar things.
4572 */
4573asmlinkage long sys_nice(int increment)
4574{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004575 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
4577 /*
4578 * Setpriority might change our priority at the same moment.
4579 * We don't have to worry. Conceptually one call occurs first
4580 * and we have a single winner.
4581 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004582 if (increment < -40)
4583 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 if (increment > 40)
4585 increment = 40;
4586
4587 nice = PRIO_TO_NICE(current->static_prio) + increment;
4588 if (nice < -20)
4589 nice = -20;
4590 if (nice > 19)
4591 nice = 19;
4592
Matt Mackalle43379f2005-05-01 08:59:00 -07004593 if (increment < 0 && !can_nice(current, nice))
4594 return -EPERM;
4595
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 retval = security_task_setnice(current, nice);
4597 if (retval)
4598 return retval;
4599
4600 set_user_nice(current, nice);
4601 return 0;
4602}
4603
4604#endif
4605
4606/**
4607 * task_prio - return the priority value of a given task.
4608 * @p: the task in question.
4609 *
4610 * This is the priority value as seen by users in /proc.
4611 * RT tasks are offset by -200. Normal tasks are centered
4612 * around 0, value goes from -16 to +15.
4613 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004614int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615{
4616 return p->prio - MAX_RT_PRIO;
4617}
4618
4619/**
4620 * task_nice - return the nice value of a given task.
4621 * @p: the task in question.
4622 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004623int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
4625 return TASK_NICE(p);
4626}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004627EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
4629/**
4630 * idle_cpu - is a given cpu idle currently?
4631 * @cpu: the processor in question.
4632 */
4633int idle_cpu(int cpu)
4634{
4635 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4636}
4637
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638/**
4639 * idle_task - return the idle task for a given cpu.
4640 * @cpu: the processor in question.
4641 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004642struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643{
4644 return cpu_rq(cpu)->idle;
4645}
4646
4647/**
4648 * find_process_by_pid - find a process with a matching PID value.
4649 * @pid: the pid in question.
4650 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004651static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004653 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654}
4655
4656/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004657static void
4658__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659{
Ingo Molnardd41f592007-07-09 18:51:59 +02004660 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004661
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004663 switch (p->policy) {
4664 case SCHED_NORMAL:
4665 case SCHED_BATCH:
4666 case SCHED_IDLE:
4667 p->sched_class = &fair_sched_class;
4668 break;
4669 case SCHED_FIFO:
4670 case SCHED_RR:
4671 p->sched_class = &rt_sched_class;
4672 break;
4673 }
4674
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004676 p->normal_prio = normal_prio(p);
4677 /* we are holding p->pi_lock already */
4678 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004679 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680}
4681
4682/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004683 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 * @p: the task in question.
4685 * @policy: new policy.
4686 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004687 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004688 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004690int sched_setscheduler(struct task_struct *p, int policy,
4691 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004693 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01004695 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004696 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Steven Rostedt66e53932006-06-27 02:54:44 -07004698 /* may grab non-irq protected spin_locks */
4699 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700recheck:
4701 /* double check policy once rq lock held */
4702 if (policy < 0)
4703 policy = oldpolicy = p->policy;
4704 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004705 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4706 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004707 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 /*
4709 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004710 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4711 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 */
4713 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004714 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004715 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004717 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 return -EINVAL;
4719
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004720 /*
4721 * Allow unprivileged RT tasks to decrease priority:
4722 */
4723 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004724 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004725 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004726
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004727 if (!lock_task_sighand(p, &flags))
4728 return -ESRCH;
4729 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4730 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004731
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004732 /* can't set/change the rt policy */
4733 if (policy != p->policy && !rlim_rtprio)
4734 return -EPERM;
4735
4736 /* can't increase priority */
4737 if (param->sched_priority > p->rt_priority &&
4738 param->sched_priority > rlim_rtprio)
4739 return -EPERM;
4740 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004741 /*
4742 * Like positive nice levels, dont allow tasks to
4743 * move out of SCHED_IDLE either:
4744 */
4745 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4746 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004747
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004748 /* can't change other user's priorities */
4749 if ((current->euid != p->euid) &&
4750 (current->euid != p->uid))
4751 return -EPERM;
4752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004754#ifdef CONFIG_RT_GROUP_SCHED
4755 /*
4756 * Do not allow realtime tasks into groups that have no runtime
4757 * assigned.
4758 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02004759 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004760 return -EPERM;
4761#endif
4762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 retval = security_task_setscheduler(p, policy, param);
4764 if (retval)
4765 return retval;
4766 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004767 * make sure no PI-waiters arrive (or leave) while we are
4768 * changing the priority of the task:
4769 */
4770 spin_lock_irqsave(&p->pi_lock, flags);
4771 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 * To be able to change p->policy safely, the apropriate
4773 * runqueue lock must be held.
4774 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004775 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 /* recheck policy now with rq lock held */
4777 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4778 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004779 __task_rq_unlock(rq);
4780 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 goto recheck;
4782 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004783 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004784 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004785 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004786 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004787 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004788 if (running)
4789 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004790
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004792 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004793
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004794 if (running)
4795 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004796 if (on_rq) {
4797 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004798
4799 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004801 __task_rq_unlock(rq);
4802 spin_unlock_irqrestore(&p->pi_lock, flags);
4803
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004804 rt_mutex_adjust_pi(p);
4805
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 return 0;
4807}
4808EXPORT_SYMBOL_GPL(sched_setscheduler);
4809
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004810static int
4811do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 struct sched_param lparam;
4814 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004815 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
4817 if (!param || pid < 0)
4818 return -EINVAL;
4819 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4820 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004821
4822 rcu_read_lock();
4823 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004825 if (p != NULL)
4826 retval = sched_setscheduler(p, policy, &lparam);
4827 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 return retval;
4830}
4831
4832/**
4833 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4834 * @pid: the pid in question.
4835 * @policy: new policy.
4836 * @param: structure containing the new RT priority.
4837 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004838asmlinkage long
4839sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840{
Jason Baronc21761f2006-01-18 17:43:03 -08004841 /* negative values for policy are not valid */
4842 if (policy < 0)
4843 return -EINVAL;
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 return do_sched_setscheduler(pid, policy, param);
4846}
4847
4848/**
4849 * sys_sched_setparam - set/change the RT priority of a thread
4850 * @pid: the pid in question.
4851 * @param: structure containing the new RT priority.
4852 */
4853asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4854{
4855 return do_sched_setscheduler(pid, -1, param);
4856}
4857
4858/**
4859 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4860 * @pid: the pid in question.
4861 */
4862asmlinkage long sys_sched_getscheduler(pid_t pid)
4863{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004864 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004865 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
4867 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004868 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
4870 retval = -ESRCH;
4871 read_lock(&tasklist_lock);
4872 p = find_process_by_pid(pid);
4873 if (p) {
4874 retval = security_task_getscheduler(p);
4875 if (!retval)
4876 retval = p->policy;
4877 }
4878 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 return retval;
4880}
4881
4882/**
4883 * sys_sched_getscheduler - get the RT priority of a thread
4884 * @pid: the pid in question.
4885 * @param: structure containing the RT priority.
4886 */
4887asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4888{
4889 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004890 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004891 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
4893 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004894 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
4896 read_lock(&tasklist_lock);
4897 p = find_process_by_pid(pid);
4898 retval = -ESRCH;
4899 if (!p)
4900 goto out_unlock;
4901
4902 retval = security_task_getscheduler(p);
4903 if (retval)
4904 goto out_unlock;
4905
4906 lp.sched_priority = p->rt_priority;
4907 read_unlock(&tasklist_lock);
4908
4909 /*
4910 * This one might sleep, we cannot do it with a spinlock held ...
4911 */
4912 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4913
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 return retval;
4915
4916out_unlock:
4917 read_unlock(&tasklist_lock);
4918 return retval;
4919}
4920
4921long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4922{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 cpumask_t cpus_allowed;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004924 struct task_struct *p;
4925 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004927 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 read_lock(&tasklist_lock);
4929
4930 p = find_process_by_pid(pid);
4931 if (!p) {
4932 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004933 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 return -ESRCH;
4935 }
4936
4937 /*
4938 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004939 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 * usage count and then drop tasklist_lock.
4941 */
4942 get_task_struct(p);
4943 read_unlock(&tasklist_lock);
4944
4945 retval = -EPERM;
4946 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4947 !capable(CAP_SYS_NICE))
4948 goto out_unlock;
4949
David Quigleye7834f82006-06-23 02:03:59 -07004950 retval = security_task_setscheduler(p, 0, NULL);
4951 if (retval)
4952 goto out_unlock;
4953
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 cpus_allowed = cpuset_cpus_allowed(p);
4955 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004956 again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 retval = set_cpus_allowed(p, new_mask);
4958
Paul Menage8707d8b2007-10-18 23:40:22 -07004959 if (!retval) {
4960 cpus_allowed = cpuset_cpus_allowed(p);
4961 if (!cpus_subset(new_mask, cpus_allowed)) {
4962 /*
4963 * We must have raced with a concurrent cpuset
4964 * update. Just reset the cpus_allowed to the
4965 * cpuset's cpus_allowed
4966 */
4967 new_mask = cpus_allowed;
4968 goto again;
4969 }
4970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971out_unlock:
4972 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004973 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 return retval;
4975}
4976
4977static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4978 cpumask_t *new_mask)
4979{
4980 if (len < sizeof(cpumask_t)) {
4981 memset(new_mask, 0, sizeof(cpumask_t));
4982 } else if (len > sizeof(cpumask_t)) {
4983 len = sizeof(cpumask_t);
4984 }
4985 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4986}
4987
4988/**
4989 * sys_sched_setaffinity - set the cpu affinity of a process
4990 * @pid: pid of the process
4991 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4992 * @user_mask_ptr: user-space pointer to the new cpu mask
4993 */
4994asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4995 unsigned long __user *user_mask_ptr)
4996{
4997 cpumask_t new_mask;
4998 int retval;
4999
5000 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5001 if (retval)
5002 return retval;
5003
5004 return sched_setaffinity(pid, new_mask);
5005}
5006
5007/*
5008 * Represents all cpu's present in the system
5009 * In systems capable of hotplug, this map could dynamically grow
5010 * as new cpu's are detected in the system via any platform specific
5011 * method, such as ACPI for e.g.
5012 */
5013
Andi Kleen4cef0c62006-01-11 22:44:57 +01005014cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015EXPORT_SYMBOL(cpu_present_map);
5016
5017#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005018cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005019EXPORT_SYMBOL(cpu_online_map);
5020
Andi Kleen4cef0c62006-01-11 22:44:57 +01005021cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005022EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023#endif
5024
5025long sched_getaffinity(pid_t pid, cpumask_t *mask)
5026{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005027 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005030 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 read_lock(&tasklist_lock);
5032
5033 retval = -ESRCH;
5034 p = find_process_by_pid(pid);
5035 if (!p)
5036 goto out_unlock;
5037
David Quigleye7834f82006-06-23 02:03:59 -07005038 retval = security_task_getscheduler(p);
5039 if (retval)
5040 goto out_unlock;
5041
Jack Steiner2f7016d2006-02-01 03:05:18 -08005042 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043
5044out_unlock:
5045 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005046 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
Ulrich Drepper9531b622007-08-09 11:16:46 +02005048 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049}
5050
5051/**
5052 * sys_sched_getaffinity - get the cpu affinity of a process
5053 * @pid: pid of the process
5054 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5055 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5056 */
5057asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5058 unsigned long __user *user_mask_ptr)
5059{
5060 int ret;
5061 cpumask_t mask;
5062
5063 if (len < sizeof(cpumask_t))
5064 return -EINVAL;
5065
5066 ret = sched_getaffinity(pid, &mask);
5067 if (ret < 0)
5068 return ret;
5069
5070 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5071 return -EFAULT;
5072
5073 return sizeof(cpumask_t);
5074}
5075
5076/**
5077 * sys_sched_yield - yield the current processor to other threads.
5078 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005079 * This function yields the current CPU to other tasks. If there are no
5080 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 */
5082asmlinkage long sys_sched_yield(void)
5083{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005084 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Ingo Molnar2d723762007-10-15 17:00:12 +02005086 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005087 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
5089 /*
5090 * Since we are going to call schedule() anyway, there's
5091 * no need to preempt or enable interrupts:
5092 */
5093 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005094 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 _raw_spin_unlock(&rq->lock);
5096 preempt_enable_no_resched();
5097
5098 schedule();
5099
5100 return 0;
5101}
5102
Andrew Mortone7b38402006-06-30 01:56:00 -07005103static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005105#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5106 __might_sleep(__FILE__, __LINE__);
5107#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005108 /*
5109 * The BKS might be reacquired before we have dropped
5110 * PREEMPT_ACTIVE, which could trigger a second
5111 * cond_resched() call.
5112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 do {
5114 add_preempt_count(PREEMPT_ACTIVE);
5115 schedule();
5116 sub_preempt_count(PREEMPT_ACTIVE);
5117 } while (need_resched());
5118}
5119
Herbert Xu02b67cc32008-01-25 21:08:28 +01005120#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5121int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122{
Ingo Molnar94142322006-12-29 16:48:13 -08005123 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5124 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 __cond_resched();
5126 return 1;
5127 }
5128 return 0;
5129}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005130EXPORT_SYMBOL(_cond_resched);
5131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
5133/*
5134 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5135 * call schedule, and on return reacquire the lock.
5136 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005137 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 * operations here to prevent schedule() from being called twice (once via
5139 * spin_unlock(), once by hand).
5140 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005141int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142{
Nick Piggin95c354f2008-01-30 13:31:20 +01005143 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005144 int ret = 0;
5145
Nick Piggin95c354f2008-01-30 13:31:20 +01005146 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005148 if (resched && need_resched())
5149 __cond_resched();
5150 else
5151 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005152 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005155 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157EXPORT_SYMBOL(cond_resched_lock);
5158
5159int __sched cond_resched_softirq(void)
5160{
5161 BUG_ON(!in_softirq());
5162
Ingo Molnar94142322006-12-29 16:48:13 -08005163 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005164 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 __cond_resched();
5166 local_bh_disable();
5167 return 1;
5168 }
5169 return 0;
5170}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171EXPORT_SYMBOL(cond_resched_softirq);
5172
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173/**
5174 * yield - yield the current processor to other threads.
5175 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005176 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 * thread runnable and calls sys_sched_yield().
5178 */
5179void __sched yield(void)
5180{
5181 set_current_state(TASK_RUNNING);
5182 sys_sched_yield();
5183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184EXPORT_SYMBOL(yield);
5185
5186/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005187 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 * that process accounting knows that this is a task in IO wait state.
5189 *
5190 * But don't do that if it is a deliberate, throttling IO wait (this task
5191 * has set its backing_dev_info: the queue against which it should throttle)
5192 */
5193void __sched io_schedule(void)
5194{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005195 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005197 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 atomic_inc(&rq->nr_iowait);
5199 schedule();
5200 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005201 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203EXPORT_SYMBOL(io_schedule);
5204
5205long __sched io_schedule_timeout(long timeout)
5206{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005207 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 long ret;
5209
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005210 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 atomic_inc(&rq->nr_iowait);
5212 ret = schedule_timeout(timeout);
5213 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005214 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 return ret;
5216}
5217
5218/**
5219 * sys_sched_get_priority_max - return maximum RT priority.
5220 * @policy: scheduling class.
5221 *
5222 * this syscall returns the maximum rt_priority that can be used
5223 * by a given scheduling class.
5224 */
5225asmlinkage long sys_sched_get_priority_max(int policy)
5226{
5227 int ret = -EINVAL;
5228
5229 switch (policy) {
5230 case SCHED_FIFO:
5231 case SCHED_RR:
5232 ret = MAX_USER_RT_PRIO-1;
5233 break;
5234 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005235 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005236 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 ret = 0;
5238 break;
5239 }
5240 return ret;
5241}
5242
5243/**
5244 * sys_sched_get_priority_min - return minimum RT priority.
5245 * @policy: scheduling class.
5246 *
5247 * this syscall returns the minimum rt_priority that can be used
5248 * by a given scheduling class.
5249 */
5250asmlinkage long sys_sched_get_priority_min(int policy)
5251{
5252 int ret = -EINVAL;
5253
5254 switch (policy) {
5255 case SCHED_FIFO:
5256 case SCHED_RR:
5257 ret = 1;
5258 break;
5259 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005260 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005261 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 ret = 0;
5263 }
5264 return ret;
5265}
5266
5267/**
5268 * sys_sched_rr_get_interval - return the default timeslice of a process.
5269 * @pid: pid of the process.
5270 * @interval: userspace pointer to the timeslice value.
5271 *
5272 * this syscall writes the default timeslice value of a given process
5273 * into the user-space timespec buffer. A value of '0' means infinity.
5274 */
5275asmlinkage
5276long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5277{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005278 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005279 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005280 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282
5283 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285
5286 retval = -ESRCH;
5287 read_lock(&tasklist_lock);
5288 p = find_process_by_pid(pid);
5289 if (!p)
5290 goto out_unlock;
5291
5292 retval = security_task_getscheduler(p);
5293 if (retval)
5294 goto out_unlock;
5295
Ingo Molnar77034932007-12-04 17:04:39 +01005296 /*
5297 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5298 * tasks that are on an otherwise idle runqueue:
5299 */
5300 time_slice = 0;
5301 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005302 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005303 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005304 struct sched_entity *se = &p->se;
5305 unsigned long flags;
5306 struct rq *rq;
5307
5308 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005309 if (rq->cfs.load.weight)
5310 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005311 task_rq_unlock(rq, &flags);
5312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005314 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005317
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318out_unlock:
5319 read_unlock(&tasklist_lock);
5320 return retval;
5321}
5322
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005323static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005324
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005325void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005328 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005331 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005332 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005333#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005335 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005337 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338#else
5339 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005340 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005342 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343#endif
5344#ifdef CONFIG_DEBUG_STACK_USAGE
5345 {
Al Viro10ebffd2005-11-13 16:06:56 -08005346 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 while (!*n)
5348 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005349 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 }
5351#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005352 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005353 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005355 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356}
5357
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005358void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005360 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
Ingo Molnar4bd77322007-07-11 21:21:47 +02005362#if BITS_PER_LONG == 32
5363 printk(KERN_INFO
5364 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005366 printk(KERN_INFO
5367 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368#endif
5369 read_lock(&tasklist_lock);
5370 do_each_thread(g, p) {
5371 /*
5372 * reset the NMI-timeout, listing all files on a slow
5373 * console might take alot of time:
5374 */
5375 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005376 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005377 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 } while_each_thread(g, p);
5379
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005380 touch_all_softlockup_watchdogs();
5381
Ingo Molnardd41f592007-07-09 18:51:59 +02005382#ifdef CONFIG_SCHED_DEBUG
5383 sysrq_sched_debug_show();
5384#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005386 /*
5387 * Only show locks if all tasks are dumped:
5388 */
5389 if (state_filter == -1)
5390 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391}
5392
Ingo Molnar1df21052007-07-09 18:51:58 +02005393void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5394{
Ingo Molnardd41f592007-07-09 18:51:59 +02005395 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005396}
5397
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005398/**
5399 * init_idle - set up an idle thread for a given CPU
5400 * @idle: task in question
5401 * @cpu: cpu the idle task belongs to
5402 *
5403 * NOTE: this function does not set the idle thread's NEED_RESCHED
5404 * flag, to make booting more robust.
5405 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005406void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005408 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 unsigned long flags;
5410
Ingo Molnardd41f592007-07-09 18:51:59 +02005411 __sched_fork(idle);
5412 idle->se.exec_start = sched_clock();
5413
Ingo Molnarb29739f2006-06-27 02:54:51 -07005414 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005416 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
5418 spin_lock_irqsave(&rq->lock, flags);
5419 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005420#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5421 idle->oncpu = 1;
5422#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 spin_unlock_irqrestore(&rq->lock, flags);
5424
5425 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08005426 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005427
Ingo Molnardd41f592007-07-09 18:51:59 +02005428 /*
5429 * The idle tasks have their own, simple scheduling class:
5430 */
5431 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432}
5433
5434/*
5435 * In a system that switches off the HZ timer nohz_cpu_mask
5436 * indicates which cpus entered this state. This is used
5437 * in the rcu update to wait only for active cpus. For system
5438 * which do not switch off the HZ timer nohz_cpu_mask should
5439 * always be CPU_MASK_NONE.
5440 */
5441cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5442
Ingo Molnar19978ca2007-11-09 22:39:38 +01005443/*
5444 * Increase the granularity value when there are more CPUs,
5445 * because with more CPUs the 'effective latency' as visible
5446 * to users decreases. But the relationship is not linear,
5447 * so pick a second-best guess by going with the log2 of the
5448 * number of CPUs.
5449 *
5450 * This idea comes from the SD scheduler of Con Kolivas:
5451 */
5452static inline void sched_init_granularity(void)
5453{
5454 unsigned int factor = 1 + ilog2(num_online_cpus());
5455 const unsigned long limit = 200000000;
5456
5457 sysctl_sched_min_granularity *= factor;
5458 if (sysctl_sched_min_granularity > limit)
5459 sysctl_sched_min_granularity = limit;
5460
5461 sysctl_sched_latency *= factor;
5462 if (sysctl_sched_latency > limit)
5463 sysctl_sched_latency = limit;
5464
5465 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005466}
5467
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468#ifdef CONFIG_SMP
5469/*
5470 * This is how migration works:
5471 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005472 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 * runqueue and wake up that CPU's migration thread.
5474 * 2) we down() the locked semaphore => thread blocks.
5475 * 3) migration thread wakes up (implicitly it forces the migrated
5476 * thread off the CPU)
5477 * 4) it gets the migration request and checks whether the migrated
5478 * task is still in the wrong runqueue.
5479 * 5) if it's in the wrong runqueue then the migration thread removes
5480 * it and puts it into the right queue.
5481 * 6) migration thread up()s the semaphore.
5482 * 7) we wake up and the migration is done.
5483 */
5484
5485/*
5486 * Change a given task's CPU affinity. Migrate the thread to a
5487 * proper CPU and schedule it away if the CPU it's executing on
5488 * is removed from the allowed bitmask.
5489 *
5490 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005491 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 * call is not atomic; no spinlocks may be held.
5493 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005494int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005496 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005498 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005499 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
5501 rq = task_rq_lock(p, &flags);
5502 if (!cpus_intersects(new_mask, cpu_online_map)) {
5503 ret = -EINVAL;
5504 goto out;
5505 }
5506
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005507 if (p->sched_class->set_cpus_allowed)
5508 p->sched_class->set_cpus_allowed(p, &new_mask);
5509 else {
Ingo Molnar0eab9142008-01-25 21:08:19 +01005510 p->cpus_allowed = new_mask;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01005511 p->rt.nr_cpus_allowed = cpus_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005512 }
5513
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 /* Can the task run on the task's current CPU? If so, we're done */
5515 if (cpu_isset(task_cpu(p), new_mask))
5516 goto out;
5517
5518 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
5519 /* Need help from migration thread: drop lock and wait. */
5520 task_rq_unlock(rq, &flags);
5521 wake_up_process(rq->migration_thread);
5522 wait_for_completion(&req.done);
5523 tlb_migrate_finish(p->mm);
5524 return 0;
5525 }
5526out:
5527 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005528
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 return ret;
5530}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531EXPORT_SYMBOL_GPL(set_cpus_allowed);
5532
5533/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005534 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 * this because either it can't run here any more (set_cpus_allowed()
5536 * away from this CPU, or CPU going down), or because we're
5537 * attempting to rebalance this task on exec (sched_exec).
5538 *
5539 * So we race with normal scheduler movements, but that's OK, as long
5540 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005541 *
5542 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005544static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005546 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005547 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
5549 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005550 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551
5552 rq_src = cpu_rq(src_cpu);
5553 rq_dest = cpu_rq(dest_cpu);
5554
5555 double_rq_lock(rq_src, rq_dest);
5556 /* Already moved. */
5557 if (task_cpu(p) != src_cpu)
5558 goto out;
5559 /* Affinity changed (again). */
5560 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5561 goto out;
5562
Ingo Molnardd41f592007-07-09 18:51:59 +02005563 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005564 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005565 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005566
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005568 if (on_rq) {
5569 activate_task(rq_dest, p, 0);
5570 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005572 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573out:
5574 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005575 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576}
5577
5578/*
5579 * migration_thread - this is a highprio system thread that performs
5580 * thread migration by bumping thread off CPU then 'pushing' onto
5581 * another runqueue.
5582 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005583static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005586 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587
5588 rq = cpu_rq(cpu);
5589 BUG_ON(rq->migration_thread != current);
5590
5591 set_current_state(TASK_INTERRUPTIBLE);
5592 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005593 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 spin_lock_irq(&rq->lock);
5597
5598 if (cpu_is_offline(cpu)) {
5599 spin_unlock_irq(&rq->lock);
5600 goto wait_to_die;
5601 }
5602
5603 if (rq->active_balance) {
5604 active_load_balance(rq, cpu);
5605 rq->active_balance = 0;
5606 }
5607
5608 head = &rq->migration_queue;
5609
5610 if (list_empty(head)) {
5611 spin_unlock_irq(&rq->lock);
5612 schedule();
5613 set_current_state(TASK_INTERRUPTIBLE);
5614 continue;
5615 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005616 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 list_del_init(head->next);
5618
Nick Piggin674311d2005-06-25 14:57:27 -07005619 spin_unlock(&rq->lock);
5620 __migrate_task(req->task, cpu, req->dest_cpu);
5621 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
5623 complete(&req->done);
5624 }
5625 __set_current_state(TASK_RUNNING);
5626 return 0;
5627
5628wait_to_die:
5629 /* Wait for kthread_stop */
5630 set_current_state(TASK_INTERRUPTIBLE);
5631 while (!kthread_should_stop()) {
5632 schedule();
5633 set_current_state(TASK_INTERRUPTIBLE);
5634 }
5635 __set_current_state(TASK_RUNNING);
5636 return 0;
5637}
5638
5639#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005640
5641static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5642{
5643 int ret;
5644
5645 local_irq_disable();
5646 ret = __migrate_task(p, src_cpu, dest_cpu);
5647 local_irq_enable();
5648 return ret;
5649}
5650
Kirill Korotaev054b9102006-12-10 02:20:11 -08005651/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005652 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005653 * NOTE: interrupts should be disabled by the caller
5654 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005655static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005657 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005659 struct rq *rq;
5660 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661
Andi Kleen3a5c3592007-10-15 17:00:14 +02005662 do {
5663 /* On same node? */
5664 mask = node_to_cpumask(cpu_to_node(dead_cpu));
5665 cpus_and(mask, mask, p->cpus_allowed);
5666 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
Andi Kleen3a5c3592007-10-15 17:00:14 +02005668 /* On any allowed CPU? */
5669 if (dest_cpu == NR_CPUS)
5670 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
Andi Kleen3a5c3592007-10-15 17:00:14 +02005672 /* No more Mr. Nice Guy. */
5673 if (dest_cpu == NR_CPUS) {
Cliff Wickman470fd6462007-10-18 23:40:46 -07005674 cpumask_t cpus_allowed = cpuset_cpus_allowed_locked(p);
5675 /*
5676 * Try to stay on the same cpuset, where the
5677 * current cpuset may be a subset of all cpus.
5678 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005679 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd6462007-10-18 23:40:46 -07005680 * called within calls to cpuset_lock/cpuset_unlock.
5681 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02005682 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd6462007-10-18 23:40:46 -07005683 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005684 dest_cpu = any_online_cpu(p->cpus_allowed);
5685 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
Andi Kleen3a5c3592007-10-15 17:00:14 +02005687 /*
5688 * Don't tell them about moving exiting tasks or
5689 * kernel threads (both mm NULL), since they never
5690 * leave kernel.
5691 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005692 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02005693 printk(KERN_INFO "process %d (%s) no "
5694 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005695 task_pid_nr(p), p->comm, dead_cpu);
5696 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02005697 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005698 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699}
5700
5701/*
5702 * While a dead CPU has no uninterruptible tasks queued at this point,
5703 * it might still have a nonzero ->nr_uninterruptible counter, because
5704 * for performance reasons the counter is not stricly tracking tasks to
5705 * their home CPUs. So we just add the counter to another CPU's counter,
5706 * to keep the global sum constant after CPU-down:
5707 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005708static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005710 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 unsigned long flags;
5712
5713 local_irq_save(flags);
5714 double_rq_lock(rq_src, rq_dest);
5715 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5716 rq_src->nr_uninterruptible = 0;
5717 double_rq_unlock(rq_src, rq_dest);
5718 local_irq_restore(flags);
5719}
5720
5721/* Run through task list and migrate tasks from the dead cpu. */
5722static void migrate_live_tasks(int src_cpu)
5723{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005724 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005726 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
Ingo Molnar48f24c42006-07-03 00:25:40 -07005728 do_each_thread(t, p) {
5729 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 continue;
5731
Ingo Molnar48f24c42006-07-03 00:25:40 -07005732 if (task_cpu(p) == src_cpu)
5733 move_task_off_dead_cpu(src_cpu, p);
5734 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005736 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737}
5738
Ingo Molnardd41f592007-07-09 18:51:59 +02005739/*
5740 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005741 * It does so by boosting its priority to highest possible.
5742 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 */
5744void sched_idle_next(void)
5745{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005746 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005747 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 struct task_struct *p = rq->idle;
5749 unsigned long flags;
5750
5751 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005752 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753
Ingo Molnar48f24c42006-07-03 00:25:40 -07005754 /*
5755 * Strictly not necessary since rest of the CPUs are stopped by now
5756 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 */
5758 spin_lock_irqsave(&rq->lock, flags);
5759
Ingo Molnardd41f592007-07-09 18:51:59 +02005760 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005761
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005762 update_rq_clock(rq);
5763 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
5765 spin_unlock_irqrestore(&rq->lock, flags);
5766}
5767
Ingo Molnar48f24c42006-07-03 00:25:40 -07005768/*
5769 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 * offline.
5771 */
5772void idle_task_exit(void)
5773{
5774 struct mm_struct *mm = current->active_mm;
5775
5776 BUG_ON(cpu_online(smp_processor_id()));
5777
5778 if (mm != &init_mm)
5779 switch_mm(mm, &init_mm, current);
5780 mmdrop(mm);
5781}
5782
Kirill Korotaev054b9102006-12-10 02:20:11 -08005783/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005784static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005786 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
5788 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07005789 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790
5791 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005792 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793
Ingo Molnar48f24c42006-07-03 00:25:40 -07005794 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795
5796 /*
5797 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005798 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 * fine.
5800 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005801 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005802 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005803 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804
Ingo Molnar48f24c42006-07-03 00:25:40 -07005805 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806}
5807
5808/* release_task() removes task from tasklist, so we won't find dead tasks. */
5809static void migrate_dead_tasks(unsigned int dead_cpu)
5810{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005811 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005812 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
Ingo Molnardd41f592007-07-09 18:51:59 +02005814 for ( ; ; ) {
5815 if (!rq->nr_running)
5816 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005817 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02005818 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02005819 if (!next)
5820 break;
5821 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005822
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 }
5824}
5825#endif /* CONFIG_HOTPLUG_CPU */
5826
Nick Piggine692ab52007-07-26 13:40:43 +02005827#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5828
5829static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005830 {
5831 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005832 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005833 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005834 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005835};
5836
5837static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005838 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005839 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005840 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005841 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005842 .child = sd_ctl_dir,
5843 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005844 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005845};
5846
5847static struct ctl_table *sd_alloc_ctl_entry(int n)
5848{
5849 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005850 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005851
Nick Piggine692ab52007-07-26 13:40:43 +02005852 return entry;
5853}
5854
Milton Miller6382bc92007-10-15 17:00:19 +02005855static void sd_free_ctl_entry(struct ctl_table **tablep)
5856{
Milton Millercd7900762007-10-17 16:55:11 +02005857 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005858
Milton Millercd7900762007-10-17 16:55:11 +02005859 /*
5860 * In the intermediate directories, both the child directory and
5861 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005862 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02005863 * static strings and all have proc handlers.
5864 */
5865 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005866 if (entry->child)
5867 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02005868 if (entry->proc_handler == NULL)
5869 kfree(entry->procname);
5870 }
Milton Miller6382bc92007-10-15 17:00:19 +02005871
5872 kfree(*tablep);
5873 *tablep = NULL;
5874}
5875
Nick Piggine692ab52007-07-26 13:40:43 +02005876static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005877set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005878 const char *procname, void *data, int maxlen,
5879 mode_t mode, proc_handler *proc_handler)
5880{
Nick Piggine692ab52007-07-26 13:40:43 +02005881 entry->procname = procname;
5882 entry->data = data;
5883 entry->maxlen = maxlen;
5884 entry->mode = mode;
5885 entry->proc_handler = proc_handler;
5886}
5887
5888static struct ctl_table *
5889sd_alloc_ctl_domain_table(struct sched_domain *sd)
5890{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005891 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02005892
Milton Millerad1cdc12007-10-15 17:00:19 +02005893 if (table == NULL)
5894 return NULL;
5895
Alexey Dobriyane0361852007-08-09 11:16:46 +02005896 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005897 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005898 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005899 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005900 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005901 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005902 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005903 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005904 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005905 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005906 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005907 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005908 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005909 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005910 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005911 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005912 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005913 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005914 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005915 &sd->cache_nice_tries,
5916 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005917 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005918 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02005919 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005920
5921 return table;
5922}
5923
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005924static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005925{
5926 struct ctl_table *entry, *table;
5927 struct sched_domain *sd;
5928 int domain_num = 0, i;
5929 char buf[32];
5930
5931 for_each_domain(cpu, sd)
5932 domain_num++;
5933 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005934 if (table == NULL)
5935 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005936
5937 i = 0;
5938 for_each_domain(cpu, sd) {
5939 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005940 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005941 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005942 entry->child = sd_alloc_ctl_domain_table(sd);
5943 entry++;
5944 i++;
5945 }
5946 return table;
5947}
5948
5949static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005950static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005951{
5952 int i, cpu_num = num_online_cpus();
5953 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5954 char buf[32];
5955
Milton Miller73785472007-10-24 18:23:48 +02005956 WARN_ON(sd_ctl_dir[0].child);
5957 sd_ctl_dir[0].child = entry;
5958
Milton Millerad1cdc12007-10-15 17:00:19 +02005959 if (entry == NULL)
5960 return;
5961
Milton Miller97b6ea72007-10-15 17:00:19 +02005962 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005963 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005964 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005965 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005966 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005967 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005968 }
Milton Miller73785472007-10-24 18:23:48 +02005969
5970 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005971 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5972}
Milton Miller6382bc92007-10-15 17:00:19 +02005973
Milton Miller73785472007-10-24 18:23:48 +02005974/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005975static void unregister_sched_domain_sysctl(void)
5976{
Milton Miller73785472007-10-24 18:23:48 +02005977 if (sd_sysctl_header)
5978 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005979 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005980 if (sd_ctl_dir[0].child)
5981 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005982}
Nick Piggine692ab52007-07-26 13:40:43 +02005983#else
Milton Miller6382bc92007-10-15 17:00:19 +02005984static void register_sched_domain_sysctl(void)
5985{
5986}
5987static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005988{
5989}
5990#endif
5991
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992/*
5993 * migration_call - callback that gets triggered when a CPU is added.
5994 * Here we can start up the necessary migration thread for the new CPU.
5995 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005996static int __cpuinit
5997migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006000 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006002 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003
6004 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006007 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006008 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 if (IS_ERR(p))
6010 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 kthread_bind(p, cpu);
6012 /* Must be high prio: stop_machine expects to yield to it. */
6013 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006014 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 task_rq_unlock(rq, &flags);
6016 cpu_rq(cpu)->migration_thread = p;
6017 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006018
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006020 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006021 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006023
6024 /* Update our root-domain */
6025 rq = cpu_rq(cpu);
6026 spin_lock_irqsave(&rq->lock, flags);
6027 if (rq->rd) {
6028 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6029 cpu_set(cpu, rq->rd->online);
6030 }
6031 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006033
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034#ifdef CONFIG_HOTPLUG_CPU
6035 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006036 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006037 if (!cpu_rq(cpu)->migration_thread)
6038 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006039 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006040 kthread_bind(cpu_rq(cpu)->migration_thread,
6041 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 kthread_stop(cpu_rq(cpu)->migration_thread);
6043 cpu_rq(cpu)->migration_thread = NULL;
6044 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006045
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006047 case CPU_DEAD_FROZEN:
Cliff Wickman470fd6462007-10-18 23:40:46 -07006048 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 migrate_live_tasks(cpu);
6050 rq = cpu_rq(cpu);
6051 kthread_stop(rq->migration_thread);
6052 rq->migration_thread = NULL;
6053 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006054 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006055 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006056 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006058 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6059 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006061 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd6462007-10-18 23:40:46 -07006062 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 migrate_nr_uninterruptible(rq);
6064 BUG_ON(rq->nr_running != 0);
6065
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006066 /*
6067 * No need to migrate the tasks: it was best-effort if
6068 * they didn't take sched_hotcpu_mutex. Just wake up
6069 * the requestors.
6070 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 spin_lock_irq(&rq->lock);
6072 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006073 struct migration_req *req;
6074
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006076 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 list_del_init(&req->list);
6078 complete(&req->done);
6079 }
6080 spin_unlock_irq(&rq->lock);
6081 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006082
Gregory Haskins08f503b2008-03-10 17:59:11 -04006083 case CPU_DYING:
6084 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006085 /* Update our root-domain */
6086 rq = cpu_rq(cpu);
6087 spin_lock_irqsave(&rq->lock, flags);
6088 if (rq->rd) {
6089 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6090 cpu_clear(cpu, rq->rd->online);
6091 }
6092 spin_unlock_irqrestore(&rq->lock, flags);
6093 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094#endif
6095 }
6096 return NOTIFY_OK;
6097}
6098
6099/* Register at highest priority so that task migration (migrate_all_tasks)
6100 * happens before everything else.
6101 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006102static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 .notifier_call = migration_call,
6104 .priority = 10
6105};
6106
Adrian Bunke6fe6642007-11-09 22:39:39 +01006107void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108{
6109 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006110 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006111
6112 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006113 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6114 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6116 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117}
6118#endif
6119
6120#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006121
6122/* Number of possible processor ids */
6123int nr_cpu_ids __read_mostly = NR_CPUS;
6124EXPORT_SYMBOL(nr_cpu_ids);
6125
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006126#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006127
6128static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level)
6129{
6130 struct sched_group *group = sd->groups;
6131 cpumask_t groupmask;
6132 char str[NR_CPUS];
6133
6134 cpumask_scnprintf(str, NR_CPUS, sd->span);
6135 cpus_clear(groupmask);
6136
6137 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6138
6139 if (!(sd->flags & SD_LOAD_BALANCE)) {
6140 printk("does not load-balance\n");
6141 if (sd->parent)
6142 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6143 " has parent");
6144 return -1;
6145 }
6146
6147 printk(KERN_CONT "span %s\n", str);
6148
6149 if (!cpu_isset(cpu, sd->span)) {
6150 printk(KERN_ERR "ERROR: domain->span does not contain "
6151 "CPU%d\n", cpu);
6152 }
6153 if (!cpu_isset(cpu, group->cpumask)) {
6154 printk(KERN_ERR "ERROR: domain->groups does not contain"
6155 " CPU%d\n", cpu);
6156 }
6157
6158 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6159 do {
6160 if (!group) {
6161 printk("\n");
6162 printk(KERN_ERR "ERROR: group is NULL\n");
6163 break;
6164 }
6165
6166 if (!group->__cpu_power) {
6167 printk(KERN_CONT "\n");
6168 printk(KERN_ERR "ERROR: domain->cpu_power not "
6169 "set\n");
6170 break;
6171 }
6172
6173 if (!cpus_weight(group->cpumask)) {
6174 printk(KERN_CONT "\n");
6175 printk(KERN_ERR "ERROR: empty group\n");
6176 break;
6177 }
6178
6179 if (cpus_intersects(groupmask, group->cpumask)) {
6180 printk(KERN_CONT "\n");
6181 printk(KERN_ERR "ERROR: repeated CPUs\n");
6182 break;
6183 }
6184
6185 cpus_or(groupmask, groupmask, group->cpumask);
6186
6187 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
6188 printk(KERN_CONT " %s", str);
6189
6190 group = group->next;
6191 } while (group != sd->groups);
6192 printk(KERN_CONT "\n");
6193
6194 if (!cpus_equal(sd->span, groupmask))
6195 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6196
6197 if (sd->parent && !cpus_subset(groupmask, sd->parent->span))
6198 printk(KERN_ERR "ERROR: parent span is not a superset "
6199 "of domain->span\n");
6200 return 0;
6201}
6202
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203static void sched_domain_debug(struct sched_domain *sd, int cpu)
6204{
6205 int level = 0;
6206
Nick Piggin41c7ce92005-06-25 14:57:24 -07006207 if (!sd) {
6208 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6209 return;
6210 }
6211
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6213
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006214 for (;;) {
6215 if (sched_domain_debug_one(sd, cpu, level))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 level++;
6218 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006219 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006220 break;
6221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222}
6223#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006224# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225#endif
6226
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006227static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006228{
6229 if (cpus_weight(sd->span) == 1)
6230 return 1;
6231
6232 /* Following flags need at least 2 groups */
6233 if (sd->flags & (SD_LOAD_BALANCE |
6234 SD_BALANCE_NEWIDLE |
6235 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006236 SD_BALANCE_EXEC |
6237 SD_SHARE_CPUPOWER |
6238 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006239 if (sd->groups != sd->groups->next)
6240 return 0;
6241 }
6242
6243 /* Following flags don't use groups */
6244 if (sd->flags & (SD_WAKE_IDLE |
6245 SD_WAKE_AFFINE |
6246 SD_WAKE_BALANCE))
6247 return 0;
6248
6249 return 1;
6250}
6251
Ingo Molnar48f24c42006-07-03 00:25:40 -07006252static int
6253sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006254{
6255 unsigned long cflags = sd->flags, pflags = parent->flags;
6256
6257 if (sd_degenerate(parent))
6258 return 1;
6259
6260 if (!cpus_equal(sd->span, parent->span))
6261 return 0;
6262
6263 /* Does parent contain flags not in child? */
6264 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6265 if (cflags & SD_WAKE_AFFINE)
6266 pflags &= ~SD_WAKE_BALANCE;
6267 /* Flags needing groups don't count if only 1 group in parent */
6268 if (parent->groups == parent->groups->next) {
6269 pflags &= ~(SD_LOAD_BALANCE |
6270 SD_BALANCE_NEWIDLE |
6271 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006272 SD_BALANCE_EXEC |
6273 SD_SHARE_CPUPOWER |
6274 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006275 }
6276 if (~cflags & pflags)
6277 return 0;
6278
6279 return 1;
6280}
6281
Gregory Haskins57d885f2008-01-25 21:08:18 +01006282static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6283{
6284 unsigned long flags;
6285 const struct sched_class *class;
6286
6287 spin_lock_irqsave(&rq->lock, flags);
6288
6289 if (rq->rd) {
6290 struct root_domain *old_rd = rq->rd;
6291
Ingo Molnar0eab9142008-01-25 21:08:19 +01006292 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006293 if (class->leave_domain)
6294 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006295 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006296
Gregory Haskinsdc938522008-01-25 21:08:26 +01006297 cpu_clear(rq->cpu, old_rd->span);
6298 cpu_clear(rq->cpu, old_rd->online);
6299
Gregory Haskins57d885f2008-01-25 21:08:18 +01006300 if (atomic_dec_and_test(&old_rd->refcount))
6301 kfree(old_rd);
6302 }
6303
6304 atomic_inc(&rd->refcount);
6305 rq->rd = rd;
6306
Gregory Haskinsdc938522008-01-25 21:08:26 +01006307 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006308 if (cpu_isset(rq->cpu, cpu_online_map))
6309 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006310
Ingo Molnar0eab9142008-01-25 21:08:19 +01006311 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006312 if (class->join_domain)
6313 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006314 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006315
6316 spin_unlock_irqrestore(&rq->lock, flags);
6317}
6318
Gregory Haskinsdc938522008-01-25 21:08:26 +01006319static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006320{
6321 memset(rd, 0, sizeof(*rd));
6322
Gregory Haskinsdc938522008-01-25 21:08:26 +01006323 cpus_clear(rd->span);
6324 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006325}
6326
6327static void init_defrootdomain(void)
6328{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006329 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006330 atomic_set(&def_root_domain.refcount, 1);
6331}
6332
Gregory Haskinsdc938522008-01-25 21:08:26 +01006333static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006334{
6335 struct root_domain *rd;
6336
6337 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6338 if (!rd)
6339 return NULL;
6340
Gregory Haskinsdc938522008-01-25 21:08:26 +01006341 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006342
6343 return rd;
6344}
6345
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006347 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 * hold the hotplug lock.
6349 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006350static void
6351cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006353 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006354 struct sched_domain *tmp;
6355
6356 /* Remove the sched domains which do not contribute to scheduling. */
6357 for (tmp = sd; tmp; tmp = tmp->parent) {
6358 struct sched_domain *parent = tmp->parent;
6359 if (!parent)
6360 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006361 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006362 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006363 if (parent->parent)
6364 parent->parent->child = tmp;
6365 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006366 }
6367
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006368 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006369 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006370 if (sd)
6371 sd->child = NULL;
6372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373
6374 sched_domain_debug(sd, cpu);
6375
Gregory Haskins57d885f2008-01-25 21:08:18 +01006376 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006377 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378}
6379
6380/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006381static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
6383/* Setup the mask of cpus configured for isolated domains */
6384static int __init isolated_cpu_setup(char *str)
6385{
6386 int ints[NR_CPUS], i;
6387
6388 str = get_options(str, ARRAY_SIZE(ints), ints);
6389 cpus_clear(cpu_isolated_map);
6390 for (i = 1; i <= ints[0]; i++)
6391 if (ints[i] < NR_CPUS)
6392 cpu_set(ints[i], cpu_isolated_map);
6393 return 1;
6394}
6395
Ingo Molnar8927f492007-10-15 17:00:13 +02006396__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397
6398/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006399 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6400 * to a function which identifies what group(along with sched group) a CPU
6401 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6402 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 *
6404 * init_sched_build_groups will build a circular linked list of the groups
6405 * covered by the given span, and will set each group's ->cpumask correctly,
6406 * and ->cpu_power to 0.
6407 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006408static void
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006409init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
6410 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
6411 struct sched_group **sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412{
6413 struct sched_group *first = NULL, *last = NULL;
6414 cpumask_t covered = CPU_MASK_NONE;
6415 int i;
6416
6417 for_each_cpu_mask(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006418 struct sched_group *sg;
6419 int group = group_fn(i, cpu_map, &sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 int j;
6421
6422 if (cpu_isset(i, covered))
6423 continue;
6424
6425 sg->cpumask = CPU_MASK_NONE;
Eric Dumazet5517d862007-05-08 00:32:57 -07006426 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427
6428 for_each_cpu_mask(j, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006429 if (group_fn(j, cpu_map, NULL) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 continue;
6431
6432 cpu_set(j, covered);
6433 cpu_set(j, sg->cpumask);
6434 }
6435 if (!first)
6436 first = sg;
6437 if (last)
6438 last->next = sg;
6439 last = sg;
6440 }
6441 last->next = first;
6442}
6443
John Hawkes9c1cfda2005-09-06 15:18:14 -07006444#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445
John Hawkes9c1cfda2005-09-06 15:18:14 -07006446#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006447
John Hawkes9c1cfda2005-09-06 15:18:14 -07006448/**
6449 * find_next_best_node - find the next node to include in a sched_domain
6450 * @node: node whose sched_domain we're building
6451 * @used_nodes: nodes already in the sched_domain
6452 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006453 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006454 * finds the closest node not already in the @used_nodes map.
6455 *
6456 * Should use nodemask_t.
6457 */
6458static int find_next_best_node(int node, unsigned long *used_nodes)
6459{
6460 int i, n, val, min_val, best_node = 0;
6461
6462 min_val = INT_MAX;
6463
6464 for (i = 0; i < MAX_NUMNODES; i++) {
6465 /* Start at @node */
6466 n = (node + i) % MAX_NUMNODES;
6467
6468 if (!nr_cpus_node(n))
6469 continue;
6470
6471 /* Skip already used nodes */
6472 if (test_bit(n, used_nodes))
6473 continue;
6474
6475 /* Simple min distance search */
6476 val = node_distance(node, n);
6477
6478 if (val < min_val) {
6479 min_val = val;
6480 best_node = n;
6481 }
6482 }
6483
6484 set_bit(best_node, used_nodes);
6485 return best_node;
6486}
6487
6488/**
6489 * sched_domain_node_span - get a cpumask for a node's sched_domain
6490 * @node: node whose cpumask we're constructing
6491 * @size: number of nodes to include in this span
6492 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006493 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006494 * should be one that prevents unnecessary balancing, but also spreads tasks
6495 * out optimally.
6496 */
6497static cpumask_t sched_domain_node_span(int node)
6498{
John Hawkes9c1cfda2005-09-06 15:18:14 -07006499 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006500 cpumask_t span, nodemask;
6501 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006502
6503 cpus_clear(span);
6504 bitmap_zero(used_nodes, MAX_NUMNODES);
6505
6506 nodemask = node_to_cpumask(node);
6507 cpus_or(span, span, nodemask);
6508 set_bit(node, used_nodes);
6509
6510 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
6511 int next_node = find_next_best_node(node, used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006512
John Hawkes9c1cfda2005-09-06 15:18:14 -07006513 nodemask = node_to_cpumask(next_node);
6514 cpus_or(span, span, nodemask);
6515 }
6516
6517 return span;
6518}
6519#endif
6520
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006521int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006522
John Hawkes9c1cfda2005-09-06 15:18:14 -07006523/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006524 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526#ifdef CONFIG_SCHED_SMT
6527static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006528static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006529
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006530static int
6531cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006533 if (sg)
6534 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 return cpu;
6536}
6537#endif
6538
Ingo Molnar48f24c42006-07-03 00:25:40 -07006539/*
6540 * multi-core sched-domains:
6541 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006542#ifdef CONFIG_SCHED_MC
6543static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006544static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006545#endif
6546
6547#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006548static int
6549cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006550{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006551 int group;
Mike Travisd5a74302007-10-16 01:24:05 -07006552 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006553 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006554 group = first_cpu(mask);
6555 if (sg)
6556 *sg = &per_cpu(sched_group_core, group);
6557 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006558}
6559#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006560static int
6561cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006562{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006563 if (sg)
6564 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006565 return cpu;
6566}
6567#endif
6568
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006570static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006571
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006572static int
6573cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006575 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006576#ifdef CONFIG_SCHED_MC
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006577 cpumask_t mask = cpu_coregroup_map(cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006578 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006579 group = first_cpu(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006580#elif defined(CONFIG_SCHED_SMT)
Mike Travisd5a74302007-10-16 01:24:05 -07006581 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006582 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006583 group = first_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006585 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006587 if (sg)
6588 *sg = &per_cpu(sched_group_phys, group);
6589 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590}
6591
6592#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006593/*
6594 * The init_sched_build_groups can't handle what we want to do with node
6595 * groups, so roll our own. Now each node has its own list of groups which
6596 * gets dynamically allocated.
6597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598static DEFINE_PER_CPU(struct sched_domain, node_domains);
John Hawkesd1b55132005-09-06 15:18:14 -07006599static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
John Hawkes9c1cfda2005-09-06 15:18:14 -07006600
6601static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006602static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006603
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006604static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
6605 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006607 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
6608 int group;
6609
6610 cpus_and(nodemask, nodemask, *cpu_map);
6611 group = first_cpu(nodemask);
6612
6613 if (sg)
6614 *sg = &per_cpu(sched_group_allnodes, group);
6615 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006617
Siddha, Suresh B08069032006-03-27 01:15:23 -08006618static void init_numa_sched_groups_power(struct sched_group *group_head)
6619{
6620 struct sched_group *sg = group_head;
6621 int j;
6622
6623 if (!sg)
6624 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006625 do {
6626 for_each_cpu_mask(j, sg->cpumask) {
6627 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006628
Andi Kleen3a5c3592007-10-15 17:00:14 +02006629 sd = &per_cpu(phys_domains, j);
6630 if (j != first_cpu(sd->groups->cpumask)) {
6631 /*
6632 * Only add "power" once for each
6633 * physical package.
6634 */
6635 continue;
6636 }
6637
6638 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006639 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006640 sg = sg->next;
6641 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006642}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643#endif
6644
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006645#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006646/* Free memory allocated for various sched_group structures */
6647static void free_sched_groups(const cpumask_t *cpu_map)
6648{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006649 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006650
6651 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006652 struct sched_group **sched_group_nodes
6653 = sched_group_nodes_bycpu[cpu];
6654
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006655 if (!sched_group_nodes)
6656 continue;
6657
6658 for (i = 0; i < MAX_NUMNODES; i++) {
6659 cpumask_t nodemask = node_to_cpumask(i);
6660 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6661
6662 cpus_and(nodemask, nodemask, *cpu_map);
6663 if (cpus_empty(nodemask))
6664 continue;
6665
6666 if (sg == NULL)
6667 continue;
6668 sg = sg->next;
6669next_sg:
6670 oldsg = sg;
6671 sg = sg->next;
6672 kfree(oldsg);
6673 if (oldsg != sched_group_nodes[i])
6674 goto next_sg;
6675 }
6676 kfree(sched_group_nodes);
6677 sched_group_nodes_bycpu[cpu] = NULL;
6678 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006679}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006680#else
6681static void free_sched_groups(const cpumask_t *cpu_map)
6682{
6683}
6684#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006685
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006687 * Initialize sched groups cpu_power.
6688 *
6689 * cpu_power indicates the capacity of sched group, which is used while
6690 * distributing the load between different sched groups in a sched domain.
6691 * Typically cpu_power for all the groups in a sched domain will be same unless
6692 * there are asymmetries in the topology. If there are asymmetries, group
6693 * having more cpu_power will pickup more load compared to the group having
6694 * less cpu_power.
6695 *
6696 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
6697 * the maximum number of tasks a group can handle in the presence of other idle
6698 * or lightly loaded groups in the same sched domain.
6699 */
6700static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6701{
6702 struct sched_domain *child;
6703 struct sched_group *group;
6704
6705 WARN_ON(!sd || !sd->groups);
6706
6707 if (cpu != first_cpu(sd->groups->cpumask))
6708 return;
6709
6710 child = sd->child;
6711
Eric Dumazet5517d862007-05-08 00:32:57 -07006712 sd->groups->__cpu_power = 0;
6713
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006714 /*
6715 * For perf policy, if the groups in child domain share resources
6716 * (for example cores sharing some portions of the cache hierarchy
6717 * or SMT), then set this domain groups cpu_power such that each group
6718 * can handle only one task, when there are other idle groups in the
6719 * same sched domain.
6720 */
6721 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
6722 (child->flags &
6723 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07006724 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006725 return;
6726 }
6727
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006728 /*
6729 * add cpu_power of each child group to this groups cpu_power
6730 */
6731 group = child->groups;
6732 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07006733 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006734 group = group->next;
6735 } while (group != child->groups);
6736}
6737
6738/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006739 * Build sched domains for a given set of cpus and attach the sched domains
6740 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006742static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743{
6744 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006745 struct root_domain *rd;
John Hawkesd1b55132005-09-06 15:18:14 -07006746#ifdef CONFIG_NUMA
6747 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006748 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07006749
6750 /*
6751 * Allocate the per-node list of sched groups
6752 */
Milton Miller5cf9f062007-10-15 17:00:19 +02006753 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006754 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006755 if (!sched_group_nodes) {
6756 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006757 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006758 }
6759 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6760#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761
Gregory Haskinsdc938522008-01-25 21:08:26 +01006762 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01006763 if (!rd) {
6764 printk(KERN_WARNING "Cannot alloc root domain\n");
6765 return -ENOMEM;
6766 }
6767
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006769 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006771 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 struct sched_domain *sd = NULL, *p;
6773 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
6774
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006775 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
6777#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006778 if (cpus_weight(*cpu_map) >
6779 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006780 sd = &per_cpu(allnodes_domains, i);
6781 *sd = SD_ALLNODES_INIT;
6782 sd->span = *cpu_map;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006783 cpu_to_allnodes_group(i, cpu_map, &sd->groups);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006784 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006785 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006786 } else
6787 p = NULL;
6788
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 sd = &per_cpu(node_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 *sd = SD_NODE_INIT;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006791 sd->span = sched_domain_node_span(cpu_to_node(i));
6792 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006793 if (p)
6794 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006795 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796#endif
6797
6798 p = sd;
6799 sd = &per_cpu(phys_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 *sd = SD_CPU_INIT;
6801 sd->span = nodemask;
6802 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006803 if (p)
6804 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006805 cpu_to_phys_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006807#ifdef CONFIG_SCHED_MC
6808 p = sd;
6809 sd = &per_cpu(core_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006810 *sd = SD_MC_INIT;
6811 sd->span = cpu_coregroup_map(i);
6812 cpus_and(sd->span, sd->span, *cpu_map);
6813 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006814 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006815 cpu_to_core_group(i, cpu_map, &sd->groups);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006816#endif
6817
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818#ifdef CONFIG_SCHED_SMT
6819 p = sd;
6820 sd = &per_cpu(cpu_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 *sd = SD_SIBLING_INIT;
Mike Travisd5a74302007-10-16 01:24:05 -07006822 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006823 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006825 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006826 cpu_to_cpu_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827#endif
6828 }
6829
6830#ifdef CONFIG_SCHED_SMT
6831 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006832 for_each_cpu_mask(i, *cpu_map) {
Mike Travisd5a74302007-10-16 01:24:05 -07006833 cpumask_t this_sibling_map = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006834 cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 if (i != first_cpu(this_sibling_map))
6836 continue;
6837
Ingo Molnardd41f592007-07-09 18:51:59 +02006838 init_sched_build_groups(this_sibling_map, cpu_map,
6839 &cpu_to_cpu_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 }
6841#endif
6842
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006843#ifdef CONFIG_SCHED_MC
6844 /* Set up multi-core groups */
6845 for_each_cpu_mask(i, *cpu_map) {
6846 cpumask_t this_core_map = cpu_coregroup_map(i);
6847 cpus_and(this_core_map, this_core_map, *cpu_map);
6848 if (i != first_cpu(this_core_map))
6849 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006850 init_sched_build_groups(this_core_map, cpu_map,
6851 &cpu_to_core_group);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006852 }
6853#endif
6854
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 /* Set up physical groups */
6856 for (i = 0; i < MAX_NUMNODES; i++) {
6857 cpumask_t nodemask = node_to_cpumask(i);
6858
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006859 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 if (cpus_empty(nodemask))
6861 continue;
6862
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006863 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 }
6865
6866#ifdef CONFIG_NUMA
6867 /* Set up node groups */
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006868 if (sd_allnodes)
Ingo Molnardd41f592007-07-09 18:51:59 +02006869 init_sched_build_groups(*cpu_map, cpu_map,
6870 &cpu_to_allnodes_group);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006871
6872 for (i = 0; i < MAX_NUMNODES; i++) {
6873 /* Set up node groups */
6874 struct sched_group *sg, *prev;
6875 cpumask_t nodemask = node_to_cpumask(i);
6876 cpumask_t domainspan;
6877 cpumask_t covered = CPU_MASK_NONE;
6878 int j;
6879
6880 cpus_and(nodemask, nodemask, *cpu_map);
John Hawkesd1b55132005-09-06 15:18:14 -07006881 if (cpus_empty(nodemask)) {
6882 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006883 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07006884 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006885
6886 domainspan = sched_domain_node_span(i);
6887 cpus_and(domainspan, domainspan, *cpu_map);
6888
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006889 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006890 if (!sg) {
6891 printk(KERN_WARNING "Can not alloc domain group for "
6892 "node %d\n", i);
6893 goto error;
6894 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006895 sched_group_nodes[i] = sg;
6896 for_each_cpu_mask(j, nodemask) {
6897 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02006898
John Hawkes9c1cfda2005-09-06 15:18:14 -07006899 sd = &per_cpu(node_domains, j);
6900 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006901 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006902 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006903 sg->cpumask = nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006904 sg->next = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006905 cpus_or(covered, covered, nodemask);
6906 prev = sg;
6907
6908 for (j = 0; j < MAX_NUMNODES; j++) {
6909 cpumask_t tmp, notcovered;
6910 int n = (i + j) % MAX_NUMNODES;
6911
6912 cpus_complement(notcovered, covered);
6913 cpus_and(tmp, notcovered, *cpu_map);
6914 cpus_and(tmp, tmp, domainspan);
6915 if (cpus_empty(tmp))
6916 break;
6917
6918 nodemask = node_to_cpumask(n);
6919 cpus_and(tmp, tmp, nodemask);
6920 if (cpus_empty(tmp))
6921 continue;
6922
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006923 sg = kmalloc_node(sizeof(struct sched_group),
6924 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006925 if (!sg) {
6926 printk(KERN_WARNING
6927 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006928 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006929 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006930 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006931 sg->cpumask = tmp;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006932 sg->next = prev->next;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006933 cpus_or(covered, covered, tmp);
6934 prev->next = sg;
6935 prev = sg;
6936 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938#endif
6939
6940 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006941#ifdef CONFIG_SCHED_SMT
6942 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006943 struct sched_domain *sd = &per_cpu(cpu_domains, i);
6944
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006945 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006946 }
6947#endif
6948#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006949 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006950 struct sched_domain *sd = &per_cpu(core_domains, i);
6951
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006952 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006953 }
6954#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006956 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006957 struct sched_domain *sd = &per_cpu(phys_domains, i);
6958
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006959 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 }
6961
John Hawkes9c1cfda2005-09-06 15:18:14 -07006962#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08006963 for (i = 0; i < MAX_NUMNODES; i++)
6964 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006965
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006966 if (sd_allnodes) {
6967 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006968
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006969 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006970 init_numa_sched_groups_power(sg);
6971 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006972#endif
6973
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006975 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 struct sched_domain *sd;
6977#ifdef CONFIG_SCHED_SMT
6978 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006979#elif defined(CONFIG_SCHED_MC)
6980 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981#else
6982 sd = &per_cpu(phys_domains, i);
6983#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01006984 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006986
6987 return 0;
6988
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006989#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006990error:
6991 free_sched_groups(cpu_map);
6992 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006993#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994}
Paul Jackson029190c2007-10-18 23:40:20 -07006995
6996static cpumask_t *doms_cur; /* current sched domains */
6997static int ndoms_cur; /* number of sched domains in 'doms_cur' */
6998
6999/*
7000 * Special case: If a kmalloc of a doms_cur partition (array of
7001 * cpumask_t) fails, then fallback to a single sched domain,
7002 * as determined by the single cpumask_t fallback_doms.
7003 */
7004static cpumask_t fallback_doms;
7005
Heiko Carstens22e52b02008-03-12 18:31:59 +01007006void __attribute__((weak)) arch_update_cpu_topology(void)
7007{
7008}
7009
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007010/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007011 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007012 * For now this just excludes isolated cpus, but could be used to
7013 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007014 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007015static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007016{
Milton Miller73785472007-10-24 18:23:48 +02007017 int err;
7018
Heiko Carstens22e52b02008-03-12 18:31:59 +01007019 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007020 ndoms_cur = 1;
7021 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7022 if (!doms_cur)
7023 doms_cur = &fallback_doms;
7024 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Milton Miller73785472007-10-24 18:23:48 +02007025 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007026 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007027
7028 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007029}
7030
7031static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032{
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007033 free_sched_groups(cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007034}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007036/*
7037 * Detach sched domains from a group of cpus specified in cpu_map
7038 * These cpus will now be attached to the NULL domain
7039 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007040static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007041{
7042 int i;
7043
Milton Miller6382bc92007-10-15 17:00:19 +02007044 unregister_sched_domain_sysctl();
7045
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007046 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007047 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007048 synchronize_sched();
7049 arch_destroy_sched_domains(cpu_map);
7050}
7051
Paul Jackson029190c2007-10-18 23:40:20 -07007052/*
7053 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007054 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007055 * doms_new[] to the current sched domain partitioning, doms_cur[].
7056 * It destroys each deleted domain and builds each new domain.
7057 *
7058 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007059 * The masks don't intersect (don't overlap.) We should setup one
7060 * sched domain for each mask. CPUs not in any of the cpumasks will
7061 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007062 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7063 * it as it is.
7064 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007065 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7066 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007067 * failed the kmalloc call, then it can pass in doms_new == NULL,
7068 * and partition_sched_domains() will fallback to the single partition
7069 * 'fallback_doms'.
7070 *
7071 * Call with hotplug lock held
7072 */
7073void partition_sched_domains(int ndoms_new, cpumask_t *doms_new)
7074{
7075 int i, j;
7076
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007077 lock_doms_cur();
7078
Milton Miller73785472007-10-24 18:23:48 +02007079 /* always unregister in case we don't destroy any domains */
7080 unregister_sched_domain_sysctl();
7081
Paul Jackson029190c2007-10-18 23:40:20 -07007082 if (doms_new == NULL) {
7083 ndoms_new = 1;
7084 doms_new = &fallback_doms;
7085 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
7086 }
7087
7088 /* Destroy deleted domains */
7089 for (i = 0; i < ndoms_cur; i++) {
7090 for (j = 0; j < ndoms_new; j++) {
7091 if (cpus_equal(doms_cur[i], doms_new[j]))
7092 goto match1;
7093 }
7094 /* no match - a current sched domain not in new doms_new[] */
7095 detach_destroy_domains(doms_cur + i);
7096match1:
7097 ;
7098 }
7099
7100 /* Build new domains */
7101 for (i = 0; i < ndoms_new; i++) {
7102 for (j = 0; j < ndoms_cur; j++) {
7103 if (cpus_equal(doms_new[i], doms_cur[j]))
7104 goto match2;
7105 }
7106 /* no match - add a new doms_new */
7107 build_sched_domains(doms_new + i);
7108match2:
7109 ;
7110 }
7111
7112 /* Remember the new sched domains */
7113 if (doms_cur != &fallback_doms)
7114 kfree(doms_cur);
7115 doms_cur = doms_new;
7116 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007117
7118 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007119
7120 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007121}
7122
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007123#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007124int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007125{
7126 int err;
7127
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007128 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007129 detach_destroy_domains(&cpu_online_map);
7130 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007131 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007132
7133 return err;
7134}
7135
7136static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7137{
7138 int ret;
7139
7140 if (buf[0] != '0' && buf[0] != '1')
7141 return -EINVAL;
7142
7143 if (smt)
7144 sched_smt_power_savings = (buf[0] == '1');
7145 else
7146 sched_mc_power_savings = (buf[0] == '1');
7147
7148 ret = arch_reinit_sched_domains();
7149
7150 return ret ? ret : count;
7151}
7152
Adrian Bunk6707de002007-08-12 18:08:19 +02007153#ifdef CONFIG_SCHED_MC
7154static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7155{
7156 return sprintf(page, "%u\n", sched_mc_power_savings);
7157}
7158static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7159 const char *buf, size_t count)
7160{
7161 return sched_power_savings_store(buf, count, 0);
7162}
7163static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7164 sched_mc_power_savings_store);
7165#endif
7166
7167#ifdef CONFIG_SCHED_SMT
7168static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7169{
7170 return sprintf(page, "%u\n", sched_smt_power_savings);
7171}
7172static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7173 const char *buf, size_t count)
7174{
7175 return sched_power_savings_store(buf, count, 1);
7176}
7177static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7178 sched_smt_power_savings_store);
7179#endif
7180
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007181int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7182{
7183 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007184
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007185#ifdef CONFIG_SCHED_SMT
7186 if (smt_capable())
7187 err = sysfs_create_file(&cls->kset.kobj,
7188 &attr_sched_smt_power_savings.attr);
7189#endif
7190#ifdef CONFIG_SCHED_MC
7191 if (!err && mc_capable())
7192 err = sysfs_create_file(&cls->kset.kobj,
7193 &attr_sched_mc_power_savings.attr);
7194#endif
7195 return err;
7196}
7197#endif
7198
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007200 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007202 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 * which will prevent rebalancing while the sched domains are recalculated.
7204 */
7205static int update_sched_domains(struct notifier_block *nfb,
7206 unsigned long action, void *hcpu)
7207{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 switch (action) {
7209 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007210 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007212 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007213 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 return NOTIFY_OK;
7215
7216 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007217 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007219 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007221 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007223 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 /*
7225 * Fall through and re-initialise the domains.
7226 */
7227 break;
7228 default:
7229 return NOTIFY_DONE;
7230 }
7231
7232 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007233 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
7235 return NOTIFY_OK;
7236}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237
7238void __init sched_init_smp(void)
7239{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007240 cpumask_t non_isolated_cpus;
7241
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007242 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007243 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007244 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007245 if (cpus_empty(non_isolated_cpus))
7246 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007247 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 /* XXX: Theoretical race here - CPU may be hotplugged now */
7249 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007250
7251 /* Move init over to a non-isolated CPU */
7252 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
7253 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007254 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255}
7256#else
7257void __init sched_init_smp(void)
7258{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007259 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260}
7261#endif /* CONFIG_SMP */
7262
7263int in_sched_functions(unsigned long addr)
7264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 return in_lock_functions(addr) ||
7266 (addr >= (unsigned long)__sched_text_start
7267 && addr < (unsigned long)__sched_text_end);
7268}
7269
Alexey Dobriyana9957442007-10-15 17:00:13 +02007270static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007271{
7272 cfs_rq->tasks_timeline = RB_ROOT;
Ingo Molnardd41f592007-07-09 18:51:59 +02007273#ifdef CONFIG_FAIR_GROUP_SCHED
7274 cfs_rq->rq = rq;
7275#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007276 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007277}
7278
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007279static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7280{
7281 struct rt_prio_array *array;
7282 int i;
7283
7284 array = &rt_rq->active;
7285 for (i = 0; i < MAX_RT_PRIO; i++) {
7286 INIT_LIST_HEAD(array->queue + i);
7287 __clear_bit(i, array->bitmap);
7288 }
7289 /* delimiter for bitsearch: */
7290 __set_bit(MAX_RT_PRIO, array->bitmap);
7291
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007292#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007293 rt_rq->highest_prio = MAX_RT_PRIO;
7294#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007295#ifdef CONFIG_SMP
7296 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007297 rt_rq->overloaded = 0;
7298#endif
7299
7300 rt_rq->rt_time = 0;
7301 rt_rq->rt_throttled = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007302
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007303#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007304 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007305 rt_rq->rq = rq;
7306#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007307}
7308
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007309#ifdef CONFIG_FAIR_GROUP_SCHED
7310static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg,
7311 struct cfs_rq *cfs_rq, struct sched_entity *se,
7312 int cpu, int add)
7313{
7314 tg->cfs_rq[cpu] = cfs_rq;
7315 init_cfs_rq(cfs_rq, rq);
7316 cfs_rq->tg = tg;
7317 if (add)
7318 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7319
7320 tg->se[cpu] = se;
7321 se->cfs_rq = &rq->cfs;
7322 se->my_q = cfs_rq;
7323 se->load.weight = tg->shares;
7324 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
7325 se->parent = NULL;
7326}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007327#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007328
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007329#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007330static void init_tg_rt_entry(struct rq *rq, struct task_group *tg,
7331 struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
7332 int cpu, int add)
7333{
7334 tg->rt_rq[cpu] = rt_rq;
7335 init_rt_rq(rt_rq, rq);
7336 rt_rq->tg = tg;
7337 rt_rq->rt_se = rt_se;
7338 if (add)
7339 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7340
7341 tg->rt_se[cpu] = rt_se;
7342 rt_se->rt_rq = &rq->rt;
7343 rt_se->my_q = rt_rq;
7344 rt_se->parent = NULL;
7345 INIT_LIST_HEAD(&rt_se->run_list);
7346}
7347#endif
7348
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349void __init sched_init(void)
7350{
Christoph Lameter476f3532007-05-06 14:48:58 -07007351 int highest_cpu = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007352 int i, j;
7353
Gregory Haskins57d885f2008-01-25 21:08:18 +01007354#ifdef CONFIG_SMP
7355 init_defrootdomain();
7356#endif
7357
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007358 init_rt_bandwidth(&def_rt_bandwidth,
7359 global_rt_period(), global_rt_runtime());
7360
7361#ifdef CONFIG_RT_GROUP_SCHED
7362 init_rt_bandwidth(&init_task_group.rt_bandwidth,
7363 global_rt_period(), global_rt_runtime());
7364#endif
7365
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007366#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007367 list_add(&init_task_group.list, &task_groups);
7368#endif
7369
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007370 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007371 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372
7373 rq = cpu_rq(i);
7374 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07007375 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07007376 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007377 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02007378 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02007379 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007380 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007381#ifdef CONFIG_FAIR_GROUP_SCHED
7382 init_task_group.shares = init_task_group_load;
7383 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
7384 init_tg_cfs_entry(rq, &init_task_group,
7385 &per_cpu(init_cfs_rq, i),
7386 &per_cpu(init_sched_entity, i), i, 1);
7387
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007388#endif
7389#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007390 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
7391 init_tg_rt_entry(rq, &init_task_group,
7392 &per_cpu(init_rt_rq, i),
7393 &per_cpu(init_sched_rt_entity, i), i, 1);
7394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395
Ingo Molnardd41f592007-07-09 18:51:59 +02007396 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7397 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007399 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007400 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007402 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007404 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 rq->migration_thread = NULL;
7406 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007407 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007409 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 atomic_set(&rq->nr_iowait, 0);
Christoph Lameter476f3532007-05-06 14:48:58 -07007411 highest_cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 }
7413
Peter Williams2dd73a42006-06-27 02:54:34 -07007414 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007415
Avi Kivitye107be32007-07-26 13:40:43 +02007416#ifdef CONFIG_PREEMPT_NOTIFIERS
7417 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7418#endif
7419
Christoph Lameterc9819f42006-12-10 02:20:25 -08007420#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07007421 nr_cpu_ids = highest_cpu + 1;
Christoph Lameterc9819f42006-12-10 02:20:25 -08007422 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
7423#endif
7424
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007425#ifdef CONFIG_RT_MUTEXES
7426 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
7427#endif
7428
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 /*
7430 * The boot idle thread does lazy MMU switching as well:
7431 */
7432 atomic_inc(&init_mm.mm_count);
7433 enter_lazy_tlb(&init_mm, current);
7434
7435 /*
7436 * Make us the idle thread. Technically, schedule() should not be
7437 * called from this thread, however somewhere below it might be,
7438 * but because we are the idle thread, we just pick up running again
7439 * when this runqueue becomes "idle".
7440 */
7441 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02007442 /*
7443 * During early bootup we pretend to be a normal task:
7444 */
7445 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01007446
7447 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448}
7449
7450#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
7451void __might_sleep(char *file, int line)
7452{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007453#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 static unsigned long prev_jiffy; /* ratelimiting */
7455
7456 if ((in_atomic() || irqs_disabled()) &&
7457 system_state == SYSTEM_RUNNING && !oops_in_progress) {
7458 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7459 return;
7460 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08007461 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 " context at %s:%d\n", file, line);
7463 printk("in_atomic():%d, irqs_disabled():%d\n",
7464 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08007465 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08007466 if (irqs_disabled())
7467 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 dump_stack();
7469 }
7470#endif
7471}
7472EXPORT_SYMBOL(__might_sleep);
7473#endif
7474
7475#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007476static void normalize_task(struct rq *rq, struct task_struct *p)
7477{
7478 int on_rq;
7479 update_rq_clock(rq);
7480 on_rq = p->se.on_rq;
7481 if (on_rq)
7482 deactivate_task(rq, p, 0);
7483 __setscheduler(rq, p, SCHED_NORMAL, 0);
7484 if (on_rq) {
7485 activate_task(rq, p, 0);
7486 resched_task(rq->curr);
7487 }
7488}
7489
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490void normalize_rt_tasks(void)
7491{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007492 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007494 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007496 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007497 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007498 /*
7499 * Only normalize user tasks:
7500 */
7501 if (!p->mm)
7502 continue;
7503
Ingo Molnardd41f592007-07-09 18:51:59 +02007504 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007505#ifdef CONFIG_SCHEDSTATS
7506 p->se.wait_start = 0;
7507 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007508 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007509#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007510 task_rq(p)->clock = 0;
7511
7512 if (!rt_task(p)) {
7513 /*
7514 * Renice negative nice level userspace
7515 * tasks back to 0:
7516 */
7517 if (TASK_NICE(p) < 0 && p->mm)
7518 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007522 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007523 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524
Ingo Molnar178be792007-10-15 17:00:18 +02007525 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007526
Ingo Molnarb29739f2006-06-27 02:54:51 -07007527 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007528 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007529 } while_each_thread(g, p);
7530
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007531 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532}
7533
7534#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007535
7536#ifdef CONFIG_IA64
7537/*
7538 * These functions are only useful for the IA64 MCA handling.
7539 *
7540 * They can only be called when the whole system has been
7541 * stopped - every CPU needs to be quiescent, and no scheduling
7542 * activity can take place. Using them for anything else would
7543 * be a serious bug, and as a result, they aren't even visible
7544 * under any other configuration.
7545 */
7546
7547/**
7548 * curr_task - return the current task for a given cpu.
7549 * @cpu: the processor in question.
7550 *
7551 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7552 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007553struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007554{
7555 return cpu_curr(cpu);
7556}
7557
7558/**
7559 * set_curr_task - set the current task for a given cpu.
7560 * @cpu: the processor in question.
7561 * @p: the task pointer to set.
7562 *
7563 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007564 * are serviced on a separate stack. It allows the architecture to switch the
7565 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007566 * must be called with all CPU's synchronized, and interrupts disabled, the
7567 * and caller must save the original value of the current task (see
7568 * curr_task() above) and restore that value before reenabling interrupts and
7569 * re-starting the system.
7570 *
7571 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7572 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007573void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007574{
7575 cpu_curr(cpu) = p;
7576}
7577
7578#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007579
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007580#ifdef CONFIG_FAIR_GROUP_SCHED
7581static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007582{
7583 int i;
7584
7585 for_each_possible_cpu(i) {
7586 if (tg->cfs_rq)
7587 kfree(tg->cfs_rq[i]);
7588 if (tg->se)
7589 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007590 }
7591
7592 kfree(tg->cfs_rq);
7593 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007594}
7595
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007596static int alloc_fair_sched_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007597{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007598 struct cfs_rq *cfs_rq;
7599 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007600 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007601 int i;
7602
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007603 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007604 if (!tg->cfs_rq)
7605 goto err;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007606 tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007607 if (!tg->se)
7608 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007609
7610 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007611
7612 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007613 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007614
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007615 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
7616 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007617 if (!cfs_rq)
7618 goto err;
7619
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007620 se = kmalloc_node(sizeof(struct sched_entity),
7621 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007622 if (!se)
7623 goto err;
7624
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007625 init_tg_cfs_entry(rq, tg, cfs_rq, se, i, 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007626 }
7627
7628 return 1;
7629
7630 err:
7631 return 0;
7632}
7633
7634static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7635{
7636 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
7637 &cpu_rq(cpu)->leaf_cfs_rq_list);
7638}
7639
7640static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7641{
7642 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
7643}
7644#else
7645static inline void free_fair_sched_group(struct task_group *tg)
7646{
7647}
7648
7649static inline int alloc_fair_sched_group(struct task_group *tg)
7650{
7651 return 1;
7652}
7653
7654static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7655{
7656}
7657
7658static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7659{
7660}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007661#endif
7662
7663#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007664static void free_rt_sched_group(struct task_group *tg)
7665{
7666 int i;
7667
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007668 destroy_rt_bandwidth(&tg->rt_bandwidth);
7669
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007670 for_each_possible_cpu(i) {
7671 if (tg->rt_rq)
7672 kfree(tg->rt_rq[i]);
7673 if (tg->rt_se)
7674 kfree(tg->rt_se[i]);
7675 }
7676
7677 kfree(tg->rt_rq);
7678 kfree(tg->rt_se);
7679}
7680
7681static int alloc_rt_sched_group(struct task_group *tg)
7682{
7683 struct rt_rq *rt_rq;
7684 struct sched_rt_entity *rt_se;
7685 struct rq *rq;
7686 int i;
7687
7688 tg->rt_rq = kzalloc(sizeof(rt_rq) * NR_CPUS, GFP_KERNEL);
7689 if (!tg->rt_rq)
7690 goto err;
7691 tg->rt_se = kzalloc(sizeof(rt_se) * NR_CPUS, GFP_KERNEL);
7692 if (!tg->rt_se)
7693 goto err;
7694
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007695 init_rt_bandwidth(&tg->rt_bandwidth,
7696 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007697
7698 for_each_possible_cpu(i) {
7699 rq = cpu_rq(i);
7700
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007701 rt_rq = kmalloc_node(sizeof(struct rt_rq),
7702 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7703 if (!rt_rq)
7704 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007705
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007706 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
7707 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7708 if (!rt_se)
7709 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007710
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007711 init_tg_rt_entry(rq, tg, rt_rq, rt_se, i, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007712 }
7713
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007714 return 1;
7715
7716 err:
7717 return 0;
7718}
7719
7720static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7721{
7722 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
7723 &cpu_rq(cpu)->leaf_rt_rq_list);
7724}
7725
7726static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7727{
7728 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
7729}
7730#else
7731static inline void free_rt_sched_group(struct task_group *tg)
7732{
7733}
7734
7735static inline int alloc_rt_sched_group(struct task_group *tg)
7736{
7737 return 1;
7738}
7739
7740static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7741{
7742}
7743
7744static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7745{
7746}
7747#endif
7748
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007749#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007750static void free_sched_group(struct task_group *tg)
7751{
7752 free_fair_sched_group(tg);
7753 free_rt_sched_group(tg);
7754 kfree(tg);
7755}
7756
7757/* allocate runqueue etc for a new task group */
7758struct task_group *sched_create_group(void)
7759{
7760 struct task_group *tg;
7761 unsigned long flags;
7762 int i;
7763
7764 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7765 if (!tg)
7766 return ERR_PTR(-ENOMEM);
7767
7768 if (!alloc_fair_sched_group(tg))
7769 goto err;
7770
7771 if (!alloc_rt_sched_group(tg))
7772 goto err;
7773
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007774 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007775 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007776 register_fair_sched_group(tg, i);
7777 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007778 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007779 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007780 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007781
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007782 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007783
7784err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007785 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007786 return ERR_PTR(-ENOMEM);
7787}
7788
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007789/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007790static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007791{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007792 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007793 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007794}
7795
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007796/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007797void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007798{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007799 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007800 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007801
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007802 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007803 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007804 unregister_fair_sched_group(tg, i);
7805 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007806 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007807 list_del_rcu(&tg->list);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007808 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007809
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007810 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007811 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007812}
7813
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007814/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007815 * The caller of this function should have put the task in its new group
7816 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7817 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007818 */
7819void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007820{
7821 int on_rq, running;
7822 unsigned long flags;
7823 struct rq *rq;
7824
7825 rq = task_rq_lock(tsk, &flags);
7826
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007827 update_rq_clock(rq);
7828
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007829 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007830 on_rq = tsk->se.on_rq;
7831
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007832 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007833 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007834 if (unlikely(running))
7835 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007836
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007837 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007838
Peter Zijlstra810b3812008-02-29 15:21:01 -05007839#ifdef CONFIG_FAIR_GROUP_SCHED
7840 if (tsk->sched_class->moved_group)
7841 tsk->sched_class->moved_group(tsk);
7842#endif
7843
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007844 if (unlikely(running))
7845 tsk->sched_class->set_curr_task(rq);
7846 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02007847 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007848
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007849 task_rq_unlock(rq, &flags);
7850}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007851#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007852
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007853#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007854static void set_se_shares(struct sched_entity *se, unsigned long shares)
7855{
7856 struct cfs_rq *cfs_rq = se->cfs_rq;
7857 struct rq *rq = cfs_rq->rq;
7858 int on_rq;
7859
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007860 spin_lock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007861
7862 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007863 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007864 dequeue_entity(cfs_rq, se, 0);
7865
7866 se->load.weight = shares;
7867 se->load.inv_weight = div64_64((1ULL<<32), shares);
7868
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007869 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007870 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007871
7872 spin_unlock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007873}
7874
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007875static DEFINE_MUTEX(shares_mutex);
7876
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007877int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007878{
7879 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007880 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01007881
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007882 /*
7883 * A weight of 0 or 1 can cause arithmetics problems.
7884 * (The default weight is 1024 - so there's no practical
7885 * limitation from this.)
7886 */
7887 if (shares < 2)
7888 shares = 2;
7889
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007890 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007891 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007892 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007893
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007894 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007895 for_each_possible_cpu(i)
7896 unregister_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007897 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007898
7899 /* wait for any ongoing reference to this group to finish */
7900 synchronize_sched();
7901
7902 /*
7903 * Now we are free to modify the group's share on each cpu
7904 * w/o tripping rebalance_share or load_balance_fair.
7905 */
7906 tg->shares = shares;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007907 for_each_possible_cpu(i)
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007908 set_se_shares(tg->se[i], shares);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007909
7910 /*
7911 * Enable load balance activity on this group, by inserting it back on
7912 * each cpu's rq->leaf_cfs_rq_list.
7913 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007914 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007915 for_each_possible_cpu(i)
7916 register_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007917 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007918done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007919 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007920 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007921}
7922
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007923unsigned long sched_group_shares(struct task_group *tg)
7924{
7925 return tg->shares;
7926}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007927#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007928
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007929#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007930/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007931 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007932 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007933static DEFINE_MUTEX(rt_constraints_mutex);
7934
7935static unsigned long to_ratio(u64 period, u64 runtime)
7936{
7937 if (runtime == RUNTIME_INF)
7938 return 1ULL << 16;
7939
Peter Zijlstra2692a242008-02-27 12:00:46 +01007940 return div64_64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007941}
7942
7943static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007944{
7945 struct task_group *tgi;
7946 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007947 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007948 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007949
7950 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007951 list_for_each_entry_rcu(tgi, &task_groups, list) {
7952 if (tgi == tg)
7953 continue;
7954
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007955 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
7956 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007957 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007958 rcu_read_unlock();
7959
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007960 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007961}
7962
Dhaval Giani521f1a242008-02-28 15:21:56 +05307963/* Must be called with tasklist_lock held */
7964static inline int tg_has_rt_tasks(struct task_group *tg)
7965{
7966 struct task_struct *g, *p;
7967 do_each_thread(g, p) {
7968 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
7969 return 1;
7970 } while_each_thread(g, p);
7971 return 0;
7972}
7973
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007974static int tg_set_bandwidth(struct task_group *tg,
7975 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007976{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007977 int err = 0;
7978
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007979 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05307980 read_lock(&tasklist_lock);
7981 if (rt_runtime_us == 0 && tg_has_rt_tasks(tg)) {
7982 err = -EBUSY;
7983 goto unlock;
7984 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007985 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
7986 err = -EINVAL;
7987 goto unlock;
7988 }
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007989 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
7990 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007991 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05307992 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007993 mutex_unlock(&rt_constraints_mutex);
7994
7995 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007996}
7997
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007998int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
7999{
8000 u64 rt_runtime, rt_period;
8001
8002 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8003 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8004 if (rt_runtime_us < 0)
8005 rt_runtime = RUNTIME_INF;
8006
8007 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8008}
8009
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008010long sched_group_rt_runtime(struct task_group *tg)
8011{
8012 u64 rt_runtime_us;
8013
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008014 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008015 return -1;
8016
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008017 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008018 do_div(rt_runtime_us, NSEC_PER_USEC);
8019 return rt_runtime_us;
8020}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008021
8022int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8023{
8024 u64 rt_runtime, rt_period;
8025
8026 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8027 rt_runtime = tg->rt_bandwidth.rt_runtime;
8028
8029 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8030}
8031
8032long sched_group_rt_period(struct task_group *tg)
8033{
8034 u64 rt_period_us;
8035
8036 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8037 do_div(rt_period_us, NSEC_PER_USEC);
8038 return rt_period_us;
8039}
8040
8041static int sched_rt_global_constraints(void)
8042{
8043 int ret = 0;
8044
8045 mutex_lock(&rt_constraints_mutex);
8046 if (!__rt_schedulable(NULL, 1, 0))
8047 ret = -EINVAL;
8048 mutex_unlock(&rt_constraints_mutex);
8049
8050 return ret;
8051}
8052#else
8053static int sched_rt_global_constraints(void)
8054{
8055 return 0;
8056}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008057#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008058
8059int sched_rt_handler(struct ctl_table *table, int write,
8060 struct file *filp, void __user *buffer, size_t *lenp,
8061 loff_t *ppos)
8062{
8063 int ret;
8064 int old_period, old_runtime;
8065 static DEFINE_MUTEX(mutex);
8066
8067 mutex_lock(&mutex);
8068 old_period = sysctl_sched_rt_period;
8069 old_runtime = sysctl_sched_rt_runtime;
8070
8071 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8072
8073 if (!ret && write) {
8074 ret = sched_rt_global_constraints();
8075 if (ret) {
8076 sysctl_sched_rt_period = old_period;
8077 sysctl_sched_rt_runtime = old_runtime;
8078 } else {
8079 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8080 def_rt_bandwidth.rt_period =
8081 ns_to_ktime(global_rt_period());
8082 }
8083 }
8084 mutex_unlock(&mutex);
8085
8086 return ret;
8087}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008088
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008089#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008090
8091/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008092static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008093{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008094 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8095 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008096}
8097
8098static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008099cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008100{
8101 struct task_group *tg;
8102
Paul Menage2b01dfe2007-10-24 18:23:50 +02008103 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008104 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008105 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008106 return &init_task_group.css;
8107 }
8108
8109 /* we support only 1-level deep hierarchical scheduler atm */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008110 if (cgrp->parent->parent)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008111 return ERR_PTR(-EINVAL);
8112
8113 tg = sched_create_group();
8114 if (IS_ERR(tg))
8115 return ERR_PTR(-ENOMEM);
8116
8117 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008118 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008119
8120 return &tg->css;
8121}
8122
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008123static void
8124cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008125{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008126 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008127
8128 sched_destroy_group(tg);
8129}
8130
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008131static int
8132cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8133 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008134{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008135#ifdef CONFIG_RT_GROUP_SCHED
8136 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008137 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008138 return -EINVAL;
8139#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008140 /* We don't support RT-tasks being in separate groups */
8141 if (tsk->sched_class != &fair_sched_class)
8142 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008143#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008144
8145 return 0;
8146}
8147
8148static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008149cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008150 struct cgroup *old_cont, struct task_struct *tsk)
8151{
8152 sched_move_task(tsk);
8153}
8154
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008155#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menage2b01dfe2007-10-24 18:23:50 +02008156static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8157 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008158{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008159 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008160}
8161
Paul Menage2b01dfe2007-10-24 18:23:50 +02008162static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008163{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008164 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008165
8166 return (u64) tg->shares;
8167}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008168#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008169
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008170#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008171static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
8172 struct file *file,
8173 const char __user *userbuf,
8174 size_t nbytes, loff_t *unused_ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008175{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008176 char buffer[64];
8177 int retval = 0;
8178 s64 val;
8179 char *end;
8180
8181 if (!nbytes)
8182 return -EINVAL;
8183 if (nbytes >= sizeof(buffer))
8184 return -E2BIG;
8185 if (copy_from_user(buffer, userbuf, nbytes))
8186 return -EFAULT;
8187
8188 buffer[nbytes] = 0; /* nul-terminate */
8189
8190 /* strip newline if necessary */
8191 if (nbytes && (buffer[nbytes-1] == '\n'))
8192 buffer[nbytes-1] = 0;
8193 val = simple_strtoll(buffer, &end, 0);
8194 if (*end)
8195 return -EINVAL;
8196
8197 /* Pass to subsystem */
8198 retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
8199 if (!retval)
8200 retval = nbytes;
8201 return retval;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008202}
8203
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008204static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
8205 struct file *file,
8206 char __user *buf, size_t nbytes,
8207 loff_t *ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008208{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008209 char tmp[64];
8210 long val = sched_group_rt_runtime(cgroup_tg(cgrp));
8211 int len = sprintf(tmp, "%ld\n", val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008212
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008213 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008214}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008215
8216static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8217 u64 rt_period_us)
8218{
8219 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
8220}
8221
8222static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
8223{
8224 return sched_group_rt_period(cgroup_tg(cgrp));
8225}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008226#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008227
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008228static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008229#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008230 {
8231 .name = "shares",
8232 .read_uint = cpu_shares_read_uint,
8233 .write_uint = cpu_shares_write_uint,
8234 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008235#endif
8236#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008237 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008238 .name = "rt_runtime_us",
8239 .read = cpu_rt_runtime_read,
8240 .write = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008241 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008242 {
8243 .name = "rt_period_us",
8244 .read_uint = cpu_rt_period_read_uint,
8245 .write_uint = cpu_rt_period_write_uint,
8246 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008247#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008248};
8249
8250static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8251{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008252 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008253}
8254
8255struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008256 .name = "cpu",
8257 .create = cpu_cgroup_create,
8258 .destroy = cpu_cgroup_destroy,
8259 .can_attach = cpu_cgroup_can_attach,
8260 .attach = cpu_cgroup_attach,
8261 .populate = cpu_cgroup_populate,
8262 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008263 .early_init = 1,
8264};
8265
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008266#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008267
8268#ifdef CONFIG_CGROUP_CPUACCT
8269
8270/*
8271 * CPU accounting code for task groups.
8272 *
8273 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8274 * (balbir@in.ibm.com).
8275 */
8276
8277/* track cpu usage of a group of tasks */
8278struct cpuacct {
8279 struct cgroup_subsys_state css;
8280 /* cpuusage holds pointer to a u64-type object on every cpu */
8281 u64 *cpuusage;
8282};
8283
8284struct cgroup_subsys cpuacct_subsys;
8285
8286/* return cpu accounting group corresponding to this container */
8287static inline struct cpuacct *cgroup_ca(struct cgroup *cont)
8288{
8289 return container_of(cgroup_subsys_state(cont, cpuacct_subsys_id),
8290 struct cpuacct, css);
8291}
8292
8293/* return cpu accounting group to which this task belongs */
8294static inline struct cpuacct *task_ca(struct task_struct *tsk)
8295{
8296 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8297 struct cpuacct, css);
8298}
8299
8300/* create a new cpu accounting group */
8301static struct cgroup_subsys_state *cpuacct_create(
8302 struct cgroup_subsys *ss, struct cgroup *cont)
8303{
8304 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
8305
8306 if (!ca)
8307 return ERR_PTR(-ENOMEM);
8308
8309 ca->cpuusage = alloc_percpu(u64);
8310 if (!ca->cpuusage) {
8311 kfree(ca);
8312 return ERR_PTR(-ENOMEM);
8313 }
8314
8315 return &ca->css;
8316}
8317
8318/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008319static void
8320cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008321{
8322 struct cpuacct *ca = cgroup_ca(cont);
8323
8324 free_percpu(ca->cpuusage);
8325 kfree(ca);
8326}
8327
8328/* return total cpu usage (in nanoseconds) of a group */
8329static u64 cpuusage_read(struct cgroup *cont, struct cftype *cft)
8330{
8331 struct cpuacct *ca = cgroup_ca(cont);
8332 u64 totalcpuusage = 0;
8333 int i;
8334
8335 for_each_possible_cpu(i) {
8336 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
8337
8338 /*
8339 * Take rq->lock to make 64-bit addition safe on 32-bit
8340 * platforms.
8341 */
8342 spin_lock_irq(&cpu_rq(i)->lock);
8343 totalcpuusage += *cpuusage;
8344 spin_unlock_irq(&cpu_rq(i)->lock);
8345 }
8346
8347 return totalcpuusage;
8348}
8349
8350static struct cftype files[] = {
8351 {
8352 .name = "usage",
8353 .read_uint = cpuusage_read,
8354 },
8355};
8356
8357static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8358{
8359 return cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
8360}
8361
8362/*
8363 * charge this task's execution time to its accounting group.
8364 *
8365 * called with rq->lock held.
8366 */
8367static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
8368{
8369 struct cpuacct *ca;
8370
8371 if (!cpuacct_subsys.active)
8372 return;
8373
8374 ca = task_ca(tsk);
8375 if (ca) {
8376 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
8377
8378 *cpuusage += cputime;
8379 }
8380}
8381
8382struct cgroup_subsys cpuacct_subsys = {
8383 .name = "cpuacct",
8384 .create = cpuacct_create,
8385 .destroy = cpuacct_destroy,
8386 .populate = cpuacct_populate,
8387 .subsys_id = cpuacct_subsys_id,
8388};
8389#endif /* CONFIG_CGROUP_CPUACCT */