blob: 01d3e51b7116142d4b3165213eeeacb30378c864 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/sched.c
3 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
35#include <linux/smp_lock.h>
36#include <asm/mmu_context.h>
37#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/completion.h>
40#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070041#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
57#include <linux/kthread.h>
58#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020059#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/syscalls.h>
61#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070062#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080063#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070064#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070065#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020066#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020067#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010068#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070069#include <linux/tick.h>
Mike Travis434d53b2008-04-04 18:11:04 -070070#include <linux/bootmem.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020071#include <linux/debugfs.h>
72#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Eric Dumazet5517d862007-05-08 00:32:57 -070074#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020075#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Gregory Haskins6e0534f2008-05-12 21:21:01 +020077#include "sched_cpupri.h"
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * Convert user-nice values [ -20 ... 0 ... 19 ]
81 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
82 * and back.
83 */
84#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
85#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
86#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
87
88/*
89 * 'User priority' is the nice value converted to something we
90 * can work with better when scaling various scheduler parameters,
91 * it's a [ 0 ... 39 ] range.
92 */
93#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
94#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
95#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
96
97/*
Ingo Molnard7876a02008-01-25 21:08:19 +010098 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100100#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200102#define NICE_0_LOAD SCHED_LOAD_SCALE
103#define NICE_0_SHIFT SCHED_LOAD_SHIFT
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * These are the 'tuning knobs' of the scheduler:
107 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200108 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * Timeslices get refilled after they expire.
110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700112
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200113/*
114 * single value that denotes runtime == period, ie unlimited time.
115 */
116#define RUNTIME_INF ((u64)~0ULL)
117
Eric Dumazet5517d862007-05-08 00:32:57 -0700118#ifdef CONFIG_SMP
119/*
120 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
121 * Since cpu_power is a 'constant', we can use a reciprocal divide.
122 */
123static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
124{
125 return reciprocal_divide(load, sg->reciprocal_cpu_power);
126}
127
128/*
129 * Each time a sched group cpu_power is changed,
130 * we must compute its reciprocal value
131 */
132static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
133{
134 sg->__cpu_power += val;
135 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
136}
137#endif
138
Ingo Molnare05606d2007-07-09 18:51:59 +0200139static inline int rt_policy(int policy)
140{
Roel Kluin3f33a7c2008-05-13 23:44:11 +0200141 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
Ingo Molnare05606d2007-07-09 18:51:59 +0200142 return 1;
143 return 0;
144}
145
146static inline int task_has_rt_policy(struct task_struct *p)
147{
148 return rt_policy(p->policy);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200152 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200154struct rt_prio_array {
155 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
Dmitry Adamushko20b63312008-06-11 00:58:30 +0200156 struct list_head queue[MAX_RT_PRIO];
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200157};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200159struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100160 /* nests inside the rq lock: */
161 spinlock_t rt_runtime_lock;
162 ktime_t rt_period;
163 u64 rt_runtime;
164 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200165};
166
167static struct rt_bandwidth def_rt_bandwidth;
168
169static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
170
171static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
172{
173 struct rt_bandwidth *rt_b =
174 container_of(timer, struct rt_bandwidth, rt_period_timer);
175 ktime_t now;
176 int overrun;
177 int idle = 0;
178
179 for (;;) {
180 now = hrtimer_cb_get_time(timer);
181 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
182
183 if (!overrun)
184 break;
185
186 idle = do_sched_rt_period_timer(rt_b, overrun);
187 }
188
189 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
190}
191
192static
193void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
194{
195 rt_b->rt_period = ns_to_ktime(period);
196 rt_b->rt_runtime = runtime;
197
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200198 spin_lock_init(&rt_b->rt_runtime_lock);
199
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200200 hrtimer_init(&rt_b->rt_period_timer,
201 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
202 rt_b->rt_period_timer.function = sched_rt_period_timer;
203 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
204}
205
206static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
207{
208 ktime_t now;
209
210 if (rt_b->rt_runtime == RUNTIME_INF)
211 return;
212
213 if (hrtimer_active(&rt_b->rt_period_timer))
214 return;
215
216 spin_lock(&rt_b->rt_runtime_lock);
217 for (;;) {
218 if (hrtimer_active(&rt_b->rt_period_timer))
219 break;
220
221 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
222 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
223 hrtimer_start(&rt_b->rt_period_timer,
224 rt_b->rt_period_timer.expires,
225 HRTIMER_MODE_ABS);
226 }
227 spin_unlock(&rt_b->rt_runtime_lock);
228}
229
230#ifdef CONFIG_RT_GROUP_SCHED
231static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
232{
233 hrtimer_cancel(&rt_b->rt_period_timer);
234}
235#endif
236
Heiko Carstens712555e2008-04-28 11:33:07 +0200237/*
238 * sched_domains_mutex serializes calls to arch_init_sched_domains,
239 * detach_destroy_domains and partition_sched_domains.
240 */
241static DEFINE_MUTEX(sched_domains_mutex);
242
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100243#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200244
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700245#include <linux/cgroup.h>
246
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200247struct cfs_rq;
248
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100249static LIST_HEAD(task_groups);
250
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200251/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200252struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100253#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700254 struct cgroup_subsys_state css;
255#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100256
257#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200258 /* schedulable entities of this group on each cpu */
259 struct sched_entity **se;
260 /* runqueue "owned" by this group on each cpu */
261 struct cfs_rq **cfs_rq;
262 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100263#endif
264
265#ifdef CONFIG_RT_GROUP_SCHED
266 struct sched_rt_entity **rt_se;
267 struct rt_rq **rt_rq;
268
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200269 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100270#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100271
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100272 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100273 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200274
275 struct task_group *parent;
276 struct list_head siblings;
277 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200278};
279
Dhaval Giani354d60c2008-04-19 19:44:59 +0200280#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200281
282/*
283 * Root task group.
284 * Every UID task group (including init_task_group aka UID-0) will
285 * be a child to this group.
286 */
287struct task_group root_task_group;
288
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100289#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200290/* Default task group's sched entity on each cpu */
291static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
292/* Default task group's cfs_rq on each cpu */
293static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200294#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100295
296#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100297static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
298static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200299#endif /* CONFIG_RT_GROUP_SCHED */
300#else /* !CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200301#define root_task_group init_task_group
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200302#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100303
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100304/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100305 * a task group's cpu shares.
306 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100307static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100308
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100309#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100310#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100311# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200312#else /* !CONFIG_USER_SCHED */
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100313# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200314#endif /* CONFIG_USER_SCHED */
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200315
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800316/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800317 * A weight of 0 or 1 can cause arithmetics problems.
318 * A weight of a cfs_rq is the sum of weights of which entities
319 * are queued on this cfs_rq, so a weight of a entity should not be
320 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800321 * (The default weight is 1024 - so there's no practical
322 * limitation from this.)
323 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200324#define MIN_SHARES 2
Lai Jiangshan2e084782008-06-12 16:42:58 +0800325#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200326
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100327static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100328#endif
329
330/* Default task group.
331 * Every task in system belong to this group at bootup.
332 */
Mike Travis434d53b2008-04-04 18:11:04 -0700333struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200334
335/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200336static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200337{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200338 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200339
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100340#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200341 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100342#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700343 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
344 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200345#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100346 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200347#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200348 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200349}
350
351/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100352static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200353{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100354#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100355 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
356 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100357#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100358
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100359#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100360 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
361 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100362#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200363}
364
365#else
366
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100367static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Peter Zijlstra83378262008-06-27 13:41:37 +0200368static inline struct task_group *task_group(struct task_struct *p)
369{
370 return NULL;
371}
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200372
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100373#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200374
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200375/* CFS-related fields in a runqueue */
376struct cfs_rq {
377 struct load_weight load;
378 unsigned long nr_running;
379
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200380 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200381 u64 min_vruntime;
Peter Zijlstra103638d92008-06-27 13:41:16 +0200382 u64 pair_start;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200383
384 struct rb_root tasks_timeline;
385 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200386
387 struct list_head tasks;
388 struct list_head *balance_iterator;
389
390 /*
391 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200392 * It is set to NULL otherwise (i.e when none are currently running).
393 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100394 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200395
396 unsigned long nr_spread_over;
397
Ingo Molnar62160e3f2007-10-15 17:00:03 +0200398#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200399 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
400
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100401 /*
402 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200403 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
404 * (like users, containers etc.)
405 *
406 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
407 * list is used during load balance.
408 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100409 struct list_head leaf_cfs_rq_list;
410 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200411
412#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200413 /*
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200414 * the part of load.weight contributed by tasks
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200415 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200416 unsigned long task_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200417
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200418 /*
419 * h_load = weight * f(tg)
420 *
421 * Where f(tg) is the recursive weight fraction assigned to
422 * this group.
423 */
424 unsigned long h_load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200425
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200426 /*
427 * this cpu's part of tg->shares
428 */
429 unsigned long shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200430#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200431#endif
432};
433
434/* Real-Time classes' related field in a runqueue: */
435struct rt_rq {
436 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100437 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100438#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100439 int highest_prio; /* highest queued rt task prio */
440#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100441#ifdef CONFIG_SMP
Gregory Haskins73fe6aae2008-01-25 21:08:07 +0100442 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc12008-01-25 21:08:12 +0100443 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100444#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100445 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100446 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200447 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100448 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200449 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100450
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100451#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100452 unsigned long rt_nr_boosted;
453
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100454 struct rq *rq;
455 struct list_head leaf_rt_rq_list;
456 struct task_group *tg;
457 struct sched_rt_entity *rt_se;
458#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200459};
460
Gregory Haskins57d885f2008-01-25 21:08:18 +0100461#ifdef CONFIG_SMP
462
463/*
464 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100465 * variables. Each exclusive cpuset essentially defines an island domain by
466 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100467 * exclusive cpuset is created, we also create and attach a new root-domain
468 * object.
469 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100470 */
471struct root_domain {
472 atomic_t refcount;
473 cpumask_t span;
474 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100475
Ingo Molnar0eab9142008-01-25 21:08:19 +0100476 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100477 * The "RT overload" flag: it gets set if a CPU has more than
478 * one runnable RT task.
479 */
480 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100481 atomic_t rto_count;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200482#ifdef CONFIG_SMP
483 struct cpupri cpupri;
484#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +0100485};
486
Gregory Haskinsdc938522008-01-25 21:08:26 +0100487/*
488 * By default the system creates a single root-domain with all cpus as
489 * members (mimicking the global state we have today).
490 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100491static struct root_domain def_root_domain;
492
493#endif
494
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200495/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * This is the main, per-CPU runqueue data structure.
497 *
498 * Locking rule: those places that want to lock multiple runqueues
499 * (such as the load balancing or the thread migration code), lock
500 * acquire operations must be ordered by ascending &runqueue.
501 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700502struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200503 /* runqueue lock: */
504 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /*
507 * nr_running and cpu_load should be in the same cacheline because
508 * remote CPUs use both these fields when doing load calculation.
509 */
510 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200511 #define CPU_LOAD_IDX_MAX 5
512 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700513 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700514#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200515 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700516 unsigned char in_nohz_recently;
517#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200518 /* capture load from *all* tasks on this cpu: */
519 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200520 unsigned long nr_load_updates;
521 u64 nr_switches;
522
523 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100524 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100525
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200526#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200527 /* list of leaf cfs_rq on this cpu: */
528 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100529#endif
530#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100531 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /*
535 * This is part of a global counter where only the total sum
536 * over all CPUs matters. A task can increase this counter on
537 * one CPU and if it got migrated afterwards it may decrease
538 * it on another CPU. Always updated under the runqueue lock:
539 */
540 unsigned long nr_uninterruptible;
541
Ingo Molnar36c8b582006-07-03 00:25:41 -0700542 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800543 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200545
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200546 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 atomic_t nr_iowait;
549
550#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100551 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct sched_domain *sd;
553
554 /* For active balancing */
555 int active_balance;
556 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200557 /* cpu of this runqueue: */
558 int cpu;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -0400559 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Peter Zijlstraa8a51d52008-06-27 13:41:26 +0200561 unsigned long avg_load_per_task;
562
Ingo Molnar36c8b582006-07-03 00:25:41 -0700563 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 struct list_head migration_queue;
565#endif
566
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100567#ifdef CONFIG_SCHED_HRTICK
568 unsigned long hrtick_flags;
569 ktime_t hrtick_expire;
570 struct hrtimer hrtick_timer;
571#endif
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573#ifdef CONFIG_SCHEDSTATS
574 /* latency stats */
575 struct sched_info rq_sched_info;
576
577 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200578 unsigned int yld_exp_empty;
579 unsigned int yld_act_empty;
580 unsigned int yld_both_empty;
581 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200584 unsigned int sched_switch;
585 unsigned int sched_count;
586 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200589 unsigned int ttwu_count;
590 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200591
592 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200593 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700595 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596};
597
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700598static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Ingo Molnardd41f592007-07-09 18:51:59 +0200600static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
601{
602 rq->curr->sched_class->check_preempt_curr(rq, p);
603}
604
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700605static inline int cpu_of(struct rq *rq)
606{
607#ifdef CONFIG_SMP
608 return rq->cpu;
609#else
610 return 0;
611#endif
612}
613
Ingo Molnar20d315d2007-07-09 18:51:58 +0200614/*
Nick Piggin674311d2005-06-25 14:57:27 -0700615 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700616 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700617 *
618 * The domain tree of any CPU may only be accessed from within
619 * preempt-disabled sections.
620 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700621#define for_each_domain(cpu, __sd) \
622 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
625#define this_rq() (&__get_cpu_var(runqueues))
626#define task_rq(p) cpu_rq(task_cpu(p))
627#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
628
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200629static inline void update_rq_clock(struct rq *rq)
630{
631 rq->clock = sched_clock_cpu(cpu_of(rq));
632}
633
Ingo Molnare436d802007-07-19 21:28:35 +0200634/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200635 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
636 */
637#ifdef CONFIG_SCHED_DEBUG
638# define const_debug __read_mostly
639#else
640# define const_debug static const
641#endif
642
643/*
644 * Debugging: various feature bits
645 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200646
647#define SCHED_FEAT(name, enabled) \
648 __SCHED_FEAT_##name ,
649
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200650enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200651#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200652};
653
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200654#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200655
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200656#define SCHED_FEAT(name, enabled) \
657 (1UL << __SCHED_FEAT_##name) * enabled |
658
659const_debug unsigned int sysctl_sched_features =
660#include "sched_features.h"
661 0;
662
663#undef SCHED_FEAT
664
665#ifdef CONFIG_SCHED_DEBUG
666#define SCHED_FEAT(name, enabled) \
667 #name ,
668
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700669static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200670#include "sched_features.h"
671 NULL
672};
673
674#undef SCHED_FEAT
675
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700676static int sched_feat_open(struct inode *inode, struct file *filp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200677{
678 filp->private_data = inode->i_private;
679 return 0;
680}
681
682static ssize_t
683sched_feat_read(struct file *filp, char __user *ubuf,
684 size_t cnt, loff_t *ppos)
685{
686 char *buf;
687 int r = 0;
688 int len = 0;
689 int i;
690
691 for (i = 0; sched_feat_names[i]; i++) {
692 len += strlen(sched_feat_names[i]);
693 len += 4;
694 }
695
696 buf = kmalloc(len + 2, GFP_KERNEL);
697 if (!buf)
698 return -ENOMEM;
699
700 for (i = 0; sched_feat_names[i]; i++) {
701 if (sysctl_sched_features & (1UL << i))
702 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
703 else
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200704 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200705 }
706
707 r += sprintf(buf + r, "\n");
708 WARN_ON(r >= len + 2);
709
710 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
711
712 kfree(buf);
713
714 return r;
715}
716
717static ssize_t
718sched_feat_write(struct file *filp, const char __user *ubuf,
719 size_t cnt, loff_t *ppos)
720{
721 char buf[64];
722 char *cmp = buf;
723 int neg = 0;
724 int i;
725
726 if (cnt > 63)
727 cnt = 63;
728
729 if (copy_from_user(&buf, ubuf, cnt))
730 return -EFAULT;
731
732 buf[cnt] = 0;
733
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200734 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200735 neg = 1;
736 cmp += 3;
737 }
738
739 for (i = 0; sched_feat_names[i]; i++) {
740 int len = strlen(sched_feat_names[i]);
741
742 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
743 if (neg)
744 sysctl_sched_features &= ~(1UL << i);
745 else
746 sysctl_sched_features |= (1UL << i);
747 break;
748 }
749 }
750
751 if (!sched_feat_names[i])
752 return -EINVAL;
753
754 filp->f_pos += cnt;
755
756 return cnt;
757}
758
759static struct file_operations sched_feat_fops = {
760 .open = sched_feat_open,
761 .read = sched_feat_read,
762 .write = sched_feat_write,
763};
764
765static __init int sched_init_debug(void)
766{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200767 debugfs_create_file("sched_features", 0644, NULL, NULL,
768 &sched_feat_fops);
769
770 return 0;
771}
772late_initcall(sched_init_debug);
773
774#endif
775
776#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200777
778/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100779 * Number of tasks to iterate in a single balance run.
780 * Limited because this is done with IRQs disabled.
781 */
782const_debug unsigned int sysctl_sched_nr_migrate = 32;
783
784/*
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200785 * ratelimit for updating the group shares.
786 * default: 0.5ms
787 */
788const_debug unsigned int sysctl_sched_shares_ratelimit = 500000;
789
790/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100791 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100792 * default: 1s
793 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100794unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100795
Ingo Molnar6892b752008-02-13 14:02:36 +0100796static __read_mostly int scheduler_running;
797
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100798/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100799 * part of the period that we allow rt tasks to run in us.
800 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100801 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100802int sysctl_sched_rt_runtime = 950000;
803
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200804static inline u64 global_rt_period(void)
805{
806 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
807}
808
809static inline u64 global_rt_runtime(void)
810{
811 if (sysctl_sched_rt_period < 0)
812 return RUNTIME_INF;
813
814 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
815}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700818# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700820#ifndef finish_arch_switch
821# define finish_arch_switch(prev) do { } while (0)
822#endif
823
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100824static inline int task_current(struct rq *rq, struct task_struct *p)
825{
826 return rq->curr == p;
827}
828
Nick Piggin4866cde2005-06-25 14:57:23 -0700829#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700830static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700831{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100832 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700833}
834
Ingo Molnar70b97a72006-07-03 00:25:42 -0700835static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700836{
837}
838
Ingo Molnar70b97a72006-07-03 00:25:42 -0700839static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700840{
Ingo Molnarda04c032005-09-13 11:17:59 +0200841#ifdef CONFIG_DEBUG_SPINLOCK
842 /* this is a valid case when another task releases the spinlock */
843 rq->lock.owner = current;
844#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700845 /*
846 * If we are tracking spinlock dependencies then we have to
847 * fix up the runqueue lock - which gets 'carried over' from
848 * prev into current:
849 */
850 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
851
Nick Piggin4866cde2005-06-25 14:57:23 -0700852 spin_unlock_irq(&rq->lock);
853}
854
855#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700856static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700857{
858#ifdef CONFIG_SMP
859 return p->oncpu;
860#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100861 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700862#endif
863}
864
Ingo Molnar70b97a72006-07-03 00:25:42 -0700865static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700866{
867#ifdef CONFIG_SMP
868 /*
869 * We can optimise this out completely for !SMP, because the
870 * SMP rebalancing from interrupt is the only thing that cares
871 * here.
872 */
873 next->oncpu = 1;
874#endif
875#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
876 spin_unlock_irq(&rq->lock);
877#else
878 spin_unlock(&rq->lock);
879#endif
880}
881
Ingo Molnar70b97a72006-07-03 00:25:42 -0700882static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700883{
884#ifdef CONFIG_SMP
885 /*
886 * After ->oncpu is cleared, the task can be moved to a different CPU.
887 * We must ensure this doesn't happen until the switch is completely
888 * finished.
889 */
890 smp_wmb();
891 prev->oncpu = 0;
892#endif
893#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
894 local_irq_enable();
895#endif
896}
897#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700900 * __task_rq_lock - lock the runqueue a given task resides on.
901 * Must be called interrupts disabled.
902 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700903static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700904 __acquires(rq->lock)
905{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200906 for (;;) {
907 struct rq *rq = task_rq(p);
908 spin_lock(&rq->lock);
909 if (likely(rq == task_rq(p)))
910 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700911 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700912 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700913}
914
915/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100917 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * explicitly disabling preemption.
919 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700920static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 __acquires(rq->lock)
922{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700923 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Andi Kleen3a5c3592007-10-15 17:00:14 +0200925 for (;;) {
926 local_irq_save(*flags);
927 rq = task_rq(p);
928 spin_lock(&rq->lock);
929 if (likely(rq == task_rq(p)))
930 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Alexey Dobriyana9957442007-10-15 17:00:13 +0200935static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700936 __releases(rq->lock)
937{
938 spin_unlock(&rq->lock);
939}
940
Ingo Molnar70b97a72006-07-03 00:25:42 -0700941static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 __releases(rq->lock)
943{
944 spin_unlock_irqrestore(&rq->lock, *flags);
945}
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800948 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200950static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 __acquires(rq->lock)
952{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700953 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 local_irq_disable();
956 rq = this_rq();
957 spin_lock(&rq->lock);
958
959 return rq;
960}
961
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100962static void __resched_task(struct task_struct *p, int tif_bit);
963
964static inline void resched_task(struct task_struct *p)
965{
966 __resched_task(p, TIF_NEED_RESCHED);
967}
968
969#ifdef CONFIG_SCHED_HRTICK
970/*
971 * Use HR-timers to deliver accurate preemption points.
972 *
973 * Its all a bit involved since we cannot program an hrt while holding the
974 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
975 * reschedule event.
976 *
977 * When we get rescheduled we reprogram the hrtick_timer outside of the
978 * rq->lock.
979 */
980static inline void resched_hrt(struct task_struct *p)
981{
982 __resched_task(p, TIF_HRTICK_RESCHED);
983}
984
985static inline void resched_rq(struct rq *rq)
986{
987 unsigned long flags;
988
989 spin_lock_irqsave(&rq->lock, flags);
990 resched_task(rq->curr);
991 spin_unlock_irqrestore(&rq->lock, flags);
992}
993
994enum {
995 HRTICK_SET, /* re-programm hrtick_timer */
996 HRTICK_RESET, /* not a new slice */
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200997 HRTICK_BLOCK, /* stop hrtick operations */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100998};
999
1000/*
1001 * Use hrtick when:
1002 * - enabled by features
1003 * - hrtimer is actually high res
1004 */
1005static inline int hrtick_enabled(struct rq *rq)
1006{
1007 if (!sched_feat(HRTICK))
1008 return 0;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001009 if (unlikely(test_bit(HRTICK_BLOCK, &rq->hrtick_flags)))
1010 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001011 return hrtimer_is_hres_active(&rq->hrtick_timer);
1012}
1013
1014/*
1015 * Called to set the hrtick timer state.
1016 *
1017 * called with rq->lock held and irqs disabled
1018 */
1019static void hrtick_start(struct rq *rq, u64 delay, int reset)
1020{
1021 assert_spin_locked(&rq->lock);
1022
1023 /*
1024 * preempt at: now + delay
1025 */
1026 rq->hrtick_expire =
1027 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1028 /*
1029 * indicate we need to program the timer
1030 */
1031 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1032 if (reset)
1033 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1034
1035 /*
1036 * New slices are called from the schedule path and don't need a
1037 * forced reschedule.
1038 */
1039 if (reset)
1040 resched_hrt(rq->curr);
1041}
1042
1043static void hrtick_clear(struct rq *rq)
1044{
1045 if (hrtimer_active(&rq->hrtick_timer))
1046 hrtimer_cancel(&rq->hrtick_timer);
1047}
1048
1049/*
1050 * Update the timer from the possible pending state.
1051 */
1052static void hrtick_set(struct rq *rq)
1053{
1054 ktime_t time;
1055 int set, reset;
1056 unsigned long flags;
1057
1058 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1059
1060 spin_lock_irqsave(&rq->lock, flags);
1061 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1062 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1063 time = rq->hrtick_expire;
1064 clear_thread_flag(TIF_HRTICK_RESCHED);
1065 spin_unlock_irqrestore(&rq->lock, flags);
1066
1067 if (set) {
1068 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1069 if (reset && !hrtimer_active(&rq->hrtick_timer))
1070 resched_rq(rq);
1071 } else
1072 hrtick_clear(rq);
1073}
1074
1075/*
1076 * High-resolution timer tick.
1077 * Runs from hardirq context with interrupts disabled.
1078 */
1079static enum hrtimer_restart hrtick(struct hrtimer *timer)
1080{
1081 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1082
1083 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1084
1085 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001086 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001087 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1088 spin_unlock(&rq->lock);
1089
1090 return HRTIMER_NORESTART;
1091}
1092
Rabin Vincent81d41d72008-05-11 05:55:33 +05301093#ifdef CONFIG_SMP
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001094static void hotplug_hrtick_disable(int cpu)
1095{
1096 struct rq *rq = cpu_rq(cpu);
1097 unsigned long flags;
1098
1099 spin_lock_irqsave(&rq->lock, flags);
1100 rq->hrtick_flags = 0;
1101 __set_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1102 spin_unlock_irqrestore(&rq->lock, flags);
1103
1104 hrtick_clear(rq);
1105}
1106
1107static void hotplug_hrtick_enable(int cpu)
1108{
1109 struct rq *rq = cpu_rq(cpu);
1110 unsigned long flags;
1111
1112 spin_lock_irqsave(&rq->lock, flags);
1113 __clear_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1114 spin_unlock_irqrestore(&rq->lock, flags);
1115}
1116
1117static int
1118hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1119{
1120 int cpu = (int)(long)hcpu;
1121
1122 switch (action) {
1123 case CPU_UP_CANCELED:
1124 case CPU_UP_CANCELED_FROZEN:
1125 case CPU_DOWN_PREPARE:
1126 case CPU_DOWN_PREPARE_FROZEN:
1127 case CPU_DEAD:
1128 case CPU_DEAD_FROZEN:
1129 hotplug_hrtick_disable(cpu);
1130 return NOTIFY_OK;
1131
1132 case CPU_UP_PREPARE:
1133 case CPU_UP_PREPARE_FROZEN:
1134 case CPU_DOWN_FAILED:
1135 case CPU_DOWN_FAILED_FROZEN:
1136 case CPU_ONLINE:
1137 case CPU_ONLINE_FROZEN:
1138 hotplug_hrtick_enable(cpu);
1139 return NOTIFY_OK;
1140 }
1141
1142 return NOTIFY_DONE;
1143}
1144
1145static void init_hrtick(void)
1146{
1147 hotcpu_notifier(hotplug_hrtick, 0);
1148}
Rabin Vincent81d41d72008-05-11 05:55:33 +05301149#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001150
1151static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001152{
1153 rq->hrtick_flags = 0;
1154 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1155 rq->hrtick_timer.function = hrtick;
1156 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1157}
1158
1159void hrtick_resched(void)
1160{
1161 struct rq *rq;
1162 unsigned long flags;
1163
1164 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1165 return;
1166
1167 local_irq_save(flags);
1168 rq = cpu_rq(smp_processor_id());
1169 hrtick_set(rq);
1170 local_irq_restore(flags);
1171}
1172#else
1173static inline void hrtick_clear(struct rq *rq)
1174{
1175}
1176
1177static inline void hrtick_set(struct rq *rq)
1178{
1179}
1180
1181static inline void init_rq_hrtick(struct rq *rq)
1182{
1183}
1184
1185void hrtick_resched(void)
1186{
1187}
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001188
1189static inline void init_hrtick(void)
1190{
1191}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001192#endif
1193
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001194/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001195 * resched_task - mark a task 'to be rescheduled now'.
1196 *
1197 * On UP this means the setting of the need_resched flag, on SMP it
1198 * might also involve a cross-CPU call to trigger the scheduler on
1199 * the target CPU.
1200 */
1201#ifdef CONFIG_SMP
1202
1203#ifndef tsk_is_polling
1204#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1205#endif
1206
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001207static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001208{
1209 int cpu;
1210
1211 assert_spin_locked(&task_rq(p)->lock);
1212
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001213 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001214 return;
1215
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001216 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001217
1218 cpu = task_cpu(p);
1219 if (cpu == smp_processor_id())
1220 return;
1221
1222 /* NEED_RESCHED must be visible before we test polling */
1223 smp_mb();
1224 if (!tsk_is_polling(p))
1225 smp_send_reschedule(cpu);
1226}
1227
1228static void resched_cpu(int cpu)
1229{
1230 struct rq *rq = cpu_rq(cpu);
1231 unsigned long flags;
1232
1233 if (!spin_trylock_irqsave(&rq->lock, flags))
1234 return;
1235 resched_task(cpu_curr(cpu));
1236 spin_unlock_irqrestore(&rq->lock, flags);
1237}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001238
1239#ifdef CONFIG_NO_HZ
1240/*
1241 * When add_timer_on() enqueues a timer into the timer wheel of an
1242 * idle CPU then this timer might expire before the next timer event
1243 * which is scheduled to wake up that CPU. In case of a completely
1244 * idle system the next event might even be infinite time into the
1245 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1246 * leaves the inner idle loop so the newly added timer is taken into
1247 * account when the CPU goes back to idle and evaluates the timer
1248 * wheel for the next timer event.
1249 */
1250void wake_up_idle_cpu(int cpu)
1251{
1252 struct rq *rq = cpu_rq(cpu);
1253
1254 if (cpu == smp_processor_id())
1255 return;
1256
1257 /*
1258 * This is safe, as this function is called with the timer
1259 * wheel base lock of (cpu) held. When the CPU is on the way
1260 * to idle and has not yet set rq->curr to idle then it will
1261 * be serialized on the timer wheel base lock and take the new
1262 * timer into account automatically.
1263 */
1264 if (rq->curr != rq->idle)
1265 return;
1266
1267 /*
1268 * We can set TIF_RESCHED on the idle task of the other CPU
1269 * lockless. The worst case is that the other CPU runs the
1270 * idle task through an additional NOOP schedule()
1271 */
1272 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1273
1274 /* NEED_RESCHED must be visible before we test polling */
1275 smp_mb();
1276 if (!tsk_is_polling(rq->idle))
1277 smp_send_reschedule(cpu);
1278}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001279#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001280
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001281#else /* !CONFIG_SMP */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001282static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001283{
1284 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001285 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001286}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001287#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001288
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001289#if BITS_PER_LONG == 32
1290# define WMULT_CONST (~0UL)
1291#else
1292# define WMULT_CONST (1UL << 32)
1293#endif
1294
1295#define WMULT_SHIFT 32
1296
Ingo Molnar194081e2007-08-09 11:16:51 +02001297/*
1298 * Shift right and round:
1299 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001300#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001301
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001302/*
1303 * delta *= weight / lw
1304 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001305static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001306calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1307 struct load_weight *lw)
1308{
1309 u64 tmp;
1310
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001311 if (!lw->inv_weight) {
1312 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1313 lw->inv_weight = 1;
1314 else
1315 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1316 / (lw->weight+1);
1317 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001318
1319 tmp = (u64)delta_exec * weight;
1320 /*
1321 * Check whether we'd overflow the 64-bit multiplication:
1322 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001323 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001324 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001325 WMULT_SHIFT/2);
1326 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001327 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001328
Ingo Molnarecf691d2007-08-02 17:41:40 +02001329 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001330}
1331
Ingo Molnar10919852007-10-15 17:00:04 +02001332static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001333{
1334 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001335 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001336}
1337
Ingo Molnar10919852007-10-15 17:00:04 +02001338static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001339{
1340 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001341 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001342}
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001345 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1346 * of tasks with abnormal "nice" values across CPUs the contribution that
1347 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001348 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001349 * scaled version of the new time slice allocation that they receive on time
1350 * slice expiry etc.
1351 */
1352
Ingo Molnardd41f592007-07-09 18:51:59 +02001353#define WEIGHT_IDLEPRIO 2
1354#define WMULT_IDLEPRIO (1 << 31)
1355
1356/*
1357 * Nice levels are multiplicative, with a gentle 10% change for every
1358 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1359 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1360 * that remained on nice 0.
1361 *
1362 * The "10% effect" is relative and cumulative: from _any_ nice level,
1363 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee62007-07-16 09:46:30 +02001364 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1365 * If a task goes up by ~10% and another task goes down by ~10% then
1366 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001367 */
1368static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001369 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1370 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1371 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1372 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1373 /* 0 */ 1024, 820, 655, 526, 423,
1374 /* 5 */ 335, 272, 215, 172, 137,
1375 /* 10 */ 110, 87, 70, 56, 45,
1376 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001377};
1378
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001379/*
1380 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1381 *
1382 * In cases where the weight does not change often, we can use the
1383 * precalculated inverse to speed up arithmetics by turning divisions
1384 * into multiplications:
1385 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001386static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001387 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1388 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1389 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1390 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1391 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1392 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1393 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1394 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001395};
Peter Williams2dd73a42006-06-27 02:54:34 -07001396
Ingo Molnardd41f592007-07-09 18:51:59 +02001397static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1398
1399/*
1400 * runqueue iterator, to support SMP load-balancing between different
1401 * scheduling classes, without having to expose their internal data
1402 * structures to the load-balancing proper:
1403 */
1404struct rq_iterator {
1405 void *arg;
1406 struct task_struct *(*start)(void *);
1407 struct task_struct *(*next)(void *);
1408};
1409
Peter Williamse1d14842007-10-24 18:23:51 +02001410#ifdef CONFIG_SMP
1411static unsigned long
1412balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1413 unsigned long max_load_move, struct sched_domain *sd,
1414 enum cpu_idle_type idle, int *all_pinned,
1415 int *this_best_prio, struct rq_iterator *iterator);
1416
1417static int
1418iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1419 struct sched_domain *sd, enum cpu_idle_type idle,
1420 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001421#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001422
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001423#ifdef CONFIG_CGROUP_CPUACCT
1424static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1425#else
1426static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1427#endif
1428
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001429static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1430{
1431 update_load_add(&rq->load, load);
1432}
1433
1434static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1435{
1436 update_load_sub(&rq->load, load);
1437}
1438
Gregory Haskinse7693a32008-01-25 21:08:09 +01001439#ifdef CONFIG_SMP
1440static unsigned long source_load(int cpu, int type);
1441static unsigned long target_load(int cpu, int type);
Gregory Haskinse7693a32008-01-25 21:08:09 +01001442static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001443
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001444static unsigned long cpu_avg_load_per_task(int cpu)
1445{
1446 struct rq *rq = cpu_rq(cpu);
1447
1448 if (rq->nr_running)
1449 rq->avg_load_per_task = rq->load.weight / rq->nr_running;
1450
1451 return rq->avg_load_per_task;
1452}
1453
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001454#ifdef CONFIG_FAIR_GROUP_SCHED
1455
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001456typedef void (*tg_visitor)(struct task_group *, int, struct sched_domain *);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001457
1458/*
1459 * Iterate the full tree, calling @down when first entering a node and @up when
1460 * leaving it for the final time.
1461 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001462static void
1463walk_tg_tree(tg_visitor down, tg_visitor up, int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001464{
1465 struct task_group *parent, *child;
1466
1467 rcu_read_lock();
1468 parent = &root_task_group;
1469down:
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001470 (*down)(parent, cpu, sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001471 list_for_each_entry_rcu(child, &parent->children, siblings) {
1472 parent = child;
1473 goto down;
1474
1475up:
1476 continue;
1477 }
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001478 (*up)(parent, cpu, sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001479
1480 child = parent;
1481 parent = parent->parent;
1482 if (parent)
1483 goto up;
1484 rcu_read_unlock();
1485}
1486
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001487static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1488
1489/*
1490 * Calculate and set the cpu's group shares.
1491 */
1492static void
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001493__update_group_shares_cpu(struct task_group *tg, int cpu,
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001494 unsigned long sd_shares, unsigned long sd_rq_weight)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001495{
1496 int boost = 0;
1497 unsigned long shares;
1498 unsigned long rq_weight;
1499
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001500 if (!tg->se[cpu])
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001501 return;
1502
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001503 rq_weight = tg->cfs_rq[cpu]->load.weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001504
1505 /*
1506 * If there are currently no tasks on the cpu pretend there is one of
1507 * average load so that when a new task gets to run here it will not
1508 * get delayed by group starvation.
1509 */
1510 if (!rq_weight) {
1511 boost = 1;
1512 rq_weight = NICE_0_LOAD;
1513 }
1514
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001515 if (unlikely(rq_weight > sd_rq_weight))
1516 rq_weight = sd_rq_weight;
1517
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001518 /*
1519 * \Sum shares * rq_weight
1520 * shares = -----------------------
1521 * \Sum rq_weight
1522 *
1523 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001524 shares = (sd_shares * rq_weight) / (sd_rq_weight + 1);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001525
1526 /*
1527 * record the actual number of shares, not the boosted amount.
1528 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001529 tg->cfs_rq[cpu]->shares = boost ? 0 : shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001530
1531 if (shares < MIN_SHARES)
1532 shares = MIN_SHARES;
1533 else if (shares > MAX_SHARES)
1534 shares = MAX_SHARES;
1535
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001536 __set_se_shares(tg->se[cpu], shares);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001537}
1538
1539/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001540 * Re-compute the task group their per cpu shares over the given domain.
1541 * This needs to be done in a bottom-up fashion because the rq weight of a
1542 * parent group depends on the shares of its child groups.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001543 */
1544static void
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001545tg_shares_up(struct task_group *tg, int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001546{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001547 unsigned long rq_weight = 0;
1548 unsigned long shares = 0;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001549 int i;
1550
1551 for_each_cpu_mask(i, sd->span) {
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001552 rq_weight += tg->cfs_rq[i]->load.weight;
1553 shares += tg->cfs_rq[i]->shares;
1554 }
1555
1556 if ((!shares && rq_weight) || shares > tg->shares)
1557 shares = tg->shares;
1558
1559 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1560 shares = tg->shares;
1561
Peter Zijlstracd809172008-06-27 13:41:34 +02001562 if (!rq_weight)
1563 rq_weight = cpus_weight(sd->span) * NICE_0_LOAD;
1564
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001565 for_each_cpu_mask(i, sd->span) {
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001566 struct rq *rq = cpu_rq(i);
1567 unsigned long flags;
1568
1569 spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001570 __update_group_shares_cpu(tg, i, shares, rq_weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001571 spin_unlock_irqrestore(&rq->lock, flags);
1572 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001573}
1574
1575/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001576 * Compute the cpu's hierarchical load factor for each task group.
1577 * This needs to be done in a top-down fashion because the load of a child
1578 * group is a fraction of its parents load.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001579 */
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001580static void
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001581tg_load_down(struct task_group *tg, int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001582{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001583 unsigned long load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001584
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001585 if (!tg->parent) {
1586 load = cpu_rq(cpu)->load.weight;
1587 } else {
1588 load = tg->parent->cfs_rq[cpu]->h_load;
1589 load *= tg->cfs_rq[cpu]->shares;
1590 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1591 }
1592
1593 tg->cfs_rq[cpu]->h_load = load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001594}
1595
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001596static void
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001597tg_nop(struct task_group *tg, int cpu, struct sched_domain *sd)
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001598{
1599}
1600
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001601static void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001602{
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001603 u64 now = cpu_clock(raw_smp_processor_id());
1604 s64 elapsed = now - sd->last_update;
1605
1606 if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
1607 sd->last_update = now;
1608 walk_tg_tree(tg_nop, tg_shares_up, 0, sd);
1609 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001610}
1611
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001612static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1613{
1614 spin_unlock(&rq->lock);
1615 update_shares(sd);
1616 spin_lock(&rq->lock);
1617}
1618
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001619static void update_h_load(int cpu)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001620{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001621 walk_tg_tree(tg_load_down, tg_nop, cpu, NULL);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001622}
1623
1624static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1625{
1626 cfs_rq->shares = shares;
1627}
1628
1629#else
1630
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001631static inline void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001632{
1633}
1634
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001635static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1636{
1637}
1638
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001639#endif
1640
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001641#endif
1642
Ingo Molnardd41f592007-07-09 18:51:59 +02001643#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001644#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001645#include "sched_fair.c"
1646#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001647#ifdef CONFIG_SCHED_DEBUG
1648# include "sched_debug.c"
1649#endif
1650
1651#define sched_class_highest (&rt_sched_class)
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04001652#define for_each_class(class) \
1653 for (class = sched_class_highest; class; class = class->next)
Ingo Molnardd41f592007-07-09 18:51:59 +02001654
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001655static void inc_nr_running(struct rq *rq)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001656{
1657 rq->nr_running++;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001658}
1659
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001660static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001661{
1662 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001663}
1664
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001665static void set_load_weight(struct task_struct *p)
1666{
1667 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001668 p->se.load.weight = prio_to_weight[0] * 2;
1669 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1670 return;
1671 }
1672
1673 /*
1674 * SCHED_IDLE tasks get minimal weight:
1675 */
1676 if (p->policy == SCHED_IDLE) {
1677 p->se.load.weight = WEIGHT_IDLEPRIO;
1678 p->se.load.inv_weight = WMULT_IDLEPRIO;
1679 return;
1680 }
1681
1682 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1683 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001684}
1685
Ingo Molnar8159f872007-08-09 11:16:49 +02001686static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001687{
1688 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001689 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001690 p->se.on_rq = 1;
1691}
1692
Ingo Molnar69be72c2007-08-09 11:16:49 +02001693static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001694{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001695 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001696 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001697}
1698
1699/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001700 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001701 */
Ingo Molnar14531182007-07-09 18:51:59 +02001702static inline int __normal_prio(struct task_struct *p)
1703{
Ingo Molnardd41f592007-07-09 18:51:59 +02001704 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001705}
1706
1707/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001708 * Calculate the expected normal priority: i.e. priority
1709 * without taking RT-inheritance into account. Might be
1710 * boosted by interactivity modifiers. Changes upon fork,
1711 * setprio syscalls, and whenever the interactivity
1712 * estimator recalculates.
1713 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001714static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001715{
1716 int prio;
1717
Ingo Molnare05606d2007-07-09 18:51:59 +02001718 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001719 prio = MAX_RT_PRIO-1 - p->rt_priority;
1720 else
1721 prio = __normal_prio(p);
1722 return prio;
1723}
1724
1725/*
1726 * Calculate the current priority, i.e. the priority
1727 * taken into account by the scheduler. This value might
1728 * be boosted by RT tasks, or might be boosted by
1729 * interactivity modifiers. Will be RT if the task got
1730 * RT-boosted. If not then it returns p->normal_prio.
1731 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001732static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001733{
1734 p->normal_prio = normal_prio(p);
1735 /*
1736 * If we are RT tasks or we were boosted to RT priority,
1737 * keep the priority unchanged. Otherwise, update priority
1738 * to the normal priority:
1739 */
1740 if (!rt_prio(p->prio))
1741 return p->normal_prio;
1742 return p->prio;
1743}
1744
1745/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001746 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001748static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001750 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001751 rq->nr_uninterruptible--;
1752
Ingo Molnar8159f872007-08-09 11:16:49 +02001753 enqueue_task(rq, p, wakeup);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001754 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
1757/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * deactivate_task - remove a task from the runqueue.
1759 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001760static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001762 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001763 rq->nr_uninterruptible++;
1764
Ingo Molnar69be72c2007-08-09 11:16:49 +02001765 dequeue_task(rq, p, sleep);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001766 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769/**
1770 * task_curr - is this task currently executing on a CPU?
1771 * @p: the task in question.
1772 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001773inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 return cpu_curr(task_cpu(p)) == p;
1776}
1777
Ingo Molnardd41f592007-07-09 18:51:59 +02001778static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1779{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001780 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001781#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001782 /*
1783 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1784 * successfuly executed on another CPU. We must ensure that updates of
1785 * per-task data have been completed by this moment.
1786 */
1787 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001788 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001789#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001790}
1791
Steven Rostedtcb469842008-01-25 21:08:22 +01001792static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1793 const struct sched_class *prev_class,
1794 int oldprio, int running)
1795{
1796 if (prev_class != p->sched_class) {
1797 if (prev_class->switched_from)
1798 prev_class->switched_from(rq, p, running);
1799 p->sched_class->switched_to(rq, p, running);
1800 } else
1801 p->sched_class->prio_changed(rq, p, oldprio, running);
1802}
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001805
Thomas Gleixnere958b362008-06-04 23:22:32 +02001806/* Used instead of source_load when we know the type == 0 */
1807static unsigned long weighted_cpuload(const int cpu)
1808{
1809 return cpu_rq(cpu)->load.weight;
1810}
1811
Ingo Molnarcc367732007-10-15 17:00:18 +02001812/*
1813 * Is this task likely cache-hot:
1814 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001815static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001816task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1817{
1818 s64 delta;
1819
Ingo Molnarf540a602008-03-15 17:10:34 +01001820 /*
1821 * Buddy candidates are cache hot:
1822 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01001823 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01001824 return 1;
1825
Ingo Molnarcc367732007-10-15 17:00:18 +02001826 if (p->sched_class != &fair_sched_class)
1827 return 0;
1828
Ingo Molnar6bc16652007-10-15 17:00:18 +02001829 if (sysctl_sched_migration_cost == -1)
1830 return 1;
1831 if (sysctl_sched_migration_cost == 0)
1832 return 0;
1833
Ingo Molnarcc367732007-10-15 17:00:18 +02001834 delta = now - p->se.exec_start;
1835
1836 return delta < (s64)sysctl_sched_migration_cost;
1837}
1838
1839
Ingo Molnardd41f592007-07-09 18:51:59 +02001840void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001841{
Ingo Molnardd41f592007-07-09 18:51:59 +02001842 int old_cpu = task_cpu(p);
1843 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001844 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1845 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001846 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001847
1848 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001849
1850#ifdef CONFIG_SCHEDSTATS
1851 if (p->se.wait_start)
1852 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001853 if (p->se.sleep_start)
1854 p->se.sleep_start -= clock_offset;
1855 if (p->se.block_start)
1856 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001857 if (old_cpu != new_cpu) {
1858 schedstat_inc(p, se.nr_migrations);
1859 if (task_hot(p, old_rq->clock, NULL))
1860 schedstat_inc(p, se.nr_forced2_migrations);
1861 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001862#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001863 p->se.vruntime -= old_cfsrq->min_vruntime -
1864 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001865
1866 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001867}
1868
Ingo Molnar70b97a72006-07-03 00:25:42 -07001869struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Ingo Molnar36c8b582006-07-03 00:25:41 -07001872 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 int dest_cpu;
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001876};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878/*
1879 * The task's runqueue lock must be held.
1880 * Returns true if you have to wait for migration thread.
1881 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001882static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001883migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001885 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 /*
1888 * If the task is not on a runqueue (and not running), then
1889 * it is sufficient to simply update the task's cpu field.
1890 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001891 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 set_task_cpu(p, dest_cpu);
1893 return 0;
1894 }
1895
1896 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 req->task = p;
1898 req->dest_cpu = dest_cpu;
1899 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 1;
1902}
1903
1904/*
1905 * wait_task_inactive - wait for a thread to unschedule.
1906 *
1907 * The caller must ensure that the task *will* unschedule sometime soon,
1908 * else this function might spin for a *long* time. This function can't
1909 * be called with interrupts off, or it may introduce deadlock with
1910 * smp_call_function() if an IPI is sent by the same process we are
1911 * waiting to become inactive.
1912 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001913void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
1915 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001916 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001917 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Andi Kleen3a5c3592007-10-15 17:00:14 +02001919 for (;;) {
1920 /*
1921 * We do the initial early heuristics without holding
1922 * any task-queue locks at all. We'll only try to get
1923 * the runqueue lock when things look like they will
1924 * work out!
1925 */
1926 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001927
Andi Kleen3a5c3592007-10-15 17:00:14 +02001928 /*
1929 * If the task is actively running on another CPU
1930 * still, just relax and busy-wait without holding
1931 * any locks.
1932 *
1933 * NOTE! Since we don't hold any locks, it's not
1934 * even sure that "rq" stays as the right runqueue!
1935 * But we don't care, since "task_running()" will
1936 * return false if the runqueue has changed and p
1937 * is actually now running somewhere else!
1938 */
1939 while (task_running(rq, p))
1940 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001941
Andi Kleen3a5c3592007-10-15 17:00:14 +02001942 /*
1943 * Ok, time to look more closely! We need the rq
1944 * lock now, to be *sure*. If we're wrong, we'll
1945 * just go back and repeat.
1946 */
1947 rq = task_rq_lock(p, &flags);
1948 running = task_running(rq, p);
1949 on_rq = p->se.on_rq;
1950 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001951
Andi Kleen3a5c3592007-10-15 17:00:14 +02001952 /*
1953 * Was it really running after all now that we
1954 * checked with the proper locks actually held?
1955 *
1956 * Oops. Go back and try again..
1957 */
1958 if (unlikely(running)) {
1959 cpu_relax();
1960 continue;
1961 }
1962
1963 /*
1964 * It's not enough that it's not actively running,
1965 * it must be off the runqueue _entirely_, and not
1966 * preempted!
1967 *
1968 * So if it wa still runnable (but just not actively
1969 * running right now), it's preempted, and we should
1970 * yield - it could be a while.
1971 */
1972 if (unlikely(on_rq)) {
1973 schedule_timeout_uninterruptible(1);
1974 continue;
1975 }
1976
1977 /*
1978 * Ahh, all good. It wasn't running, and it wasn't
1979 * runnable, which means that it will never become
1980 * running in the future either. We're all done!
1981 */
1982 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
1986/***
1987 * kick_process - kick a running thread to enter/exit the kernel
1988 * @p: the to-be-kicked thread
1989 *
1990 * Cause a process which is running on another CPU to enter
1991 * kernel-mode, without any delay. (to get signals handled.)
1992 *
1993 * NOTE: this function doesnt have to take the runqueue lock,
1994 * because all it wants to ensure is that the remote task enters
1995 * the kernel. If the IPI races and the task has been migrated
1996 * to another CPU then no harm is done and the purpose has been
1997 * achieved as well.
1998 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001999void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 int cpu;
2002
2003 preempt_disable();
2004 cpu = task_cpu(p);
2005 if ((cpu != smp_processor_id()) && task_curr(p))
2006 smp_send_reschedule(cpu);
2007 preempt_enable();
2008}
2009
2010/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002011 * Return a low guess at the load of a migration-source cpu weighted
2012 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 *
2014 * We want to under-estimate the load of migration sources, to
2015 * balance conservatively.
2016 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002017static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002018{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002019 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002020 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002021
Peter Zijlstra93b75212008-06-27 13:41:33 +02002022 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002023 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002024
Ingo Molnardd41f592007-07-09 18:51:59 +02002025 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
2028/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002029 * Return a high guess at the load of a migration-target cpu weighted
2030 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002032static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002033{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002034 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002035 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002036
Peter Zijlstra93b75212008-06-27 13:41:33 +02002037 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002038 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002039
Ingo Molnardd41f592007-07-09 18:51:59 +02002040 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002041}
2042
2043/*
Nick Piggin147cbb42005-06-25 14:57:19 -07002044 * find_idlest_group finds and returns the least busy CPU group within the
2045 * domain.
2046 */
2047static struct sched_group *
2048find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2049{
2050 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2051 unsigned long min_load = ULONG_MAX, this_load = 0;
2052 int load_idx = sd->forkexec_idx;
2053 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2054
2055 do {
2056 unsigned long load, avg_load;
2057 int local_group;
2058 int i;
2059
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002060 /* Skip over this group if it has no CPUs allowed */
2061 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002062 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002063
Nick Piggin147cbb42005-06-25 14:57:19 -07002064 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002065
2066 /* Tally up the load of all CPUs in the group */
2067 avg_load = 0;
2068
2069 for_each_cpu_mask(i, group->cpumask) {
2070 /* Bias balancing toward cpus of our domain */
2071 if (local_group)
2072 load = source_load(i, load_idx);
2073 else
2074 load = target_load(i, load_idx);
2075
2076 avg_load += load;
2077 }
2078
2079 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002080 avg_load = sg_div_cpu_power(group,
2081 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002082
2083 if (local_group) {
2084 this_load = avg_load;
2085 this = group;
2086 } else if (avg_load < min_load) {
2087 min_load = avg_load;
2088 idlest = group;
2089 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002090 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002091
2092 if (!idlest || 100*this_load < imbalance*min_load)
2093 return NULL;
2094 return idlest;
2095}
2096
2097/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002098 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002099 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002100static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002101find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2102 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002103{
2104 unsigned long load, min_load = ULONG_MAX;
2105 int idlest = -1;
2106 int i;
2107
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002108 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002109 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002110
Mike Travis7c16ec52008-04-04 18:11:11 -07002111 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002112 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002113
2114 if (load < min_load || (load == min_load && i == this_cpu)) {
2115 min_load = load;
2116 idlest = i;
2117 }
2118 }
2119
2120 return idlest;
2121}
2122
Nick Piggin476d1392005-06-25 14:57:29 -07002123/*
2124 * sched_balance_self: balance the current task (running on cpu) in domains
2125 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2126 * SD_BALANCE_EXEC.
2127 *
2128 * Balance, ie. select the least loaded group.
2129 *
2130 * Returns the target CPU number, or the same CPU if no balancing is needed.
2131 *
2132 * preempt must be disabled.
2133 */
2134static int sched_balance_self(int cpu, int flag)
2135{
2136 struct task_struct *t = current;
2137 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002138
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002139 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002140 /*
2141 * If power savings logic is enabled for a domain, stop there.
2142 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002143 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2144 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002145 if (tmp->flags & flag)
2146 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002147 }
Nick Piggin476d1392005-06-25 14:57:29 -07002148
Peter Zijlstra039a1c412008-06-27 13:41:25 +02002149 if (sd)
2150 update_shares(sd);
2151
Nick Piggin476d1392005-06-25 14:57:29 -07002152 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002153 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002154 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002155 int new_cpu, weight;
2156
2157 if (!(sd->flags & flag)) {
2158 sd = sd->child;
2159 continue;
2160 }
Nick Piggin476d1392005-06-25 14:57:29 -07002161
2162 span = sd->span;
2163 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002164 if (!group) {
2165 sd = sd->child;
2166 continue;
2167 }
Nick Piggin476d1392005-06-25 14:57:29 -07002168
Mike Travis7c16ec52008-04-04 18:11:11 -07002169 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002170 if (new_cpu == -1 || new_cpu == cpu) {
2171 /* Now try balancing at a lower domain level of cpu */
2172 sd = sd->child;
2173 continue;
2174 }
Nick Piggin476d1392005-06-25 14:57:29 -07002175
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002176 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002177 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002178 sd = NULL;
2179 weight = cpus_weight(span);
2180 for_each_domain(cpu, tmp) {
2181 if (weight <= cpus_weight(tmp->span))
2182 break;
2183 if (tmp->flags & flag)
2184 sd = tmp;
2185 }
2186 /* while loop will break here if sd == NULL */
2187 }
2188
2189 return cpu;
2190}
2191
2192#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194/***
2195 * try_to_wake_up - wake up a thread
2196 * @p: the to-be-woken-up thread
2197 * @state: the mask of task states that can be woken
2198 * @sync: do a synchronous wakeup?
2199 *
2200 * Put it on the run-queue if it's not already there. The "current"
2201 * thread is always on the run-queue (except when the actual
2202 * re-schedule is in progress), and as such you're allowed to do
2203 * the simpler "current->state = TASK_RUNNING" to mark yourself
2204 * runnable without the overhead of this.
2205 *
2206 * returns failure only if the task is already active.
2207 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002208static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Ingo Molnarcc367732007-10-15 17:00:18 +02002210 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 unsigned long flags;
2212 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002213 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Ingo Molnarb85d0662008-03-16 20:03:22 +01002215 if (!sched_feat(SYNC_WAKEUPS))
2216 sync = 0;
2217
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002218#ifdef CONFIG_SMP
2219 if (sched_feat(LB_WAKEUP_UPDATE)) {
2220 struct sched_domain *sd;
2221
2222 this_cpu = raw_smp_processor_id();
2223 cpu = task_cpu(p);
2224
2225 for_each_domain(this_cpu, sd) {
2226 if (cpu_isset(cpu, sd->span)) {
2227 update_shares(sd);
2228 break;
2229 }
2230 }
2231 }
2232#endif
2233
Linus Torvalds04e2f172008-02-23 18:05:03 -08002234 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 rq = task_rq_lock(p, &flags);
2236 old_state = p->state;
2237 if (!(old_state & state))
2238 goto out;
2239
Ingo Molnardd41f592007-07-09 18:51:59 +02002240 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 goto out_running;
2242
2243 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002244 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 this_cpu = smp_processor_id();
2246
2247#ifdef CONFIG_SMP
2248 if (unlikely(task_running(rq, p)))
2249 goto out_activate;
2250
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002251 cpu = p->sched_class->select_task_rq(p, sync);
2252 if (cpu != orig_cpu) {
2253 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 task_rq_unlock(rq, &flags);
2255 /* might preempt at this point */
2256 rq = task_rq_lock(p, &flags);
2257 old_state = p->state;
2258 if (!(old_state & state))
2259 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002260 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 goto out_running;
2262
2263 this_cpu = smp_processor_id();
2264 cpu = task_cpu(p);
2265 }
2266
Gregory Haskinse7693a32008-01-25 21:08:09 +01002267#ifdef CONFIG_SCHEDSTATS
2268 schedstat_inc(rq, ttwu_count);
2269 if (cpu == this_cpu)
2270 schedstat_inc(rq, ttwu_local);
2271 else {
2272 struct sched_domain *sd;
2273 for_each_domain(this_cpu, sd) {
2274 if (cpu_isset(cpu, sd->span)) {
2275 schedstat_inc(sd, ttwu_wake_remote);
2276 break;
2277 }
2278 }
2279 }
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002280#endif /* CONFIG_SCHEDSTATS */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282out_activate:
2283#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002284 schedstat_inc(p, se.nr_wakeups);
2285 if (sync)
2286 schedstat_inc(p, se.nr_wakeups_sync);
2287 if (orig_cpu != cpu)
2288 schedstat_inc(p, se.nr_wakeups_migrate);
2289 if (cpu == this_cpu)
2290 schedstat_inc(p, se.nr_wakeups_local);
2291 else
2292 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002293 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002294 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 success = 1;
2296
2297out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002298 check_preempt_curr(rq, p);
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002301#ifdef CONFIG_SMP
2302 if (p->sched_class->task_wake_up)
2303 p->sched_class->task_wake_up(rq, p);
2304#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305out:
2306 task_rq_unlock(rq, &flags);
2307
2308 return success;
2309}
2310
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002311int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002313 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315EXPORT_SYMBOL(wake_up_process);
2316
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002317int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
2319 return try_to_wake_up(p, state, 0);
2320}
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322/*
2323 * Perform scheduler related setup for a newly forked process p.
2324 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002325 *
2326 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002328static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
Ingo Molnardd41f592007-07-09 18:51:59 +02002330 p->se.exec_start = 0;
2331 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002332 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002333 p->se.last_wakeup = 0;
2334 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002335
2336#ifdef CONFIG_SCHEDSTATS
2337 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002338 p->se.sum_sleep_runtime = 0;
2339 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002340 p->se.block_start = 0;
2341 p->se.sleep_max = 0;
2342 p->se.block_max = 0;
2343 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002344 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002345 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002346#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002347
Peter Zijlstrafa717062008-01-25 21:08:27 +01002348 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002349 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002350 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002351
Avi Kivitye107be32007-07-26 13:40:43 +02002352#ifdef CONFIG_PREEMPT_NOTIFIERS
2353 INIT_HLIST_HEAD(&p->preempt_notifiers);
2354#endif
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 /*
2357 * We mark the process as running here, but have not actually
2358 * inserted it onto the runqueue yet. This guarantees that
2359 * nobody will actually run it, and a signal or other external
2360 * event cannot wake it up and insert it on the runqueue either.
2361 */
2362 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002363}
2364
2365/*
2366 * fork()/clone()-time setup:
2367 */
2368void sched_fork(struct task_struct *p, int clone_flags)
2369{
2370 int cpu = get_cpu();
2371
2372 __sched_fork(p);
2373
2374#ifdef CONFIG_SMP
2375 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2376#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002377 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002378
2379 /*
2380 * Make sure we do not leak PI boosting priority to the child:
2381 */
2382 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002383 if (!rt_prio(p->prio))
2384 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002385
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002386#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002387 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002388 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002390#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002391 p->oncpu = 0;
2392#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002394 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002395 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002397 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
2400/*
2401 * wake_up_new_task - wake up a newly created task for the first time.
2402 *
2403 * This function will do some initial scheduler statistics housekeeping
2404 * that must be done for every newly created context, then puts the task
2405 * on the runqueue and wakes it.
2406 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002407void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
2409 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002410 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002414 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 p->prio = effective_prio(p);
2417
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002418 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002419 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002422 * Let the scheduling class do new task startup
2423 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002425 p->sched_class->task_new(rq, p);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002426 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002428 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002429#ifdef CONFIG_SMP
2430 if (p->sched_class->task_wake_up)
2431 p->sched_class->task_wake_up(rq, p);
2432#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002433 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Avi Kivitye107be32007-07-26 13:40:43 +02002436#ifdef CONFIG_PREEMPT_NOTIFIERS
2437
2438/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002439 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2440 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002441 */
2442void preempt_notifier_register(struct preempt_notifier *notifier)
2443{
2444 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2445}
2446EXPORT_SYMBOL_GPL(preempt_notifier_register);
2447
2448/**
2449 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002450 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002451 *
2452 * This is safe to call from within a preemption notifier.
2453 */
2454void preempt_notifier_unregister(struct preempt_notifier *notifier)
2455{
2456 hlist_del(&notifier->link);
2457}
2458EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2459
2460static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2461{
2462 struct preempt_notifier *notifier;
2463 struct hlist_node *node;
2464
2465 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2466 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2467}
2468
2469static void
2470fire_sched_out_preempt_notifiers(struct task_struct *curr,
2471 struct task_struct *next)
2472{
2473 struct preempt_notifier *notifier;
2474 struct hlist_node *node;
2475
2476 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2477 notifier->ops->sched_out(notifier, next);
2478}
2479
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002480#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002481
2482static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2483{
2484}
2485
2486static void
2487fire_sched_out_preempt_notifiers(struct task_struct *curr,
2488 struct task_struct *next)
2489{
2490}
2491
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002492#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002495 * prepare_task_switch - prepare to switch tasks
2496 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002497 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002498 * @next: the task we are going to switch to.
2499 *
2500 * This is called with the rq lock held and interrupts off. It must
2501 * be paired with a subsequent finish_task_switch after the context
2502 * switch.
2503 *
2504 * prepare_task_switch sets up locking and calls architecture specific
2505 * hooks.
2506 */
Avi Kivitye107be32007-07-26 13:40:43 +02002507static inline void
2508prepare_task_switch(struct rq *rq, struct task_struct *prev,
2509 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002510{
Avi Kivitye107be32007-07-26 13:40:43 +02002511 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002512 prepare_lock_switch(rq, next);
2513 prepare_arch_switch(next);
2514}
2515
2516/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002518 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 * @prev: the thread we just switched away from.
2520 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002521 * finish_task_switch must be called after the context switch, paired
2522 * with a prepare_task_switch call before the context switch.
2523 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2524 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 *
2526 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002527 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 * with the lock held can cause deadlocks; see schedule() for
2529 * details.)
2530 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002531static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 __releases(rq->lock)
2533{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002535 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 rq->prev_mm = NULL;
2538
2539 /*
2540 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002541 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002542 * schedule one last time. The schedule call will never return, and
2543 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002544 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 * still held, otherwise prev could be scheduled on another cpu, die
2546 * there before we look at prev->state, and then the reference would
2547 * be dropped twice.
2548 * Manfred Spraul <manfred@colorfullife.com>
2549 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002550 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002551 finish_arch_switch(prev);
2552 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002553#ifdef CONFIG_SMP
2554 if (current->sched_class->post_schedule)
2555 current->sched_class->post_schedule(rq);
2556#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002557
Avi Kivitye107be32007-07-26 13:40:43 +02002558 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (mm)
2560 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002561 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002562 /*
2563 * Remove function-return probe instances associated with this
2564 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002565 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002566 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569}
2570
2571/**
2572 * schedule_tail - first thing a freshly forked thread must call.
2573 * @prev: the thread we just switched away from.
2574 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002575asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 __releases(rq->lock)
2577{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002578 struct rq *rq = this_rq();
2579
Nick Piggin4866cde2005-06-25 14:57:23 -07002580 finish_task_switch(rq, prev);
2581#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2582 /* In this case, finish_task_switch does not reenable preemption */
2583 preempt_enable();
2584#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002586 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
2589/*
2590 * context_switch - switch to the new MM and the new
2591 * thread's register state.
2592 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002593static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002594context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002595 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
Ingo Molnardd41f592007-07-09 18:51:59 +02002597 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Avi Kivitye107be32007-07-26 13:40:43 +02002599 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002600 mm = next->mm;
2601 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002602 /*
2603 * For paravirt, this is coupled with an exit in switch_to to
2604 * combine the page table reload and the switch backend into
2605 * one hypercall.
2606 */
2607 arch_enter_lazy_cpu_mode();
2608
Ingo Molnardd41f592007-07-09 18:51:59 +02002609 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 next->active_mm = oldmm;
2611 atomic_inc(&oldmm->mm_count);
2612 enter_lazy_tlb(oldmm, next);
2613 } else
2614 switch_mm(oldmm, mm, next);
2615
Ingo Molnardd41f592007-07-09 18:51:59 +02002616 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 rq->prev_mm = oldmm;
2619 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002620 /*
2621 * Since the runqueue lock will be released by the next
2622 * task (which is an invalid locking op but in the case
2623 * of the scheduler it's an obvious special-case), so we
2624 * do an early lockdep release here:
2625 */
2626#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002627 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002628#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 /* Here we just switch the register state and the stack. */
2631 switch_to(prev, next, prev);
2632
Ingo Molnardd41f592007-07-09 18:51:59 +02002633 barrier();
2634 /*
2635 * this_rq must be evaluated again because prev may have moved
2636 * CPUs since it called schedule(), thus the 'rq' on its stack
2637 * frame will be invalid.
2638 */
2639 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641
2642/*
2643 * nr_running, nr_uninterruptible and nr_context_switches:
2644 *
2645 * externally visible scheduler statistics: current number of runnable
2646 * threads, current number of uninterruptible-sleeping threads, total
2647 * number of context switches performed since bootup.
2648 */
2649unsigned long nr_running(void)
2650{
2651 unsigned long i, sum = 0;
2652
2653 for_each_online_cpu(i)
2654 sum += cpu_rq(i)->nr_running;
2655
2656 return sum;
2657}
2658
2659unsigned long nr_uninterruptible(void)
2660{
2661 unsigned long i, sum = 0;
2662
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002663 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 sum += cpu_rq(i)->nr_uninterruptible;
2665
2666 /*
2667 * Since we read the counters lockless, it might be slightly
2668 * inaccurate. Do not allow it to go below zero though:
2669 */
2670 if (unlikely((long)sum < 0))
2671 sum = 0;
2672
2673 return sum;
2674}
2675
2676unsigned long long nr_context_switches(void)
2677{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002678 int i;
2679 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002681 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 sum += cpu_rq(i)->nr_switches;
2683
2684 return sum;
2685}
2686
2687unsigned long nr_iowait(void)
2688{
2689 unsigned long i, sum = 0;
2690
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002691 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2693
2694 return sum;
2695}
2696
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002697unsigned long nr_active(void)
2698{
2699 unsigned long i, running = 0, uninterruptible = 0;
2700
2701 for_each_online_cpu(i) {
2702 running += cpu_rq(i)->nr_running;
2703 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2704 }
2705
2706 if (unlikely((long)uninterruptible < 0))
2707 uninterruptible = 0;
2708
2709 return running + uninterruptible;
2710}
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002713 * Update rq->cpu_load[] statistics. This function is usually called every
2714 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002715 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002716static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002717{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002718 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002719 int i, scale;
2720
2721 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002722
2723 /* Update our load: */
2724 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2725 unsigned long old_load, new_load;
2726
2727 /* scale is effectively 1 << i now, and >> i divides by scale */
2728
2729 old_load = this_rq->cpu_load[i];
2730 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002731 /*
2732 * Round up the averaging division if load is increasing. This
2733 * prevents us from getting stuck on 9 if the load is 10, for
2734 * example.
2735 */
2736 if (new_load > old_load)
2737 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002738 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2739 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002740}
2741
Ingo Molnardd41f592007-07-09 18:51:59 +02002742#ifdef CONFIG_SMP
2743
Ingo Molnar48f24c42006-07-03 00:25:40 -07002744/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 * double_rq_lock - safely lock two runqueues
2746 *
2747 * Note this does not disable interrupts like task_rq_lock,
2748 * you need to do so manually before calling.
2749 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002750static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 __acquires(rq1->lock)
2752 __acquires(rq2->lock)
2753{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002754 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if (rq1 == rq2) {
2756 spin_lock(&rq1->lock);
2757 __acquire(rq2->lock); /* Fake it out ;) */
2758 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002759 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 spin_lock(&rq1->lock);
2761 spin_lock(&rq2->lock);
2762 } else {
2763 spin_lock(&rq2->lock);
2764 spin_lock(&rq1->lock);
2765 }
2766 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002767 update_rq_clock(rq1);
2768 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
2771/*
2772 * double_rq_unlock - safely unlock two runqueues
2773 *
2774 * Note this does not restore interrupts like task_rq_unlock,
2775 * you need to do so manually after calling.
2776 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002777static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 __releases(rq1->lock)
2779 __releases(rq2->lock)
2780{
2781 spin_unlock(&rq1->lock);
2782 if (rq1 != rq2)
2783 spin_unlock(&rq2->lock);
2784 else
2785 __release(rq2->lock);
2786}
2787
2788/*
2789 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2790 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002791static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 __releases(this_rq->lock)
2793 __acquires(busiest->lock)
2794 __acquires(this_rq->lock)
2795{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002796 int ret = 0;
2797
Kirill Korotaev054b9102006-12-10 02:20:11 -08002798 if (unlikely(!irqs_disabled())) {
2799 /* printk() doesn't work good under rq->lock */
2800 spin_unlock(&this_rq->lock);
2801 BUG_ON(1);
2802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002804 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 spin_unlock(&this_rq->lock);
2806 spin_lock(&busiest->lock);
2807 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002808 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 } else
2810 spin_lock(&busiest->lock);
2811 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002812 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813}
2814
2815/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 * If dest_cpu is allowed for this process, migrate the task to it.
2817 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002818 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 * the cpu_allowed mask is restored.
2820 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002821static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002823 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002825 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 rq = task_rq_lock(p, &flags);
2828 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2829 || unlikely(cpu_is_offline(dest_cpu)))
2830 goto out;
2831
2832 /* force the process onto the specified CPU */
2833 if (migrate_task(p, dest_cpu, &req)) {
2834 /* Need to wait for migration thread (might exit: take ref). */
2835 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 get_task_struct(mt);
2838 task_rq_unlock(rq, &flags);
2839 wake_up_process(mt);
2840 put_task_struct(mt);
2841 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return;
2844 }
2845out:
2846 task_rq_unlock(rq, &flags);
2847}
2848
2849/*
Nick Piggin476d1392005-06-25 14:57:29 -07002850 * sched_exec - execve() is a valuable balancing opportunity, because at
2851 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 */
2853void sched_exec(void)
2854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002856 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002858 if (new_cpu != this_cpu)
2859 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
2862/*
2863 * pull_task - move a task from a remote runqueue to the local runqueue.
2864 * Both runqueues must be locked.
2865 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002866static void pull_task(struct rq *src_rq, struct task_struct *p,
2867 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002869 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002871 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 /*
2873 * Note that idle threads have a prio of MAX_PRIO, for this test
2874 * to be always true for them.
2875 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002876 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877}
2878
2879/*
2880 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2881 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002882static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002883int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002884 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002885 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
2887 /*
2888 * We do not migrate tasks that are:
2889 * 1) running (obviously), or
2890 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2891 * 3) are cache-hot on their current CPU.
2892 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002893 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2894 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002896 }
Nick Piggin81026792005-06-25 14:57:07 -07002897 *all_pinned = 0;
2898
Ingo Molnarcc367732007-10-15 17:00:18 +02002899 if (task_running(rq, p)) {
2900 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002901 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Ingo Molnarda84d962007-10-15 17:00:18 +02002904 /*
2905 * Aggressive migration if:
2906 * 1) task is cache cold, or
2907 * 2) too many balance attempts have failed.
2908 */
2909
Ingo Molnar6bc16652007-10-15 17:00:18 +02002910 if (!task_hot(p, rq->clock, sd) ||
2911 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002912#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002913 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002914 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002915 schedstat_inc(p, se.nr_forced_migrations);
2916 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002917#endif
2918 return 1;
2919 }
2920
Ingo Molnarcc367732007-10-15 17:00:18 +02002921 if (task_hot(p, rq->clock, sd)) {
2922 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002923 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return 1;
2926}
2927
Peter Williamse1d14842007-10-24 18:23:51 +02002928static unsigned long
2929balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2930 unsigned long max_load_move, struct sched_domain *sd,
2931 enum cpu_idle_type idle, int *all_pinned,
2932 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002933{
Peter Zijlstra051c6762008-06-27 13:41:31 +02002934 int loops = 0, pulled = 0, pinned = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002935 struct task_struct *p;
2936 long rem_load_move = max_load_move;
2937
Peter Williamse1d14842007-10-24 18:23:51 +02002938 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002939 goto out;
2940
2941 pinned = 1;
2942
2943 /*
2944 * Start the load-balancing iterator:
2945 */
2946 p = iterator->start(iterator->arg);
2947next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002948 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002949 goto out;
Peter Zijlstra051c6762008-06-27 13:41:31 +02002950
2951 if ((p->se.load.weight >> 1) > rem_load_move ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002952 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002953 p = iterator->next(iterator->arg);
2954 goto next;
2955 }
2956
2957 pull_task(busiest, p, this_rq, this_cpu);
2958 pulled++;
2959 rem_load_move -= p->se.load.weight;
2960
2961 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002962 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002963 */
Peter Williamse1d14842007-10-24 18:23:51 +02002964 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002965 if (p->prio < *this_best_prio)
2966 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002967 p = iterator->next(iterator->arg);
2968 goto next;
2969 }
2970out:
2971 /*
Peter Williamse1d14842007-10-24 18:23:51 +02002972 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02002973 * so we can safely collect pull_task() stats here rather than
2974 * inside pull_task().
2975 */
2976 schedstat_add(sd, lb_gained[idle], pulled);
2977
2978 if (all_pinned)
2979 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02002980
2981 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02002982}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984/*
Peter Williams43010652007-08-09 11:16:46 +02002985 * move_tasks tries to move up to max_load_move weighted load from busiest to
2986 * this_rq, as part of a balancing operation within domain "sd".
2987 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 *
2989 * Called with both runqueues locked.
2990 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002991static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002992 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002993 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002994 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002996 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002997 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002998 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Ingo Molnardd41f592007-07-09 18:51:59 +02003000 do {
Peter Williams43010652007-08-09 11:16:46 +02003001 total_load_moved +=
3002 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003003 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003004 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003005 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003006 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Peter Williams43010652007-08-09 11:16:46 +02003008 return total_load_moved > 0;
3009}
3010
Peter Williamse1d14842007-10-24 18:23:51 +02003011static int
3012iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3013 struct sched_domain *sd, enum cpu_idle_type idle,
3014 struct rq_iterator *iterator)
3015{
3016 struct task_struct *p = iterator->start(iterator->arg);
3017 int pinned = 0;
3018
3019 while (p) {
3020 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3021 pull_task(busiest, p, this_rq, this_cpu);
3022 /*
3023 * Right now, this is only the second place pull_task()
3024 * is called, so we can safely collect pull_task()
3025 * stats here rather than inside pull_task().
3026 */
3027 schedstat_inc(sd, lb_gained[idle]);
3028
3029 return 1;
3030 }
3031 p = iterator->next(iterator->arg);
3032 }
3033
3034 return 0;
3035}
3036
Peter Williams43010652007-08-09 11:16:46 +02003037/*
3038 * move_one_task tries to move exactly one task from busiest to this_rq, as
3039 * part of active balancing operations within "domain".
3040 * Returns 1 if successful and 0 otherwise.
3041 *
3042 * Called with both runqueues locked.
3043 */
3044static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3045 struct sched_domain *sd, enum cpu_idle_type idle)
3046{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003047 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003048
3049 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003050 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003051 return 1;
3052
3053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
3056/*
3057 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003058 * domain. It calculates and returns the amount of weighted load which
3059 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 */
3061static struct sched_group *
3062find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003063 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003064 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
3066 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3067 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003068 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003069 unsigned long busiest_load_per_task, busiest_nr_running;
3070 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003071 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003072#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3073 int power_savings_balance = 1;
3074 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3075 unsigned long min_nr_running = ULONG_MAX;
3076 struct sched_group *group_min = NULL, *group_leader = NULL;
3077#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003080 busiest_load_per_task = busiest_nr_running = 0;
3081 this_load_per_task = this_nr_running = 0;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003082
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003083 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003084 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003085 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003086 load_idx = sd->newidle_idx;
3087 else
3088 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
3090 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003091 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 int local_group;
3093 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003094 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003095 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003096 unsigned long sum_nr_running, sum_weighted_load;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003097 unsigned long sum_avg_load_per_task;
3098 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100 local_group = cpu_isset(this_cpu, group->cpumask);
3101
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003102 if (local_group)
3103 balance_cpu = first_cpu(group->cpumask);
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003106 sum_weighted_load = sum_nr_running = avg_load = 0;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003107 sum_avg_load_per_task = avg_load_per_task = 0;
3108
Ken Chen908a7c12007-10-17 16:55:11 +02003109 max_cpu_load = 0;
3110 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003113 struct rq *rq;
3114
3115 if (!cpu_isset(i, *cpus))
3116 continue;
3117
3118 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003119
Suresh Siddha9439aab2007-07-19 21:28:35 +02003120 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003121 *sd_idle = 0;
3122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003124 if (local_group) {
3125 if (idle_cpu(i) && !first_idle_cpu) {
3126 first_idle_cpu = 1;
3127 balance_cpu = i;
3128 }
3129
Nick Piggina2000572006-02-10 01:51:02 -08003130 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003131 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003132 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003133 if (load > max_cpu_load)
3134 max_cpu_load = load;
3135 if (min_cpu_load > load)
3136 min_cpu_load = load;
3137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003140 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003141 sum_weighted_load += weighted_cpuload(i);
Peter Zijlstra408ed062008-06-27 13:41:28 +02003142
3143 sum_avg_load_per_task += cpu_avg_load_per_task(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
3145
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003146 /*
3147 * First idle cpu or the first cpu(busiest) in this sched group
3148 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003149 * domains. In the newly idle case, we will allow all the cpu's
3150 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003151 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003152 if (idle != CPU_NEWLY_IDLE && local_group &&
3153 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003154 *balance = 0;
3155 goto ret;
3156 }
3157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003159 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003162 avg_load = sg_div_cpu_power(group,
3163 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Peter Zijlstra408ed062008-06-27 13:41:28 +02003165
3166 /*
3167 * Consider the group unbalanced when the imbalance is larger
3168 * than the average weight of two tasks.
3169 *
3170 * APZ: with cgroup the avg task weight can vary wildly and
3171 * might not be a suitable number - should we keep a
3172 * normalized nr_running number somewhere that negates
3173 * the hierarchy?
3174 */
3175 avg_load_per_task = sg_div_cpu_power(group,
3176 sum_avg_load_per_task * SCHED_LOAD_SCALE);
3177
3178 if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
Ken Chen908a7c12007-10-17 16:55:11 +02003179 __group_imb = 1;
3180
Eric Dumazet5517d862007-05-08 00:32:57 -07003181 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (local_group) {
3184 this_load = avg_load;
3185 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003186 this_nr_running = sum_nr_running;
3187 this_load_per_task = sum_weighted_load;
3188 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003189 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 max_load = avg_load;
3191 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003192 busiest_nr_running = sum_nr_running;
3193 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003194 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003196
3197#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3198 /*
3199 * Busy processors will not participate in power savings
3200 * balance.
3201 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003202 if (idle == CPU_NOT_IDLE ||
3203 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3204 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003205
3206 /*
3207 * If the local group is idle or completely loaded
3208 * no need to do power savings balance at this domain
3209 */
3210 if (local_group && (this_nr_running >= group_capacity ||
3211 !this_nr_running))
3212 power_savings_balance = 0;
3213
Ingo Molnardd41f592007-07-09 18:51:59 +02003214 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003215 * If a group is already running at full capacity or idle,
3216 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003217 */
3218 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003219 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003220 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003221
Ingo Molnardd41f592007-07-09 18:51:59 +02003222 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003223 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003224 * This is the group from where we need to pick up the load
3225 * for saving power
3226 */
3227 if ((sum_nr_running < min_nr_running) ||
3228 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003229 first_cpu(group->cpumask) <
3230 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003231 group_min = group;
3232 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003233 min_load_per_task = sum_weighted_load /
3234 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003235 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003236
Ingo Molnardd41f592007-07-09 18:51:59 +02003237 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003238 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003239 * capacity but still has some space to pick up some load
3240 * from other group and save more power
3241 */
3242 if (sum_nr_running <= group_capacity - 1) {
3243 if (sum_nr_running > leader_nr_running ||
3244 (sum_nr_running == leader_nr_running &&
3245 first_cpu(group->cpumask) >
3246 first_cpu(group_leader->cpumask))) {
3247 group_leader = group;
3248 leader_nr_running = sum_nr_running;
3249 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003250 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003251group_next:
3252#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 group = group->next;
3254 } while (group != sd->groups);
3255
Peter Williams2dd73a42006-06-27 02:54:34 -07003256 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 goto out_balanced;
3258
3259 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3260
3261 if (this_load >= avg_load ||
3262 100*max_load <= sd->imbalance_pct*this_load)
3263 goto out_balanced;
3264
Peter Williams2dd73a42006-06-27 02:54:34 -07003265 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003266 if (group_imb)
3267 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3268
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 /*
3270 * We're trying to get all the cpus to the average_load, so we don't
3271 * want to push ourselves above the average load, nor do we wish to
3272 * reduce the max loaded cpu below the average load, as either of these
3273 * actions would just result in more rebalancing later, and ping-pong
3274 * tasks around. Thus we look for the minimum possible imbalance.
3275 * Negative imbalances (*we* are more loaded than anyone else) will
3276 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003277 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 * appear as very large values with unsigned longs.
3279 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003280 if (max_load <= busiest_load_per_task)
3281 goto out_balanced;
3282
3283 /*
3284 * In the presence of smp nice balancing, certain scenarios can have
3285 * max load less than avg load(as we skip the groups at or below
3286 * its cpu_power, while calculating max_load..)
3287 */
3288 if (max_load < avg_load) {
3289 *imbalance = 0;
3290 goto small_imbalance;
3291 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003292
3293 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003294 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003297 *imbalance = min(max_pull * busiest->__cpu_power,
3298 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 / SCHED_LOAD_SCALE;
3300
Peter Williams2dd73a42006-06-27 02:54:34 -07003301 /*
3302 * if *imbalance is less than the average load per runnable task
3303 * there is no gaurantee that any tasks will be moved so we'll have
3304 * a think about bumping its value to force at least one task to be
3305 * moved
3306 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003307 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003308 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003309 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Peter Williams2dd73a42006-06-27 02:54:34 -07003311small_imbalance:
3312 pwr_move = pwr_now = 0;
3313 imbn = 2;
3314 if (this_nr_running) {
3315 this_load_per_task /= this_nr_running;
3316 if (busiest_load_per_task > this_load_per_task)
3317 imbn = 1;
3318 } else
Peter Zijlstra408ed062008-06-27 13:41:28 +02003319 this_load_per_task = cpu_avg_load_per_task(this_cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07003320
Peter Zijlstra408ed062008-06-27 13:41:28 +02003321 if (max_load - this_load + 2*busiest_load_per_task >=
Ingo Molnardd41f592007-07-09 18:51:59 +02003322 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003323 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 return busiest;
3325 }
3326
3327 /*
3328 * OK, we don't have enough imbalance to justify moving tasks,
3329 * however we may be able to increase total CPU power used by
3330 * moving them.
3331 */
3332
Eric Dumazet5517d862007-05-08 00:32:57 -07003333 pwr_now += busiest->__cpu_power *
3334 min(busiest_load_per_task, max_load);
3335 pwr_now += this->__cpu_power *
3336 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 pwr_now /= SCHED_LOAD_SCALE;
3338
3339 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003340 tmp = sg_div_cpu_power(busiest,
3341 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003343 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003344 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
3346 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003347 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003348 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003349 tmp = sg_div_cpu_power(this,
3350 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003352 tmp = sg_div_cpu_power(this,
3353 busiest_load_per_task * SCHED_LOAD_SCALE);
3354 pwr_move += this->__cpu_power *
3355 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 pwr_move /= SCHED_LOAD_SCALE;
3357
3358 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003359 if (pwr_move > pwr_now)
3360 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 }
3362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 return busiest;
3364
3365out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003366#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003367 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003368 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003370 if (this == group_leader && group_leader != group_min) {
3371 *imbalance = min_load_per_task;
3372 return group_min;
3373 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003374#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003375ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 *imbalance = 0;
3377 return NULL;
3378}
3379
3380/*
3381 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3382 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003383static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003384find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003385 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003387 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003388 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 int i;
3390
3391 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003392 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003393
3394 if (!cpu_isset(i, *cpus))
3395 continue;
3396
Ingo Molnar48f24c42006-07-03 00:25:40 -07003397 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003398 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Ingo Molnardd41f592007-07-09 18:51:59 +02003400 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003401 continue;
3402
Ingo Molnardd41f592007-07-09 18:51:59 +02003403 if (wl > max_load) {
3404 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003405 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 }
3407 }
3408
3409 return busiest;
3410}
3411
3412/*
Nick Piggin77391d72005-06-25 14:57:30 -07003413 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3414 * so long as it is large enough.
3415 */
3416#define MAX_PINNED_INTERVAL 512
3417
3418/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3420 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003422static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003423 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003424 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425{
Peter Williams43010652007-08-09 11:16:46 +02003426 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003429 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003430 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003431
Mike Travis7c16ec52008-04-04 18:11:11 -07003432 cpus_setall(*cpus);
3433
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003434 /*
3435 * When power savings policy is enabled for the parent domain, idle
3436 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003437 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003438 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003439 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003440 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003441 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003442 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Ingo Molnar2d723762007-10-15 17:00:12 +02003444 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003446redo:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02003447 update_shares(sd);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003448 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003449 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003450
Chen, Kenneth W06066712006-12-10 02:20:35 -08003451 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003452 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 if (!group) {
3455 schedstat_inc(sd, lb_nobusyg[idle]);
3456 goto out_balanced;
3457 }
3458
Mike Travis7c16ec52008-04-04 18:11:11 -07003459 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 if (!busiest) {
3461 schedstat_inc(sd, lb_nobusyq[idle]);
3462 goto out_balanced;
3463 }
3464
Nick Piggindb935db2005-06-25 14:57:11 -07003465 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
3467 schedstat_add(sd, lb_imbalance[idle], imbalance);
3468
Peter Williams43010652007-08-09 11:16:46 +02003469 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 if (busiest->nr_running > 1) {
3471 /*
3472 * Attempt to move tasks. If find_busiest_group has found
3473 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003474 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 * correctly treated as an imbalance.
3476 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003477 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003478 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003479 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003480 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003481 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003482 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003483
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003484 /*
3485 * some other cpu did the load balance for us.
3486 */
Peter Williams43010652007-08-09 11:16:46 +02003487 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003488 resched_cpu(this_cpu);
3489
Nick Piggin81026792005-06-25 14:57:07 -07003490 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003491 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003492 cpu_clear(cpu_of(busiest), *cpus);
3493 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003494 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003495 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
Nick Piggin81026792005-06-25 14:57:07 -07003498
Peter Williams43010652007-08-09 11:16:46 +02003499 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 schedstat_inc(sd, lb_failed[idle]);
3501 sd->nr_balance_failed++;
3502
3503 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003505 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003506
3507 /* don't kick the migration_thread, if the curr
3508 * task on busiest cpu can't be moved to this_cpu
3509 */
3510 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003511 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003512 all_pinned = 1;
3513 goto out_one_pinned;
3514 }
3515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 if (!busiest->active_balance) {
3517 busiest->active_balance = 1;
3518 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003519 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003521 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003522 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 wake_up_process(busiest->migration_thread);
3524
3525 /*
3526 * We've kicked active balancing, reset the failure
3527 * counter.
3528 */
Nick Piggin39507452005-06-25 14:57:09 -07003529 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 }
Nick Piggin81026792005-06-25 14:57:07 -07003531 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 sd->nr_balance_failed = 0;
3533
Nick Piggin81026792005-06-25 14:57:07 -07003534 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 /* We were unbalanced, so reset the balancing interval */
3536 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003537 } else {
3538 /*
3539 * If we've begun active balancing, start to back off. This
3540 * case may not be covered by the all_pinned logic if there
3541 * is only 1 task on the busy runqueue (because we don't call
3542 * move_tasks).
3543 */
3544 if (sd->balance_interval < sd->max_interval)
3545 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
3547
Peter Williams43010652007-08-09 11:16:46 +02003548 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003549 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003550 ld_moved = -1;
3551
3552 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
3554out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 schedstat_inc(sd, lb_balanced[idle]);
3556
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003557 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003558
3559out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003561 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3562 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 sd->balance_interval *= 2;
3564
Ingo Molnar48f24c42006-07-03 00:25:40 -07003565 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003566 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003567 ld_moved = -1;
3568 else
3569 ld_moved = 0;
3570out:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02003571 if (ld_moved)
3572 update_shares(sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003573 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
3576/*
3577 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3578 * tasks if there is an imbalance.
3579 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003580 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 * this_rq is locked.
3582 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003583static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003584load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3585 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
3587 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003588 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003590 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003591 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003592 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003593
3594 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003595
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003596 /*
3597 * When power savings policy is enabled for the parent domain, idle
3598 * sibling can pick up load irrespective of busy siblings. In this case,
3599 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003600 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003601 */
3602 if (sd->flags & SD_SHARE_CPUPOWER &&
3603 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003604 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Ingo Molnar2d723762007-10-15 17:00:12 +02003606 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003607redo:
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02003608 update_shares_locked(this_rq, sd);
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003609 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003610 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003612 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003613 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 }
3615
Mike Travis7c16ec52008-04-04 18:11:11 -07003616 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003617 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003618 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003619 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 }
3621
Nick Piggindb935db2005-06-25 14:57:11 -07003622 BUG_ON(busiest == this_rq);
3623
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003624 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003625
Peter Williams43010652007-08-09 11:16:46 +02003626 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003627 if (busiest->nr_running > 1) {
3628 /* Attempt to move tasks */
3629 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003630 /* this_rq->clock is already updated */
3631 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003632 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003633 imbalance, sd, CPU_NEWLY_IDLE,
3634 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003635 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003636
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003637 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003638 cpu_clear(cpu_of(busiest), *cpus);
3639 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003640 goto redo;
3641 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003642 }
3643
Peter Williams43010652007-08-09 11:16:46 +02003644 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003645 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003646 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3647 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003648 return -1;
3649 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003650 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02003652 update_shares_locked(this_rq, sd);
Peter Williams43010652007-08-09 11:16:46 +02003653 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003654
3655out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003656 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003657 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003658 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003659 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003660 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003661
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
3665/*
3666 * idle_balance is called by schedule() if this_cpu is about to become
3667 * idle. Attempts to pull tasks from other CPUs.
3668 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003669static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670{
3671 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003672 int pulled_task = -1;
3673 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003674 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
3676 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003677 unsigned long interval;
3678
3679 if (!(sd->flags & SD_LOAD_BALANCE))
3680 continue;
3681
3682 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003683 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003684 pulled_task = load_balance_newidle(this_cpu, this_rq,
3685 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003686
3687 interval = msecs_to_jiffies(sd->balance_interval);
3688 if (time_after(next_balance, sd->last_balance + interval))
3689 next_balance = sd->last_balance + interval;
3690 if (pulled_task)
3691 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003693 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003694 /*
3695 * We are going idle. next_balance may be set based on
3696 * a busy processor. So reset next_balance.
3697 */
3698 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700}
3701
3702/*
3703 * active_load_balance is run by migration threads. It pushes running tasks
3704 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3705 * running on each physical CPU where possible, and avoids physical /
3706 * logical imbalances.
3707 *
3708 * Called with busiest_rq locked.
3709 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003710static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
Nick Piggin39507452005-06-25 14:57:09 -07003712 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003713 struct sched_domain *sd;
3714 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003715
Ingo Molnar48f24c42006-07-03 00:25:40 -07003716 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003717 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003718 return;
3719
3720 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
3722 /*
Nick Piggin39507452005-06-25 14:57:09 -07003723 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003724 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003725 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 */
Nick Piggin39507452005-06-25 14:57:09 -07003727 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
Nick Piggin39507452005-06-25 14:57:09 -07003729 /* move a task from busiest_rq to target_rq */
3730 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003731 update_rq_clock(busiest_rq);
3732 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Nick Piggin39507452005-06-25 14:57:09 -07003734 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003735 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003736 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003737 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003738 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Ingo Molnar48f24c42006-07-03 00:25:40 -07003741 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003742 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Peter Williams43010652007-08-09 11:16:46 +02003744 if (move_one_task(target_rq, target_cpu, busiest_rq,
3745 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003746 schedstat_inc(sd, alb_pushed);
3747 else
3748 schedstat_inc(sd, alb_failed);
3749 }
Nick Piggin39507452005-06-25 14:57:09 -07003750 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751}
3752
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003753#ifdef CONFIG_NO_HZ
3754static struct {
3755 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003756 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003757} nohz ____cacheline_aligned = {
3758 .load_balancer = ATOMIC_INIT(-1),
3759 .cpu_mask = CPU_MASK_NONE,
3760};
3761
Christoph Lameter7835b982006-12-10 02:20:22 -08003762/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003763 * This routine will try to nominate the ilb (idle load balancing)
3764 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3765 * load balancing on behalf of all those cpus. If all the cpus in the system
3766 * go into this tickless mode, then there will be no ilb owner (as there is
3767 * no need for one) and all the cpus will sleep till the next wakeup event
3768 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003769 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003770 * For the ilb owner, tick is not stopped. And this tick will be used
3771 * for idle load balancing. ilb owner will still be part of
3772 * nohz.cpu_mask..
3773 *
3774 * While stopping the tick, this cpu will become the ilb owner if there
3775 * is no other owner. And will be the owner till that cpu becomes busy
3776 * or if all cpus in the system stop their ticks at which point
3777 * there is no need for ilb owner.
3778 *
3779 * When the ilb owner becomes busy, it nominates another owner, during the
3780 * next busy scheduler_tick()
3781 */
3782int select_nohz_load_balancer(int stop_tick)
3783{
3784 int cpu = smp_processor_id();
3785
3786 if (stop_tick) {
3787 cpu_set(cpu, nohz.cpu_mask);
3788 cpu_rq(cpu)->in_nohz_recently = 1;
3789
3790 /*
3791 * If we are going offline and still the leader, give up!
3792 */
3793 if (cpu_is_offline(cpu) &&
3794 atomic_read(&nohz.load_balancer) == cpu) {
3795 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3796 BUG();
3797 return 0;
3798 }
3799
3800 /* time for ilb owner also to sleep */
3801 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3802 if (atomic_read(&nohz.load_balancer) == cpu)
3803 atomic_set(&nohz.load_balancer, -1);
3804 return 0;
3805 }
3806
3807 if (atomic_read(&nohz.load_balancer) == -1) {
3808 /* make me the ilb owner */
3809 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3810 return 1;
3811 } else if (atomic_read(&nohz.load_balancer) == cpu)
3812 return 1;
3813 } else {
3814 if (!cpu_isset(cpu, nohz.cpu_mask))
3815 return 0;
3816
3817 cpu_clear(cpu, nohz.cpu_mask);
3818
3819 if (atomic_read(&nohz.load_balancer) == cpu)
3820 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3821 BUG();
3822 }
3823 return 0;
3824}
3825#endif
3826
3827static DEFINE_SPINLOCK(balancing);
3828
3829/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003830 * It checks each scheduling domain to see if it is due to be balanced,
3831 * and initiates a balancing operation if so.
3832 *
3833 * Balancing parameters are set up in arch_init_sched_domains.
3834 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003835static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003836{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003837 int balance = 1;
3838 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003839 unsigned long interval;
3840 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003841 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003842 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003843 int update_next_balance = 0;
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003844 int need_serialize;
Mike Travis7c16ec52008-04-04 18:11:11 -07003845 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003847 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 if (!(sd->flags & SD_LOAD_BALANCE))
3849 continue;
3850
3851 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003852 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 interval *= sd->busy_factor;
3854
3855 /* scale ms to jiffies */
3856 interval = msecs_to_jiffies(interval);
3857 if (unlikely(!interval))
3858 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003859 if (interval > HZ*NR_CPUS/10)
3860 interval = HZ*NR_CPUS/10;
3861
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003862 need_serialize = sd->flags & SD_SERIALIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003864 if (need_serialize) {
Christoph Lameter08c183f2006-12-10 02:20:29 -08003865 if (!spin_trylock(&balancing))
3866 goto out;
3867 }
3868
Christoph Lameterc9819f42006-12-10 02:20:25 -08003869 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003870 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003871 /*
3872 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003873 * longer idle, or one of our SMT siblings is
3874 * not idle.
3875 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003876 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003878 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 }
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003880 if (need_serialize)
Christoph Lameter08c183f2006-12-10 02:20:29 -08003881 spin_unlock(&balancing);
3882out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003883 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003884 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003885 update_next_balance = 1;
3886 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003887
3888 /*
3889 * Stop the load balance at this level. There is another
3890 * CPU in our sched group which is doing load balancing more
3891 * actively.
3892 */
3893 if (!balance)
3894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003896
3897 /*
3898 * next_balance will be updated only when there is a need.
3899 * When the cpu is attached to null domain for ex, it will not be
3900 * updated.
3901 */
3902 if (likely(update_next_balance))
3903 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003904}
3905
3906/*
3907 * run_rebalance_domains is triggered when needed from the scheduler tick.
3908 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3909 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3910 */
3911static void run_rebalance_domains(struct softirq_action *h)
3912{
Ingo Molnardd41f592007-07-09 18:51:59 +02003913 int this_cpu = smp_processor_id();
3914 struct rq *this_rq = cpu_rq(this_cpu);
3915 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3916 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003917
Ingo Molnardd41f592007-07-09 18:51:59 +02003918 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003919
3920#ifdef CONFIG_NO_HZ
3921 /*
3922 * If this cpu is the owner for idle load balancing, then do the
3923 * balancing on behalf of the other idle cpus whose ticks are
3924 * stopped.
3925 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003926 if (this_rq->idle_at_tick &&
3927 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003928 cpumask_t cpus = nohz.cpu_mask;
3929 struct rq *rq;
3930 int balance_cpu;
3931
Ingo Molnardd41f592007-07-09 18:51:59 +02003932 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003933 for_each_cpu_mask(balance_cpu, cpus) {
3934 /*
3935 * If this cpu gets work to do, stop the load balancing
3936 * work being done for other cpus. Next load
3937 * balancing owner will pick it up.
3938 */
3939 if (need_resched())
3940 break;
3941
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003942 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003943
3944 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003945 if (time_after(this_rq->next_balance, rq->next_balance))
3946 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003947 }
3948 }
3949#endif
3950}
3951
3952/*
3953 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3954 *
3955 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3956 * idle load balancing owner or decide to stop the periodic load balancing,
3957 * if the whole system is idle.
3958 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003959static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003960{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003961#ifdef CONFIG_NO_HZ
3962 /*
3963 * If we were in the nohz mode recently and busy at the current
3964 * scheduler tick, then check if we need to nominate new idle
3965 * load balancer.
3966 */
3967 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3968 rq->in_nohz_recently = 0;
3969
3970 if (atomic_read(&nohz.load_balancer) == cpu) {
3971 cpu_clear(cpu, nohz.cpu_mask);
3972 atomic_set(&nohz.load_balancer, -1);
3973 }
3974
3975 if (atomic_read(&nohz.load_balancer) == -1) {
3976 /*
3977 * simple selection for now: Nominate the
3978 * first cpu in the nohz list to be the next
3979 * ilb owner.
3980 *
3981 * TBD: Traverse the sched domains and nominate
3982 * the nearest cpu in the nohz.cpu_mask.
3983 */
3984 int ilb = first_cpu(nohz.cpu_mask);
3985
Mike Travis434d53b2008-04-04 18:11:04 -07003986 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003987 resched_cpu(ilb);
3988 }
3989 }
3990
3991 /*
3992 * If this cpu is idle and doing idle load balancing for all the
3993 * cpus with ticks stopped, is it time for that to stop?
3994 */
3995 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3996 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3997 resched_cpu(cpu);
3998 return;
3999 }
4000
4001 /*
4002 * If this cpu is idle and the idle load balancing is done by
4003 * someone else, then no need raise the SCHED_SOFTIRQ
4004 */
4005 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4006 cpu_isset(cpu, nohz.cpu_mask))
4007 return;
4008#endif
4009 if (time_after_eq(jiffies, rq->next_balance))
4010 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
Ingo Molnardd41f592007-07-09 18:51:59 +02004012
4013#else /* CONFIG_SMP */
4014
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015/*
4016 * on UP we do not need to balance between CPUs:
4017 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004018static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
4020}
Ingo Molnardd41f592007-07-09 18:51:59 +02004021
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022#endif
4023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024DEFINE_PER_CPU(struct kernel_stat, kstat);
4025
4026EXPORT_PER_CPU_SYMBOL(kstat);
4027
4028/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004029 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4030 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004032unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004035 u64 ns, delta_exec;
4036 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004037
Ingo Molnar41b86e92007-07-09 18:51:58 +02004038 rq = task_rq_lock(p, &flags);
4039 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004040 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004041 update_rq_clock(rq);
4042 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004043 if ((s64)delta_exec > 0)
4044 ns += delta_exec;
4045 }
4046 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004047
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 return ns;
4049}
4050
4051/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 * Account user cpu time to a process.
4053 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 * @cputime: the cpu time spent in user space since the last update
4055 */
4056void account_user_time(struct task_struct *p, cputime_t cputime)
4057{
4058 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4059 cputime64_t tmp;
4060
4061 p->utime = cputime_add(p->utime, cputime);
4062
4063 /* Add user time to cpustat. */
4064 tmp = cputime_to_cputime64(cputime);
4065 if (TASK_NICE(p) > 0)
4066 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4067 else
4068 cpustat->user = cputime64_add(cpustat->user, tmp);
4069}
4070
4071/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004072 * Account guest cpu time to a process.
4073 * @p: the process that the cpu time gets accounted to
4074 * @cputime: the cpu time spent in virtual machine since the last update
4075 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004076static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004077{
4078 cputime64_t tmp;
4079 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4080
4081 tmp = cputime_to_cputime64(cputime);
4082
4083 p->utime = cputime_add(p->utime, cputime);
4084 p->gtime = cputime_add(p->gtime, cputime);
4085
4086 cpustat->user = cputime64_add(cpustat->user, tmp);
4087 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4088}
4089
4090/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004091 * Account scaled user cpu time to a process.
4092 * @p: the process that the cpu time gets accounted to
4093 * @cputime: the cpu time spent in user space since the last update
4094 */
4095void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4096{
4097 p->utimescaled = cputime_add(p->utimescaled, cputime);
4098}
4099
4100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 * Account system cpu time to a process.
4102 * @p: the process that the cpu time gets accounted to
4103 * @hardirq_offset: the offset to subtract from hardirq_count()
4104 * @cputime: the cpu time spent in kernel space since the last update
4105 */
4106void account_system_time(struct task_struct *p, int hardirq_offset,
4107 cputime_t cputime)
4108{
4109 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004110 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 cputime64_t tmp;
4112
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004113 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4114 account_guest_time(p, cputime);
4115 return;
4116 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 p->stime = cputime_add(p->stime, cputime);
4119
4120 /* Add system time to cpustat. */
4121 tmp = cputime_to_cputime64(cputime);
4122 if (hardirq_count() - hardirq_offset)
4123 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4124 else if (softirq_count())
4125 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004126 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004128 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4130 else
4131 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4132 /* Account for system time used */
4133 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134}
4135
4136/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004137 * Account scaled system cpu time to a process.
4138 * @p: the process that the cpu time gets accounted to
4139 * @hardirq_offset: the offset to subtract from hardirq_count()
4140 * @cputime: the cpu time spent in kernel space since the last update
4141 */
4142void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4143{
4144 p->stimescaled = cputime_add(p->stimescaled, cputime);
4145}
4146
4147/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 * Account for involuntary wait time.
4149 * @p: the process from which the cpu time has been stolen
4150 * @steal: the cpu time spent in involuntary wait
4151 */
4152void account_steal_time(struct task_struct *p, cputime_t steal)
4153{
4154 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4155 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004156 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
4158 if (p == rq->idle) {
4159 p->stime = cputime_add(p->stime, steal);
4160 if (atomic_read(&rq->nr_iowait) > 0)
4161 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4162 else
4163 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004164 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4166}
4167
Christoph Lameter7835b982006-12-10 02:20:22 -08004168/*
4169 * This function gets called by the timer code, with HZ frequency.
4170 * We call it with interrupts disabled.
4171 *
4172 * It also gets called by the fork code, when changing the parent's
4173 * timeslices.
4174 */
4175void scheduler_tick(void)
4176{
Christoph Lameter7835b982006-12-10 02:20:22 -08004177 int cpu = smp_processor_id();
4178 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004179 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004180
4181 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004182
Ingo Molnardd41f592007-07-09 18:51:59 +02004183 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004184 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004185 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004186 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004187 spin_unlock(&rq->lock);
4188
Christoph Lametere418e1c2006-12-10 02:20:23 -08004189#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004190 rq->idle_at_tick = idle_cpu(cpu);
4191 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193}
4194
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
4196
Srinivasa Ds43627582008-02-23 15:24:04 -08004197void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
4199 /*
4200 * Underflow?
4201 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004202 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4203 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 preempt_count() += val;
4205 /*
4206 * Spinlock count overflowing soon?
4207 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004208 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4209 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210}
4211EXPORT_SYMBOL(add_preempt_count);
4212
Srinivasa Ds43627582008-02-23 15:24:04 -08004213void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214{
4215 /*
4216 * Underflow?
4217 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004218 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4219 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 /*
4221 * Is the spinlock portion underflowing?
4222 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004223 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4224 !(preempt_count() & PREEMPT_MASK)))
4225 return;
4226
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 preempt_count() -= val;
4228}
4229EXPORT_SYMBOL(sub_preempt_count);
4230
4231#endif
4232
4233/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004234 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004236static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
Satyam Sharma838225b2007-10-24 18:23:50 +02004238 struct pt_regs *regs = get_irq_regs();
4239
4240 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4241 prev->comm, prev->pid, preempt_count());
4242
Ingo Molnardd41f592007-07-09 18:51:59 +02004243 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07004244 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02004245 if (irqs_disabled())
4246 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004247
4248 if (regs)
4249 show_regs(regs);
4250 else
4251 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004252}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Ingo Molnardd41f592007-07-09 18:51:59 +02004254/*
4255 * Various schedule()-time debugging checks and statistics:
4256 */
4257static inline void schedule_debug(struct task_struct *prev)
4258{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004260 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 * schedule() atomically, we ignore that path for now.
4262 * Otherwise, whine if we are scheduling when we should not be.
4263 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02004264 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02004265 __schedule_bug(prev);
4266
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4268
Ingo Molnar2d723762007-10-15 17:00:12 +02004269 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004270#ifdef CONFIG_SCHEDSTATS
4271 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004272 schedstat_inc(this_rq(), bkl_count);
4273 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004274 }
4275#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004276}
4277
4278/*
4279 * Pick up the highest-prio task:
4280 */
4281static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004282pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004283{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004284 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004285 struct task_struct *p;
4286
4287 /*
4288 * Optimization: we know that if all tasks are in
4289 * the fair class we can call that function directly:
4290 */
4291 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004292 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004293 if (likely(p))
4294 return p;
4295 }
4296
4297 class = sched_class_highest;
4298 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004299 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004300 if (p)
4301 return p;
4302 /*
4303 * Will never be NULL as the idle class always
4304 * returns a non-NULL p:
4305 */
4306 class = class->next;
4307 }
4308}
4309
4310/*
4311 * schedule() is the main scheduler function.
4312 */
4313asmlinkage void __sched schedule(void)
4314{
4315 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004316 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004317 struct rq *rq;
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004318 int cpu, hrtick = sched_feat(HRTICK);
Ingo Molnardd41f592007-07-09 18:51:59 +02004319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320need_resched:
4321 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004322 cpu = smp_processor_id();
4323 rq = cpu_rq(cpu);
4324 rcu_qsctr_inc(cpu);
4325 prev = rq->curr;
4326 switch_count = &prev->nivcsw;
4327
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 release_kernel_lock(prev);
4329need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Ingo Molnardd41f592007-07-09 18:51:59 +02004331 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004333 if (hrtick)
4334 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004335
Ingo Molnar1e819952007-10-15 17:00:13 +02004336 /*
4337 * Do the rq-clock update outside the rq lock:
4338 */
4339 local_irq_disable();
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004340 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004341 spin_lock(&rq->lock);
4342 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Ingo Molnardd41f592007-07-09 18:51:59 +02004344 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Oleg Nesterov16882c12008-06-08 21:20:41 +04004345 if (unlikely(signal_pending_state(prev->state, prev)))
Ingo Molnardd41f592007-07-09 18:51:59 +02004346 prev->state = TASK_RUNNING;
Oleg Nesterov16882c12008-06-08 21:20:41 +04004347 else
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004348 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004349 switch_count = &prev->nvcsw;
4350 }
4351
Steven Rostedt9a897c52008-01-25 21:08:22 +01004352#ifdef CONFIG_SMP
4353 if (prev->sched_class->pre_schedule)
4354 prev->sched_class->pre_schedule(rq, prev);
4355#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004356
Ingo Molnardd41f592007-07-09 18:51:59 +02004357 if (unlikely(!rq->nr_running))
4358 idle_balance(cpu, rq);
4359
Ingo Molnar31ee5292007-08-09 11:16:49 +02004360 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004361 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01004364 sched_info_switch(prev, next);
4365
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 rq->nr_switches++;
4367 rq->curr = next;
4368 ++*switch_count;
4369
Ingo Molnardd41f592007-07-09 18:51:59 +02004370 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004371 /*
4372 * the context switch might have flipped the stack from under
4373 * us, hence refresh the local variables.
4374 */
4375 cpu = smp_processor_id();
4376 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 } else
4378 spin_unlock_irq(&rq->lock);
4379
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004380 if (hrtick)
4381 hrtick_set(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004382
4383 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004385
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 preempt_enable_no_resched();
4387 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4388 goto need_resched;
4389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390EXPORT_SYMBOL(schedule);
4391
4392#ifdef CONFIG_PREEMPT
4393/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004394 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004395 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 * occur there and call schedule directly.
4397 */
4398asmlinkage void __sched preempt_schedule(void)
4399{
4400 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004401
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 /*
4403 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004404 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004406 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 return;
4408
Andi Kleen3a5c3592007-10-15 17:00:14 +02004409 do {
4410 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004411 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004412 sub_preempt_count(PREEMPT_ACTIVE);
4413
4414 /*
4415 * Check again in case we missed a preemption opportunity
4416 * between schedule and now.
4417 */
4418 barrier();
4419 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421EXPORT_SYMBOL(preempt_schedule);
4422
4423/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004424 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 * off of irq context.
4426 * Note, that this is called and return with irqs disabled. This will
4427 * protect us against recursive calling from irq.
4428 */
4429asmlinkage void __sched preempt_schedule_irq(void)
4430{
4431 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004432
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004433 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 BUG_ON(ti->preempt_count || !irqs_disabled());
4435
Andi Kleen3a5c3592007-10-15 17:00:14 +02004436 do {
4437 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004438 local_irq_enable();
4439 schedule();
4440 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004441 sub_preempt_count(PREEMPT_ACTIVE);
4442
4443 /*
4444 * Check again in case we missed a preemption opportunity
4445 * between schedule and now.
4446 */
4447 barrier();
4448 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449}
4450
4451#endif /* CONFIG_PREEMPT */
4452
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004453int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4454 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004456 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458EXPORT_SYMBOL(default_wake_function);
4459
4460/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004461 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4462 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 * number) then we wake all the non-exclusive tasks and one exclusive task.
4464 *
4465 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004466 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4468 */
4469static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4470 int nr_exclusive, int sync, void *key)
4471{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004472 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004474 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004475 unsigned flags = curr->flags;
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004478 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 break;
4480 }
4481}
4482
4483/**
4484 * __wake_up - wake up threads blocked on a waitqueue.
4485 * @q: the waitqueue
4486 * @mode: which threads
4487 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004488 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004490void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004491 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
4493 unsigned long flags;
4494
4495 spin_lock_irqsave(&q->lock, flags);
4496 __wake_up_common(q, mode, nr_exclusive, 0, key);
4497 spin_unlock_irqrestore(&q->lock, flags);
4498}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499EXPORT_SYMBOL(__wake_up);
4500
4501/*
4502 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4503 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004504void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
4506 __wake_up_common(q, mode, 1, 0, NULL);
4507}
4508
4509/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004510 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 * @q: the waitqueue
4512 * @mode: which threads
4513 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4514 *
4515 * The sync wakeup differs that the waker knows that it will schedule
4516 * away soon, so while the target thread will be woken up, it will not
4517 * be migrated to another CPU - ie. the two threads are 'synchronized'
4518 * with each other. This can prevent needless bouncing between CPUs.
4519 *
4520 * On UP it can prevent extra preemption.
4521 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004522void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004523__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
4525 unsigned long flags;
4526 int sync = 1;
4527
4528 if (unlikely(!q))
4529 return;
4530
4531 if (unlikely(!nr_exclusive))
4532 sync = 0;
4533
4534 spin_lock_irqsave(&q->lock, flags);
4535 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4536 spin_unlock_irqrestore(&q->lock, flags);
4537}
4538EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4539
Ingo Molnarb15136e2007-10-24 18:23:48 +02004540void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
4542 unsigned long flags;
4543
4544 spin_lock_irqsave(&x->wait.lock, flags);
4545 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004546 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 spin_unlock_irqrestore(&x->wait.lock, flags);
4548}
4549EXPORT_SYMBOL(complete);
4550
Ingo Molnarb15136e2007-10-24 18:23:48 +02004551void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
4553 unsigned long flags;
4554
4555 spin_lock_irqsave(&x->wait.lock, flags);
4556 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004557 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 spin_unlock_irqrestore(&x->wait.lock, flags);
4559}
4560EXPORT_SYMBOL(complete_all);
4561
Andi Kleen8cbbe862007-10-15 17:00:14 +02004562static inline long __sched
4563do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 if (!x->done) {
4566 DECLARE_WAITQUEUE(wait, current);
4567
4568 wait.flags |= WQ_FLAG_EXCLUSIVE;
4569 __add_wait_queue_tail(&x->wait, &wait);
4570 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004571 if ((state == TASK_INTERRUPTIBLE &&
4572 signal_pending(current)) ||
4573 (state == TASK_KILLABLE &&
4574 fatal_signal_pending(current))) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04004575 timeout = -ERESTARTSYS;
4576 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004577 }
4578 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004580 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004582 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004584 if (!x->done)
4585 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 }
4587 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04004588 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004589}
4590
4591static long __sched
4592wait_for_common(struct completion *x, long timeout, int state)
4593{
4594 might_sleep();
4595
4596 spin_lock_irq(&x->wait.lock);
4597 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004599 return timeout;
4600}
4601
Ingo Molnarb15136e2007-10-24 18:23:48 +02004602void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004603{
4604 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605}
4606EXPORT_SYMBOL(wait_for_completion);
4607
Ingo Molnarb15136e2007-10-24 18:23:48 +02004608unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4610{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004611 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612}
4613EXPORT_SYMBOL(wait_for_completion_timeout);
4614
Andi Kleen8cbbe862007-10-15 17:00:14 +02004615int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616{
Andi Kleen51e97992007-10-18 21:32:55 +02004617 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4618 if (t == -ERESTARTSYS)
4619 return t;
4620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621}
4622EXPORT_SYMBOL(wait_for_completion_interruptible);
4623
Ingo Molnarb15136e2007-10-24 18:23:48 +02004624unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625wait_for_completion_interruptible_timeout(struct completion *x,
4626 unsigned long timeout)
4627{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004628 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629}
4630EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4631
Matthew Wilcox009e5772007-12-06 12:29:54 -05004632int __sched wait_for_completion_killable(struct completion *x)
4633{
4634 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4635 if (t == -ERESTARTSYS)
4636 return t;
4637 return 0;
4638}
4639EXPORT_SYMBOL(wait_for_completion_killable);
4640
Andi Kleen8cbbe862007-10-15 17:00:14 +02004641static long __sched
4642sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004643{
4644 unsigned long flags;
4645 wait_queue_t wait;
4646
4647 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Andi Kleen8cbbe862007-10-15 17:00:14 +02004649 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Andi Kleen8cbbe862007-10-15 17:00:14 +02004651 spin_lock_irqsave(&q->lock, flags);
4652 __add_wait_queue(q, &wait);
4653 spin_unlock(&q->lock);
4654 timeout = schedule_timeout(timeout);
4655 spin_lock_irq(&q->lock);
4656 __remove_wait_queue(q, &wait);
4657 spin_unlock_irqrestore(&q->lock, flags);
4658
4659 return timeout;
4660}
4661
4662void __sched interruptible_sleep_on(wait_queue_head_t *q)
4663{
4664 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666EXPORT_SYMBOL(interruptible_sleep_on);
4667
Ingo Molnar0fec1712007-07-09 18:52:01 +02004668long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004669interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004671 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4674
Ingo Molnar0fec1712007-07-09 18:52:01 +02004675void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004677 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679EXPORT_SYMBOL(sleep_on);
4680
Ingo Molnar0fec1712007-07-09 18:52:01 +02004681long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004683 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685EXPORT_SYMBOL(sleep_on_timeout);
4686
Ingo Molnarb29739f2006-06-27 02:54:51 -07004687#ifdef CONFIG_RT_MUTEXES
4688
4689/*
4690 * rt_mutex_setprio - set the current priority of a task
4691 * @p: task
4692 * @prio: prio value (kernel-internal form)
4693 *
4694 * This function changes the 'effective' priority of a task. It does
4695 * not touch ->normal_prio like __setscheduler().
4696 *
4697 * Used by the rt_mutex code to implement priority inheritance logic.
4698 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004699void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004700{
4701 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004702 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004703 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004704 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004705
4706 BUG_ON(prio < 0 || prio > MAX_PRIO);
4707
4708 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004709 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004710
Andrew Mortond5f9f942007-05-08 20:27:06 -07004711 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004712 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004713 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004714 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004715 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004716 if (running)
4717 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004718
4719 if (rt_prio(prio))
4720 p->sched_class = &rt_sched_class;
4721 else
4722 p->sched_class = &fair_sched_class;
4723
Ingo Molnarb29739f2006-06-27 02:54:51 -07004724 p->prio = prio;
4725
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004726 if (running)
4727 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004728 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004729 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004730
4731 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004732 }
4733 task_rq_unlock(rq, &flags);
4734}
4735
4736#endif
4737
Ingo Molnar36c8b582006-07-03 00:25:41 -07004738void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739{
Ingo Molnardd41f592007-07-09 18:51:59 +02004740 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004742 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
4744 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4745 return;
4746 /*
4747 * We have to be careful, if called from sys_setpriority(),
4748 * the task might be in the middle of scheduling on another CPU.
4749 */
4750 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004751 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 /*
4753 * The RT priorities are set via sched_setscheduler(), but we still
4754 * allow the 'normal' nice value to be set - but as expected
4755 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004756 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004758 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 p->static_prio = NICE_TO_PRIO(nice);
4760 goto out_unlock;
4761 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004762 on_rq = p->se.on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004763 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004764 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004767 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004768 old_prio = p->prio;
4769 p->prio = effective_prio(p);
4770 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
Ingo Molnardd41f592007-07-09 18:51:59 +02004772 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004773 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004775 * If the task increased its priority or is running and
4776 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004778 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 resched_task(rq->curr);
4780 }
4781out_unlock:
4782 task_rq_unlock(rq, &flags);
4783}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784EXPORT_SYMBOL(set_user_nice);
4785
Matt Mackalle43379f2005-05-01 08:59:00 -07004786/*
4787 * can_nice - check if a task can reduce its nice value
4788 * @p: task
4789 * @nice: nice value
4790 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004791int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004792{
Matt Mackall024f4742005-08-18 11:24:19 -07004793 /* convert nice value [19,-20] to rlimit style value [1,40] */
4794 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004795
Matt Mackalle43379f2005-05-01 08:59:00 -07004796 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4797 capable(CAP_SYS_NICE));
4798}
4799
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800#ifdef __ARCH_WANT_SYS_NICE
4801
4802/*
4803 * sys_nice - change the priority of the current process.
4804 * @increment: priority increment
4805 *
4806 * sys_setpriority is a more generic, but much slower function that
4807 * does similar things.
4808 */
4809asmlinkage long sys_nice(int increment)
4810{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004811 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
4813 /*
4814 * Setpriority might change our priority at the same moment.
4815 * We don't have to worry. Conceptually one call occurs first
4816 * and we have a single winner.
4817 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004818 if (increment < -40)
4819 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 if (increment > 40)
4821 increment = 40;
4822
4823 nice = PRIO_TO_NICE(current->static_prio) + increment;
4824 if (nice < -20)
4825 nice = -20;
4826 if (nice > 19)
4827 nice = 19;
4828
Matt Mackalle43379f2005-05-01 08:59:00 -07004829 if (increment < 0 && !can_nice(current, nice))
4830 return -EPERM;
4831
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 retval = security_task_setnice(current, nice);
4833 if (retval)
4834 return retval;
4835
4836 set_user_nice(current, nice);
4837 return 0;
4838}
4839
4840#endif
4841
4842/**
4843 * task_prio - return the priority value of a given task.
4844 * @p: the task in question.
4845 *
4846 * This is the priority value as seen by users in /proc.
4847 * RT tasks are offset by -200. Normal tasks are centered
4848 * around 0, value goes from -16 to +15.
4849 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004850int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
4852 return p->prio - MAX_RT_PRIO;
4853}
4854
4855/**
4856 * task_nice - return the nice value of a given task.
4857 * @p: the task in question.
4858 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004859int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860{
4861 return TASK_NICE(p);
4862}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004863EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
4865/**
4866 * idle_cpu - is a given cpu idle currently?
4867 * @cpu: the processor in question.
4868 */
4869int idle_cpu(int cpu)
4870{
4871 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4872}
4873
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874/**
4875 * idle_task - return the idle task for a given cpu.
4876 * @cpu: the processor in question.
4877 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004878struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
4880 return cpu_rq(cpu)->idle;
4881}
4882
4883/**
4884 * find_process_by_pid - find a process with a matching PID value.
4885 * @pid: the pid in question.
4886 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004887static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004889 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890}
4891
4892/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004893static void
4894__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895{
Ingo Molnardd41f592007-07-09 18:51:59 +02004896 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004899 switch (p->policy) {
4900 case SCHED_NORMAL:
4901 case SCHED_BATCH:
4902 case SCHED_IDLE:
4903 p->sched_class = &fair_sched_class;
4904 break;
4905 case SCHED_FIFO:
4906 case SCHED_RR:
4907 p->sched_class = &rt_sched_class;
4908 break;
4909 }
4910
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004912 p->normal_prio = normal_prio(p);
4913 /* we are holding p->pi_lock already */
4914 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004915 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916}
4917
4918/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004919 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 * @p: the task in question.
4921 * @policy: new policy.
4922 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004923 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004924 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004926int sched_setscheduler(struct task_struct *p, int policy,
4927 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004929 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01004931 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004932 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Steven Rostedt66e53932006-06-27 02:54:44 -07004934 /* may grab non-irq protected spin_locks */
4935 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936recheck:
4937 /* double check policy once rq lock held */
4938 if (policy < 0)
4939 policy = oldpolicy = p->policy;
4940 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004941 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4942 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004943 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 /*
4945 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004946 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4947 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 */
4949 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004950 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004951 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004953 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 return -EINVAL;
4955
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004956 /*
4957 * Allow unprivileged RT tasks to decrease priority:
4958 */
4959 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004960 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004961 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004962
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004963 if (!lock_task_sighand(p, &flags))
4964 return -ESRCH;
4965 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4966 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004967
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004968 /* can't set/change the rt policy */
4969 if (policy != p->policy && !rlim_rtprio)
4970 return -EPERM;
4971
4972 /* can't increase priority */
4973 if (param->sched_priority > p->rt_priority &&
4974 param->sched_priority > rlim_rtprio)
4975 return -EPERM;
4976 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004977 /*
4978 * Like positive nice levels, dont allow tasks to
4979 * move out of SCHED_IDLE either:
4980 */
4981 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4982 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004983
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004984 /* can't change other user's priorities */
4985 if ((current->euid != p->euid) &&
4986 (current->euid != p->uid))
4987 return -EPERM;
4988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004990#ifdef CONFIG_RT_GROUP_SCHED
4991 /*
4992 * Do not allow realtime tasks into groups that have no runtime
4993 * assigned.
4994 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02004995 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004996 return -EPERM;
4997#endif
4998
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 retval = security_task_setscheduler(p, policy, param);
5000 if (retval)
5001 return retval;
5002 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005003 * make sure no PI-waiters arrive (or leave) while we are
5004 * changing the priority of the task:
5005 */
5006 spin_lock_irqsave(&p->pi_lock, flags);
5007 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 * To be able to change p->policy safely, the apropriate
5009 * runqueue lock must be held.
5010 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005011 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 /* recheck policy now with rq lock held */
5013 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5014 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005015 __task_rq_unlock(rq);
5016 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 goto recheck;
5018 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005019 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005020 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005021 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005022 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005023 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005024 if (running)
5025 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005028 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005029
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005030 if (running)
5031 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005032 if (on_rq) {
5033 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005034
5035 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005037 __task_rq_unlock(rq);
5038 spin_unlock_irqrestore(&p->pi_lock, flags);
5039
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005040 rt_mutex_adjust_pi(p);
5041
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 return 0;
5043}
5044EXPORT_SYMBOL_GPL(sched_setscheduler);
5045
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005046static int
5047do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 struct sched_param lparam;
5050 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005051 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
5053 if (!param || pid < 0)
5054 return -EINVAL;
5055 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5056 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005057
5058 rcu_read_lock();
5059 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005061 if (p != NULL)
5062 retval = sched_setscheduler(p, policy, &lparam);
5063 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 return retval;
5066}
5067
5068/**
5069 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5070 * @pid: the pid in question.
5071 * @policy: new policy.
5072 * @param: structure containing the new RT priority.
5073 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005074asmlinkage long
5075sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076{
Jason Baronc21761f2006-01-18 17:43:03 -08005077 /* negative values for policy are not valid */
5078 if (policy < 0)
5079 return -EINVAL;
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 return do_sched_setscheduler(pid, policy, param);
5082}
5083
5084/**
5085 * sys_sched_setparam - set/change the RT priority of a thread
5086 * @pid: the pid in question.
5087 * @param: structure containing the new RT priority.
5088 */
5089asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5090{
5091 return do_sched_setscheduler(pid, -1, param);
5092}
5093
5094/**
5095 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5096 * @pid: the pid in question.
5097 */
5098asmlinkage long sys_sched_getscheduler(pid_t pid)
5099{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005100 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005101 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102
5103 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005104 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
5106 retval = -ESRCH;
5107 read_lock(&tasklist_lock);
5108 p = find_process_by_pid(pid);
5109 if (p) {
5110 retval = security_task_getscheduler(p);
5111 if (!retval)
5112 retval = p->policy;
5113 }
5114 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 return retval;
5116}
5117
5118/**
5119 * sys_sched_getscheduler - get the RT priority of a thread
5120 * @pid: the pid in question.
5121 * @param: structure containing the RT priority.
5122 */
5123asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5124{
5125 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005126 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005127 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
5129 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005130 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
5132 read_lock(&tasklist_lock);
5133 p = find_process_by_pid(pid);
5134 retval = -ESRCH;
5135 if (!p)
5136 goto out_unlock;
5137
5138 retval = security_task_getscheduler(p);
5139 if (retval)
5140 goto out_unlock;
5141
5142 lp.sched_priority = p->rt_priority;
5143 read_unlock(&tasklist_lock);
5144
5145 /*
5146 * This one might sleep, we cannot do it with a spinlock held ...
5147 */
5148 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5149
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 return retval;
5151
5152out_unlock:
5153 read_unlock(&tasklist_lock);
5154 return retval;
5155}
5156
Mike Travisb53e9212008-04-04 18:11:08 -07005157long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005160 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005161 struct task_struct *p;
5162 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005164 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 read_lock(&tasklist_lock);
5166
5167 p = find_process_by_pid(pid);
5168 if (!p) {
5169 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005170 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 return -ESRCH;
5172 }
5173
5174 /*
5175 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005176 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 * usage count and then drop tasklist_lock.
5178 */
5179 get_task_struct(p);
5180 read_unlock(&tasklist_lock);
5181
5182 retval = -EPERM;
5183 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5184 !capable(CAP_SYS_NICE))
5185 goto out_unlock;
5186
David Quigleye7834f82006-06-23 02:03:59 -07005187 retval = security_task_setscheduler(p, 0, NULL);
5188 if (retval)
5189 goto out_unlock;
5190
Mike Travisf9a86fc2008-04-04 18:11:07 -07005191 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005193 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005194 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
Paul Menage8707d8b2007-10-18 23:40:22 -07005196 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005197 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005198 if (!cpus_subset(new_mask, cpus_allowed)) {
5199 /*
5200 * We must have raced with a concurrent cpuset
5201 * update. Just reset the cpus_allowed to the
5202 * cpuset's cpus_allowed
5203 */
5204 new_mask = cpus_allowed;
5205 goto again;
5206 }
5207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208out_unlock:
5209 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005210 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 return retval;
5212}
5213
5214static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5215 cpumask_t *new_mask)
5216{
5217 if (len < sizeof(cpumask_t)) {
5218 memset(new_mask, 0, sizeof(cpumask_t));
5219 } else if (len > sizeof(cpumask_t)) {
5220 len = sizeof(cpumask_t);
5221 }
5222 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5223}
5224
5225/**
5226 * sys_sched_setaffinity - set the cpu affinity of a process
5227 * @pid: pid of the process
5228 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5229 * @user_mask_ptr: user-space pointer to the new cpu mask
5230 */
5231asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5232 unsigned long __user *user_mask_ptr)
5233{
5234 cpumask_t new_mask;
5235 int retval;
5236
5237 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5238 if (retval)
5239 return retval;
5240
Mike Travisb53e9212008-04-04 18:11:08 -07005241 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242}
5243
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244long sched_getaffinity(pid_t pid, cpumask_t *mask)
5245{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005246 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005249 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 read_lock(&tasklist_lock);
5251
5252 retval = -ESRCH;
5253 p = find_process_by_pid(pid);
5254 if (!p)
5255 goto out_unlock;
5256
David Quigleye7834f82006-06-23 02:03:59 -07005257 retval = security_task_getscheduler(p);
5258 if (retval)
5259 goto out_unlock;
5260
Jack Steiner2f7016d2006-02-01 03:05:18 -08005261 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262
5263out_unlock:
5264 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005265 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
Ulrich Drepper9531b622007-08-09 11:16:46 +02005267 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268}
5269
5270/**
5271 * sys_sched_getaffinity - get the cpu affinity of a process
5272 * @pid: pid of the process
5273 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5274 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5275 */
5276asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5277 unsigned long __user *user_mask_ptr)
5278{
5279 int ret;
5280 cpumask_t mask;
5281
5282 if (len < sizeof(cpumask_t))
5283 return -EINVAL;
5284
5285 ret = sched_getaffinity(pid, &mask);
5286 if (ret < 0)
5287 return ret;
5288
5289 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5290 return -EFAULT;
5291
5292 return sizeof(cpumask_t);
5293}
5294
5295/**
5296 * sys_sched_yield - yield the current processor to other threads.
5297 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005298 * This function yields the current CPU to other tasks. If there are no
5299 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 */
5301asmlinkage long sys_sched_yield(void)
5302{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005303 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
Ingo Molnar2d723762007-10-15 17:00:12 +02005305 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005306 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
5308 /*
5309 * Since we are going to call schedule() anyway, there's
5310 * no need to preempt or enable interrupts:
5311 */
5312 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005313 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 _raw_spin_unlock(&rq->lock);
5315 preempt_enable_no_resched();
5316
5317 schedule();
5318
5319 return 0;
5320}
5321
Andrew Mortone7b38402006-06-30 01:56:00 -07005322static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005324#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5325 __might_sleep(__FILE__, __LINE__);
5326#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005327 /*
5328 * The BKS might be reacquired before we have dropped
5329 * PREEMPT_ACTIVE, which could trigger a second
5330 * cond_resched() call.
5331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 do {
5333 add_preempt_count(PREEMPT_ACTIVE);
5334 schedule();
5335 sub_preempt_count(PREEMPT_ACTIVE);
5336 } while (need_resched());
5337}
5338
Herbert Xu02b67cc32008-01-25 21:08:28 +01005339int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340{
Ingo Molnar94142322006-12-29 16:48:13 -08005341 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5342 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 __cond_resched();
5344 return 1;
5345 }
5346 return 0;
5347}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005348EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349
5350/*
5351 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5352 * call schedule, and on return reacquire the lock.
5353 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005354 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 * operations here to prevent schedule() from being called twice (once via
5356 * spin_unlock(), once by hand).
5357 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005358int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359{
Nick Piggin95c354f2008-01-30 13:31:20 +01005360 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005361 int ret = 0;
5362
Nick Piggin95c354f2008-01-30 13:31:20 +01005363 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005365 if (resched && need_resched())
5366 __cond_resched();
5367 else
5368 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005369 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374EXPORT_SYMBOL(cond_resched_lock);
5375
5376int __sched cond_resched_softirq(void)
5377{
5378 BUG_ON(!in_softirq());
5379
Ingo Molnar94142322006-12-29 16:48:13 -08005380 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005381 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 __cond_resched();
5383 local_bh_disable();
5384 return 1;
5385 }
5386 return 0;
5387}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388EXPORT_SYMBOL(cond_resched_softirq);
5389
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390/**
5391 * yield - yield the current processor to other threads.
5392 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005393 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 * thread runnable and calls sys_sched_yield().
5395 */
5396void __sched yield(void)
5397{
5398 set_current_state(TASK_RUNNING);
5399 sys_sched_yield();
5400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401EXPORT_SYMBOL(yield);
5402
5403/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005404 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 * that process accounting knows that this is a task in IO wait state.
5406 *
5407 * But don't do that if it is a deliberate, throttling IO wait (this task
5408 * has set its backing_dev_info: the queue against which it should throttle)
5409 */
5410void __sched io_schedule(void)
5411{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005412 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005414 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 atomic_inc(&rq->nr_iowait);
5416 schedule();
5417 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005418 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420EXPORT_SYMBOL(io_schedule);
5421
5422long __sched io_schedule_timeout(long timeout)
5423{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005424 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 long ret;
5426
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005427 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 atomic_inc(&rq->nr_iowait);
5429 ret = schedule_timeout(timeout);
5430 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005431 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 return ret;
5433}
5434
5435/**
5436 * sys_sched_get_priority_max - return maximum RT priority.
5437 * @policy: scheduling class.
5438 *
5439 * this syscall returns the maximum rt_priority that can be used
5440 * by a given scheduling class.
5441 */
5442asmlinkage long sys_sched_get_priority_max(int policy)
5443{
5444 int ret = -EINVAL;
5445
5446 switch (policy) {
5447 case SCHED_FIFO:
5448 case SCHED_RR:
5449 ret = MAX_USER_RT_PRIO-1;
5450 break;
5451 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005452 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005453 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 ret = 0;
5455 break;
5456 }
5457 return ret;
5458}
5459
5460/**
5461 * sys_sched_get_priority_min - return minimum RT priority.
5462 * @policy: scheduling class.
5463 *
5464 * this syscall returns the minimum rt_priority that can be used
5465 * by a given scheduling class.
5466 */
5467asmlinkage long sys_sched_get_priority_min(int policy)
5468{
5469 int ret = -EINVAL;
5470
5471 switch (policy) {
5472 case SCHED_FIFO:
5473 case SCHED_RR:
5474 ret = 1;
5475 break;
5476 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005477 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005478 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 ret = 0;
5480 }
5481 return ret;
5482}
5483
5484/**
5485 * sys_sched_rr_get_interval - return the default timeslice of a process.
5486 * @pid: pid of the process.
5487 * @interval: userspace pointer to the timeslice value.
5488 *
5489 * this syscall writes the default timeslice value of a given process
5490 * into the user-space timespec buffer. A value of '0' means infinity.
5491 */
5492asmlinkage
5493long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5494{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005495 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005496 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005497 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
5500 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005501 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502
5503 retval = -ESRCH;
5504 read_lock(&tasklist_lock);
5505 p = find_process_by_pid(pid);
5506 if (!p)
5507 goto out_unlock;
5508
5509 retval = security_task_getscheduler(p);
5510 if (retval)
5511 goto out_unlock;
5512
Ingo Molnar77034932007-12-04 17:04:39 +01005513 /*
5514 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5515 * tasks that are on an otherwise idle runqueue:
5516 */
5517 time_slice = 0;
5518 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005519 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005520 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005521 struct sched_entity *se = &p->se;
5522 unsigned long flags;
5523 struct rq *rq;
5524
5525 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005526 if (rq->cfs.load.weight)
5527 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005528 task_rq_unlock(rq, &flags);
5529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005531 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005534
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535out_unlock:
5536 read_unlock(&tasklist_lock);
5537 return retval;
5538}
5539
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005540static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005541
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005542void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005545 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005548 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005549 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005550#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005552 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005554 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555#else
5556 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005557 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005559 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560#endif
5561#ifdef CONFIG_DEBUG_STACK_USAGE
5562 {
Al Viro10ebffd2005-11-13 16:06:56 -08005563 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 while (!*n)
5565 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005566 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 }
5568#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005569 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005570 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005572 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573}
5574
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005575void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005577 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
Ingo Molnar4bd77322007-07-11 21:21:47 +02005579#if BITS_PER_LONG == 32
5580 printk(KERN_INFO
5581 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005583 printk(KERN_INFO
5584 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585#endif
5586 read_lock(&tasklist_lock);
5587 do_each_thread(g, p) {
5588 /*
5589 * reset the NMI-timeout, listing all files on a slow
5590 * console might take alot of time:
5591 */
5592 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005593 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005594 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 } while_each_thread(g, p);
5596
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005597 touch_all_softlockup_watchdogs();
5598
Ingo Molnardd41f592007-07-09 18:51:59 +02005599#ifdef CONFIG_SCHED_DEBUG
5600 sysrq_sched_debug_show();
5601#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005603 /*
5604 * Only show locks if all tasks are dumped:
5605 */
5606 if (state_filter == -1)
5607 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608}
5609
Ingo Molnar1df21052007-07-09 18:51:58 +02005610void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5611{
Ingo Molnardd41f592007-07-09 18:51:59 +02005612 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005613}
5614
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005615/**
5616 * init_idle - set up an idle thread for a given CPU
5617 * @idle: task in question
5618 * @cpu: cpu the idle task belongs to
5619 *
5620 * NOTE: this function does not set the idle thread's NEED_RESCHED
5621 * flag, to make booting more robust.
5622 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005623void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005625 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 unsigned long flags;
5627
Ingo Molnardd41f592007-07-09 18:51:59 +02005628 __sched_fork(idle);
5629 idle->se.exec_start = sched_clock();
5630
Ingo Molnarb29739f2006-06-27 02:54:51 -07005631 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005633 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
5635 spin_lock_irqsave(&rq->lock, flags);
5636 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005637#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5638 idle->oncpu = 1;
5639#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 spin_unlock_irqrestore(&rq->lock, flags);
5641
5642 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005643#if defined(CONFIG_PREEMPT)
5644 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
5645#else
Al Viroa1261f542005-11-13 16:06:55 -08005646 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005647#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005648 /*
5649 * The idle tasks have their own, simple scheduling class:
5650 */
5651 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652}
5653
5654/*
5655 * In a system that switches off the HZ timer nohz_cpu_mask
5656 * indicates which cpus entered this state. This is used
5657 * in the rcu update to wait only for active cpus. For system
5658 * which do not switch off the HZ timer nohz_cpu_mask should
5659 * always be CPU_MASK_NONE.
5660 */
5661cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5662
Ingo Molnar19978ca2007-11-09 22:39:38 +01005663/*
5664 * Increase the granularity value when there are more CPUs,
5665 * because with more CPUs the 'effective latency' as visible
5666 * to users decreases. But the relationship is not linear,
5667 * so pick a second-best guess by going with the log2 of the
5668 * number of CPUs.
5669 *
5670 * This idea comes from the SD scheduler of Con Kolivas:
5671 */
5672static inline void sched_init_granularity(void)
5673{
5674 unsigned int factor = 1 + ilog2(num_online_cpus());
5675 const unsigned long limit = 200000000;
5676
5677 sysctl_sched_min_granularity *= factor;
5678 if (sysctl_sched_min_granularity > limit)
5679 sysctl_sched_min_granularity = limit;
5680
5681 sysctl_sched_latency *= factor;
5682 if (sysctl_sched_latency > limit)
5683 sysctl_sched_latency = limit;
5684
5685 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005686}
5687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688#ifdef CONFIG_SMP
5689/*
5690 * This is how migration works:
5691 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005692 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 * runqueue and wake up that CPU's migration thread.
5694 * 2) we down() the locked semaphore => thread blocks.
5695 * 3) migration thread wakes up (implicitly it forces the migrated
5696 * thread off the CPU)
5697 * 4) it gets the migration request and checks whether the migrated
5698 * task is still in the wrong runqueue.
5699 * 5) if it's in the wrong runqueue then the migration thread removes
5700 * it and puts it into the right queue.
5701 * 6) migration thread up()s the semaphore.
5702 * 7) we wake up and the migration is done.
5703 */
5704
5705/*
5706 * Change a given task's CPU affinity. Migrate the thread to a
5707 * proper CPU and schedule it away if the CPU it's executing on
5708 * is removed from the allowed bitmask.
5709 *
5710 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005711 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 * call is not atomic; no spinlocks may be held.
5713 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005714int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005716 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005718 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005719 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005722 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 ret = -EINVAL;
5724 goto out;
5725 }
5726
David Rientjes9985b0b2008-06-05 12:57:11 -07005727 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
5728 !cpus_equal(p->cpus_allowed, *new_mask))) {
5729 ret = -EINVAL;
5730 goto out;
5731 }
5732
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005733 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005734 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005735 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005736 p->cpus_allowed = *new_mask;
5737 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005738 }
5739
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005741 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 goto out;
5743
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005744 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 /* Need help from migration thread: drop lock and wait. */
5746 task_rq_unlock(rq, &flags);
5747 wake_up_process(rq->migration_thread);
5748 wait_for_completion(&req.done);
5749 tlb_migrate_finish(p->mm);
5750 return 0;
5751 }
5752out:
5753 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005754
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 return ret;
5756}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005757EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
5759/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005760 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 * this because either it can't run here any more (set_cpus_allowed()
5762 * away from this CPU, or CPU going down), or because we're
5763 * attempting to rebalance this task on exec (sched_exec).
5764 *
5765 * So we race with normal scheduler movements, but that's OK, as long
5766 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005767 *
5768 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005770static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005772 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005773 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774
5775 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005776 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
5778 rq_src = cpu_rq(src_cpu);
5779 rq_dest = cpu_rq(dest_cpu);
5780
5781 double_rq_lock(rq_src, rq_dest);
5782 /* Already moved. */
5783 if (task_cpu(p) != src_cpu)
5784 goto out;
5785 /* Affinity changed (again). */
5786 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5787 goto out;
5788
Ingo Molnardd41f592007-07-09 18:51:59 +02005789 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005790 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005791 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005792
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005794 if (on_rq) {
5795 activate_task(rq_dest, p, 0);
5796 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005798 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799out:
5800 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005801 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802}
5803
5804/*
5805 * migration_thread - this is a highprio system thread that performs
5806 * thread migration by bumping thread off CPU then 'pushing' onto
5807 * another runqueue.
5808 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005809static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005812 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
5814 rq = cpu_rq(cpu);
5815 BUG_ON(rq->migration_thread != current);
5816
5817 set_current_state(TASK_INTERRUPTIBLE);
5818 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005819 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 spin_lock_irq(&rq->lock);
5823
5824 if (cpu_is_offline(cpu)) {
5825 spin_unlock_irq(&rq->lock);
5826 goto wait_to_die;
5827 }
5828
5829 if (rq->active_balance) {
5830 active_load_balance(rq, cpu);
5831 rq->active_balance = 0;
5832 }
5833
5834 head = &rq->migration_queue;
5835
5836 if (list_empty(head)) {
5837 spin_unlock_irq(&rq->lock);
5838 schedule();
5839 set_current_state(TASK_INTERRUPTIBLE);
5840 continue;
5841 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005842 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 list_del_init(head->next);
5844
Nick Piggin674311d2005-06-25 14:57:27 -07005845 spin_unlock(&rq->lock);
5846 __migrate_task(req->task, cpu, req->dest_cpu);
5847 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
5849 complete(&req->done);
5850 }
5851 __set_current_state(TASK_RUNNING);
5852 return 0;
5853
5854wait_to_die:
5855 /* Wait for kthread_stop */
5856 set_current_state(TASK_INTERRUPTIBLE);
5857 while (!kthread_should_stop()) {
5858 schedule();
5859 set_current_state(TASK_INTERRUPTIBLE);
5860 }
5861 __set_current_state(TASK_RUNNING);
5862 return 0;
5863}
5864
5865#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005866
5867static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5868{
5869 int ret;
5870
5871 local_irq_disable();
5872 ret = __migrate_task(p, src_cpu, dest_cpu);
5873 local_irq_enable();
5874 return ret;
5875}
5876
Kirill Korotaev054b9102006-12-10 02:20:11 -08005877/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005878 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005879 * NOTE: interrupts should be disabled by the caller
5880 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005881static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005883 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005885 struct rq *rq;
5886 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887
Andi Kleen3a5c3592007-10-15 17:00:14 +02005888 do {
5889 /* On same node? */
5890 mask = node_to_cpumask(cpu_to_node(dead_cpu));
5891 cpus_and(mask, mask, p->cpus_allowed);
5892 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893
Andi Kleen3a5c3592007-10-15 17:00:14 +02005894 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07005895 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005896 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
Andi Kleen3a5c3592007-10-15 17:00:14 +02005898 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07005899 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005900 cpumask_t cpus_allowed;
5901
5902 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07005903 /*
5904 * Try to stay on the same cpuset, where the
5905 * current cpuset may be a subset of all cpus.
5906 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005907 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07005908 * called within calls to cpuset_lock/cpuset_unlock.
5909 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02005910 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07005911 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005912 dest_cpu = any_online_cpu(p->cpus_allowed);
5913 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
Andi Kleen3a5c3592007-10-15 17:00:14 +02005915 /*
5916 * Don't tell them about moving exiting tasks or
5917 * kernel threads (both mm NULL), since they never
5918 * leave kernel.
5919 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005920 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02005921 printk(KERN_INFO "process %d (%s) no "
5922 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005923 task_pid_nr(p), p->comm, dead_cpu);
5924 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02005925 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005926 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927}
5928
5929/*
5930 * While a dead CPU has no uninterruptible tasks queued at this point,
5931 * it might still have a nonzero ->nr_uninterruptible counter, because
5932 * for performance reasons the counter is not stricly tracking tasks to
5933 * their home CPUs. So we just add the counter to another CPU's counter,
5934 * to keep the global sum constant after CPU-down:
5935 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005936static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937{
Mike Travis7c16ec52008-04-04 18:11:11 -07005938 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 unsigned long flags;
5940
5941 local_irq_save(flags);
5942 double_rq_lock(rq_src, rq_dest);
5943 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5944 rq_src->nr_uninterruptible = 0;
5945 double_rq_unlock(rq_src, rq_dest);
5946 local_irq_restore(flags);
5947}
5948
5949/* Run through task list and migrate tasks from the dead cpu. */
5950static void migrate_live_tasks(int src_cpu)
5951{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005952 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005954 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
Ingo Molnar48f24c42006-07-03 00:25:40 -07005956 do_each_thread(t, p) {
5957 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 continue;
5959
Ingo Molnar48f24c42006-07-03 00:25:40 -07005960 if (task_cpu(p) == src_cpu)
5961 move_task_off_dead_cpu(src_cpu, p);
5962 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005964 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965}
5966
Ingo Molnardd41f592007-07-09 18:51:59 +02005967/*
5968 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005969 * It does so by boosting its priority to highest possible.
5970 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 */
5972void sched_idle_next(void)
5973{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005974 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005975 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 struct task_struct *p = rq->idle;
5977 unsigned long flags;
5978
5979 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005980 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981
Ingo Molnar48f24c42006-07-03 00:25:40 -07005982 /*
5983 * Strictly not necessary since rest of the CPUs are stopped by now
5984 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 */
5986 spin_lock_irqsave(&rq->lock, flags);
5987
Ingo Molnardd41f592007-07-09 18:51:59 +02005988 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005989
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005990 update_rq_clock(rq);
5991 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
5993 spin_unlock_irqrestore(&rq->lock, flags);
5994}
5995
Ingo Molnar48f24c42006-07-03 00:25:40 -07005996/*
5997 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 * offline.
5999 */
6000void idle_task_exit(void)
6001{
6002 struct mm_struct *mm = current->active_mm;
6003
6004 BUG_ON(cpu_online(smp_processor_id()));
6005
6006 if (mm != &init_mm)
6007 switch_mm(mm, &init_mm, current);
6008 mmdrop(mm);
6009}
6010
Kirill Korotaev054b9102006-12-10 02:20:11 -08006011/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006012static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006014 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
6016 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006017 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
6019 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006020 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
Ingo Molnar48f24c42006-07-03 00:25:40 -07006022 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023
6024 /*
6025 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006026 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 * fine.
6028 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006029 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006030 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006031 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
Ingo Molnar48f24c42006-07-03 00:25:40 -07006033 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034}
6035
6036/* release_task() removes task from tasklist, so we won't find dead tasks. */
6037static void migrate_dead_tasks(unsigned int dead_cpu)
6038{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006039 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006040 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041
Ingo Molnardd41f592007-07-09 18:51:59 +02006042 for ( ; ; ) {
6043 if (!rq->nr_running)
6044 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006045 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006046 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006047 if (!next)
6048 break;
6049 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006050
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 }
6052}
6053#endif /* CONFIG_HOTPLUG_CPU */
6054
Nick Piggine692ab52007-07-26 13:40:43 +02006055#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6056
6057static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006058 {
6059 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006060 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006061 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006062 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006063};
6064
6065static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006066 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006067 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006068 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006069 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006070 .child = sd_ctl_dir,
6071 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006072 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006073};
6074
6075static struct ctl_table *sd_alloc_ctl_entry(int n)
6076{
6077 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006078 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006079
Nick Piggine692ab52007-07-26 13:40:43 +02006080 return entry;
6081}
6082
Milton Miller6382bc92007-10-15 17:00:19 +02006083static void sd_free_ctl_entry(struct ctl_table **tablep)
6084{
Milton Millercd7900762007-10-17 16:55:11 +02006085 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006086
Milton Millercd7900762007-10-17 16:55:11 +02006087 /*
6088 * In the intermediate directories, both the child directory and
6089 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006090 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006091 * static strings and all have proc handlers.
6092 */
6093 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006094 if (entry->child)
6095 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006096 if (entry->proc_handler == NULL)
6097 kfree(entry->procname);
6098 }
Milton Miller6382bc92007-10-15 17:00:19 +02006099
6100 kfree(*tablep);
6101 *tablep = NULL;
6102}
6103
Nick Piggine692ab52007-07-26 13:40:43 +02006104static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006105set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006106 const char *procname, void *data, int maxlen,
6107 mode_t mode, proc_handler *proc_handler)
6108{
Nick Piggine692ab52007-07-26 13:40:43 +02006109 entry->procname = procname;
6110 entry->data = data;
6111 entry->maxlen = maxlen;
6112 entry->mode = mode;
6113 entry->proc_handler = proc_handler;
6114}
6115
6116static struct ctl_table *
6117sd_alloc_ctl_domain_table(struct sched_domain *sd)
6118{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006119 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006120
Milton Millerad1cdc12007-10-15 17:00:19 +02006121 if (table == NULL)
6122 return NULL;
6123
Alexey Dobriyane0361852007-08-09 11:16:46 +02006124 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006125 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006126 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006127 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006128 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006129 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006130 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006131 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006132 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006133 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006134 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006135 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006136 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006137 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006138 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006139 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006140 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006141 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006142 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006143 &sd->cache_nice_tries,
6144 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006145 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006146 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006147 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006148
6149 return table;
6150}
6151
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006152static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006153{
6154 struct ctl_table *entry, *table;
6155 struct sched_domain *sd;
6156 int domain_num = 0, i;
6157 char buf[32];
6158
6159 for_each_domain(cpu, sd)
6160 domain_num++;
6161 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006162 if (table == NULL)
6163 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006164
6165 i = 0;
6166 for_each_domain(cpu, sd) {
6167 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006168 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006169 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006170 entry->child = sd_alloc_ctl_domain_table(sd);
6171 entry++;
6172 i++;
6173 }
6174 return table;
6175}
6176
6177static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006178static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006179{
6180 int i, cpu_num = num_online_cpus();
6181 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6182 char buf[32];
6183
Milton Miller73785472007-10-24 18:23:48 +02006184 WARN_ON(sd_ctl_dir[0].child);
6185 sd_ctl_dir[0].child = entry;
6186
Milton Millerad1cdc12007-10-15 17:00:19 +02006187 if (entry == NULL)
6188 return;
6189
Milton Miller97b6ea72007-10-15 17:00:19 +02006190 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006191 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006192 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006193 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006194 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006195 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006196 }
Milton Miller73785472007-10-24 18:23:48 +02006197
6198 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006199 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6200}
Milton Miller6382bc92007-10-15 17:00:19 +02006201
Milton Miller73785472007-10-24 18:23:48 +02006202/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006203static void unregister_sched_domain_sysctl(void)
6204{
Milton Miller73785472007-10-24 18:23:48 +02006205 if (sd_sysctl_header)
6206 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006207 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006208 if (sd_ctl_dir[0].child)
6209 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006210}
Nick Piggine692ab52007-07-26 13:40:43 +02006211#else
Milton Miller6382bc92007-10-15 17:00:19 +02006212static void register_sched_domain_sysctl(void)
6213{
6214}
6215static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006216{
6217}
6218#endif
6219
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006220static void set_rq_online(struct rq *rq)
6221{
6222 if (!rq->online) {
6223 const struct sched_class *class;
6224
6225 cpu_set(rq->cpu, rq->rd->online);
6226 rq->online = 1;
6227
6228 for_each_class(class) {
6229 if (class->rq_online)
6230 class->rq_online(rq);
6231 }
6232 }
6233}
6234
6235static void set_rq_offline(struct rq *rq)
6236{
6237 if (rq->online) {
6238 const struct sched_class *class;
6239
6240 for_each_class(class) {
6241 if (class->rq_offline)
6242 class->rq_offline(rq);
6243 }
6244
6245 cpu_clear(rq->cpu, rq->rd->online);
6246 rq->online = 0;
6247 }
6248}
6249
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250/*
6251 * migration_call - callback that gets triggered when a CPU is added.
6252 * Here we can start up the necessary migration thread for the new CPU.
6253 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006254static int __cpuinit
6255migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006258 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006260 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
6262 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006263
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006265 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006266 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 if (IS_ERR(p))
6268 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 kthread_bind(p, cpu);
6270 /* Must be high prio: stop_machine expects to yield to it. */
6271 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006272 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 task_rq_unlock(rq, &flags);
6274 cpu_rq(cpu)->migration_thread = p;
6275 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006276
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006278 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006279 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006281
6282 /* Update our root-domain */
6283 rq = cpu_rq(cpu);
6284 spin_lock_irqsave(&rq->lock, flags);
6285 if (rq->rd) {
6286 BUG_ON(!cpu_isset(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006287
6288 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006289 }
6290 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006292
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293#ifdef CONFIG_HOTPLUG_CPU
6294 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006295 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006296 if (!cpu_rq(cpu)->migration_thread)
6297 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006298 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006299 kthread_bind(cpu_rq(cpu)->migration_thread,
6300 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 kthread_stop(cpu_rq(cpu)->migration_thread);
6302 cpu_rq(cpu)->migration_thread = NULL;
6303 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006304
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006306 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006307 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 migrate_live_tasks(cpu);
6309 rq = cpu_rq(cpu);
6310 kthread_stop(rq->migration_thread);
6311 rq->migration_thread = NULL;
6312 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006313 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006314 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006315 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006317 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6318 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006320 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006321 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 migrate_nr_uninterruptible(rq);
6323 BUG_ON(rq->nr_running != 0);
6324
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006325 /*
6326 * No need to migrate the tasks: it was best-effort if
6327 * they didn't take sched_hotcpu_mutex. Just wake up
6328 * the requestors.
6329 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 spin_lock_irq(&rq->lock);
6331 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006332 struct migration_req *req;
6333
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006335 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 list_del_init(&req->list);
6337 complete(&req->done);
6338 }
6339 spin_unlock_irq(&rq->lock);
6340 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006341
Gregory Haskins08f503b2008-03-10 17:59:11 -04006342 case CPU_DYING:
6343 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006344 /* Update our root-domain */
6345 rq = cpu_rq(cpu);
6346 spin_lock_irqsave(&rq->lock, flags);
6347 if (rq->rd) {
6348 BUG_ON(!cpu_isset(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006349 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006350 }
6351 spin_unlock_irqrestore(&rq->lock, flags);
6352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353#endif
6354 }
6355 return NOTIFY_OK;
6356}
6357
6358/* Register at highest priority so that task migration (migrate_all_tasks)
6359 * happens before everything else.
6360 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006361static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 .notifier_call = migration_call,
6363 .priority = 10
6364};
6365
Adrian Bunke6fe6642007-11-09 22:39:39 +01006366void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367{
6368 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006369 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006370
6371 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006372 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6373 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6375 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376}
6377#endif
6378
6379#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006380
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006381#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006382
Gautham R Shenoy099f98c2008-05-29 20:56:32 +05306383static inline const char *sd_level_to_string(enum sched_domain_level lvl)
6384{
6385 switch (lvl) {
6386 case SD_LV_NONE:
6387 return "NONE";
6388 case SD_LV_SIBLING:
6389 return "SIBLING";
6390 case SD_LV_MC:
6391 return "MC";
6392 case SD_LV_CPU:
6393 return "CPU";
6394 case SD_LV_NODE:
6395 return "NODE";
6396 case SD_LV_ALLNODES:
6397 return "ALLNODES";
6398 case SD_LV_MAX:
6399 return "MAX";
6400
6401 }
6402 return "MAX";
6403}
6404
Mike Travis7c16ec52008-04-04 18:11:11 -07006405static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6406 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006407{
6408 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006409 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006410
Mike Travis434d53b2008-04-04 18:11:04 -07006411 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006412 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006413
6414 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6415
6416 if (!(sd->flags & SD_LOAD_BALANCE)) {
6417 printk("does not load-balance\n");
6418 if (sd->parent)
6419 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6420 " has parent");
6421 return -1;
6422 }
6423
Gautham R Shenoy099f98c2008-05-29 20:56:32 +05306424 printk(KERN_CONT "span %s level %s\n",
6425 str, sd_level_to_string(sd->level));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006426
6427 if (!cpu_isset(cpu, sd->span)) {
6428 printk(KERN_ERR "ERROR: domain->span does not contain "
6429 "CPU%d\n", cpu);
6430 }
6431 if (!cpu_isset(cpu, group->cpumask)) {
6432 printk(KERN_ERR "ERROR: domain->groups does not contain"
6433 " CPU%d\n", cpu);
6434 }
6435
6436 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6437 do {
6438 if (!group) {
6439 printk("\n");
6440 printk(KERN_ERR "ERROR: group is NULL\n");
6441 break;
6442 }
6443
6444 if (!group->__cpu_power) {
6445 printk(KERN_CONT "\n");
6446 printk(KERN_ERR "ERROR: domain->cpu_power not "
6447 "set\n");
6448 break;
6449 }
6450
6451 if (!cpus_weight(group->cpumask)) {
6452 printk(KERN_CONT "\n");
6453 printk(KERN_ERR "ERROR: empty group\n");
6454 break;
6455 }
6456
Mike Travis7c16ec52008-04-04 18:11:11 -07006457 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006458 printk(KERN_CONT "\n");
6459 printk(KERN_ERR "ERROR: repeated CPUs\n");
6460 break;
6461 }
6462
Mike Travis7c16ec52008-04-04 18:11:11 -07006463 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006464
Mike Travis434d53b2008-04-04 18:11:04 -07006465 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006466 printk(KERN_CONT " %s", str);
6467
6468 group = group->next;
6469 } while (group != sd->groups);
6470 printk(KERN_CONT "\n");
6471
Mike Travis7c16ec52008-04-04 18:11:11 -07006472 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006473 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6474
Mike Travis7c16ec52008-04-04 18:11:11 -07006475 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006476 printk(KERN_ERR "ERROR: parent span is not a superset "
6477 "of domain->span\n");
6478 return 0;
6479}
6480
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481static void sched_domain_debug(struct sched_domain *sd, int cpu)
6482{
Mike Travis7c16ec52008-04-04 18:11:11 -07006483 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 int level = 0;
6485
Nick Piggin41c7ce92005-06-25 14:57:24 -07006486 if (!sd) {
6487 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6488 return;
6489 }
6490
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6492
Mike Travis7c16ec52008-04-04 18:11:11 -07006493 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6494 if (!groupmask) {
6495 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6496 return;
6497 }
6498
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006499 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006500 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 level++;
6503 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006504 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006505 break;
6506 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006507 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006509#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006510# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006511#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006513static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006514{
6515 if (cpus_weight(sd->span) == 1)
6516 return 1;
6517
6518 /* Following flags need at least 2 groups */
6519 if (sd->flags & (SD_LOAD_BALANCE |
6520 SD_BALANCE_NEWIDLE |
6521 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006522 SD_BALANCE_EXEC |
6523 SD_SHARE_CPUPOWER |
6524 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006525 if (sd->groups != sd->groups->next)
6526 return 0;
6527 }
6528
6529 /* Following flags don't use groups */
6530 if (sd->flags & (SD_WAKE_IDLE |
6531 SD_WAKE_AFFINE |
6532 SD_WAKE_BALANCE))
6533 return 0;
6534
6535 return 1;
6536}
6537
Ingo Molnar48f24c42006-07-03 00:25:40 -07006538static int
6539sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006540{
6541 unsigned long cflags = sd->flags, pflags = parent->flags;
6542
6543 if (sd_degenerate(parent))
6544 return 1;
6545
6546 if (!cpus_equal(sd->span, parent->span))
6547 return 0;
6548
6549 /* Does parent contain flags not in child? */
6550 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6551 if (cflags & SD_WAKE_AFFINE)
6552 pflags &= ~SD_WAKE_BALANCE;
6553 /* Flags needing groups don't count if only 1 group in parent */
6554 if (parent->groups == parent->groups->next) {
6555 pflags &= ~(SD_LOAD_BALANCE |
6556 SD_BALANCE_NEWIDLE |
6557 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006558 SD_BALANCE_EXEC |
6559 SD_SHARE_CPUPOWER |
6560 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006561 }
6562 if (~cflags & pflags)
6563 return 0;
6564
6565 return 1;
6566}
6567
Gregory Haskins57d885f2008-01-25 21:08:18 +01006568static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6569{
6570 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006571
6572 spin_lock_irqsave(&rq->lock, flags);
6573
6574 if (rq->rd) {
6575 struct root_domain *old_rd = rq->rd;
6576
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006577 if (cpu_isset(rq->cpu, old_rd->online))
6578 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006579
Gregory Haskinsdc938522008-01-25 21:08:26 +01006580 cpu_clear(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006581
Gregory Haskins57d885f2008-01-25 21:08:18 +01006582 if (atomic_dec_and_test(&old_rd->refcount))
6583 kfree(old_rd);
6584 }
6585
6586 atomic_inc(&rd->refcount);
6587 rq->rd = rd;
6588
Gregory Haskinsdc938522008-01-25 21:08:26 +01006589 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006590 if (cpu_isset(rq->cpu, cpu_online_map))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006591 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006592
6593 spin_unlock_irqrestore(&rq->lock, flags);
6594}
6595
Gregory Haskinsdc938522008-01-25 21:08:26 +01006596static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006597{
6598 memset(rd, 0, sizeof(*rd));
6599
Gregory Haskinsdc938522008-01-25 21:08:26 +01006600 cpus_clear(rd->span);
6601 cpus_clear(rd->online);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006602
6603 cpupri_init(&rd->cpupri);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006604}
6605
6606static void init_defrootdomain(void)
6607{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006608 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006609 atomic_set(&def_root_domain.refcount, 1);
6610}
6611
Gregory Haskinsdc938522008-01-25 21:08:26 +01006612static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006613{
6614 struct root_domain *rd;
6615
6616 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6617 if (!rd)
6618 return NULL;
6619
Gregory Haskinsdc938522008-01-25 21:08:26 +01006620 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006621
6622 return rd;
6623}
6624
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006626 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 * hold the hotplug lock.
6628 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006629static void
6630cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006632 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006633 struct sched_domain *tmp;
6634
6635 /* Remove the sched domains which do not contribute to scheduling. */
6636 for (tmp = sd; tmp; tmp = tmp->parent) {
6637 struct sched_domain *parent = tmp->parent;
6638 if (!parent)
6639 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006640 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006641 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006642 if (parent->parent)
6643 parent->parent->child = tmp;
6644 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006645 }
6646
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006647 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006648 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006649 if (sd)
6650 sd->child = NULL;
6651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652
6653 sched_domain_debug(sd, cpu);
6654
Gregory Haskins57d885f2008-01-25 21:08:18 +01006655 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006656 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657}
6658
6659/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006660static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661
6662/* Setup the mask of cpus configured for isolated domains */
6663static int __init isolated_cpu_setup(char *str)
6664{
6665 int ints[NR_CPUS], i;
6666
6667 str = get_options(str, ARRAY_SIZE(ints), ints);
6668 cpus_clear(cpu_isolated_map);
6669 for (i = 1; i <= ints[0]; i++)
6670 if (ints[i] < NR_CPUS)
6671 cpu_set(ints[i], cpu_isolated_map);
6672 return 1;
6673}
6674
Ingo Molnar8927f492007-10-15 17:00:13 +02006675__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
6677/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006678 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6679 * to a function which identifies what group(along with sched group) a CPU
6680 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6681 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 *
6683 * init_sched_build_groups will build a circular linked list of the groups
6684 * covered by the given span, and will set each group's ->cpumask correctly,
6685 * and ->cpu_power to 0.
6686 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006687static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006688init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006689 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006690 struct sched_group **sg,
6691 cpumask_t *tmpmask),
6692 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693{
6694 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 int i;
6696
Mike Travis7c16ec52008-04-04 18:11:11 -07006697 cpus_clear(*covered);
6698
6699 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006700 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006701 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 int j;
6703
Mike Travis7c16ec52008-04-04 18:11:11 -07006704 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 continue;
6706
Mike Travis7c16ec52008-04-04 18:11:11 -07006707 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006708 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709
Mike Travis7c16ec52008-04-04 18:11:11 -07006710 for_each_cpu_mask(j, *span) {
6711 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 continue;
6713
Mike Travis7c16ec52008-04-04 18:11:11 -07006714 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 cpu_set(j, sg->cpumask);
6716 }
6717 if (!first)
6718 first = sg;
6719 if (last)
6720 last->next = sg;
6721 last = sg;
6722 }
6723 last->next = first;
6724}
6725
John Hawkes9c1cfda2005-09-06 15:18:14 -07006726#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727
John Hawkes9c1cfda2005-09-06 15:18:14 -07006728#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006729
John Hawkes9c1cfda2005-09-06 15:18:14 -07006730/**
6731 * find_next_best_node - find the next node to include in a sched_domain
6732 * @node: node whose sched_domain we're building
6733 * @used_nodes: nodes already in the sched_domain
6734 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006735 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006736 * finds the closest node not already in the @used_nodes map.
6737 *
6738 * Should use nodemask_t.
6739 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006740static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006741{
6742 int i, n, val, min_val, best_node = 0;
6743
6744 min_val = INT_MAX;
6745
6746 for (i = 0; i < MAX_NUMNODES; i++) {
6747 /* Start at @node */
6748 n = (node + i) % MAX_NUMNODES;
6749
6750 if (!nr_cpus_node(n))
6751 continue;
6752
6753 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006754 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006755 continue;
6756
6757 /* Simple min distance search */
6758 val = node_distance(node, n);
6759
6760 if (val < min_val) {
6761 min_val = val;
6762 best_node = n;
6763 }
6764 }
6765
Mike Travisc5f59f02008-04-04 18:11:10 -07006766 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006767 return best_node;
6768}
6769
6770/**
6771 * sched_domain_node_span - get a cpumask for a node's sched_domain
6772 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07006773 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07006774 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006775 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006776 * should be one that prevents unnecessary balancing, but also spreads tasks
6777 * out optimally.
6778 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07006779static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006780{
Mike Travisc5f59f02008-04-04 18:11:10 -07006781 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07006782 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006783 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006784
Mike Travis4bdbaad32008-04-15 16:35:52 -07006785 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07006786 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006787
Mike Travis4bdbaad32008-04-15 16:35:52 -07006788 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07006789 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006790
6791 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07006792 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006793
Mike Travisc5f59f02008-04-04 18:11:10 -07006794 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07006795 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006796 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006797}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006798#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006799
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006800int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006801
John Hawkes9c1cfda2005-09-06 15:18:14 -07006802/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006803 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006804 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805#ifdef CONFIG_SCHED_SMT
6806static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006807static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006808
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006809static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006810cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6811 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006813 if (sg)
6814 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 return cpu;
6816}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006817#endif /* CONFIG_SCHED_SMT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818
Ingo Molnar48f24c42006-07-03 00:25:40 -07006819/*
6820 * multi-core sched-domains:
6821 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006822#ifdef CONFIG_SCHED_MC
6823static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006824static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006825#endif /* CONFIG_SCHED_MC */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006826
6827#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006828static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006829cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6830 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006831{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006832 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07006833
6834 *mask = per_cpu(cpu_sibling_map, cpu);
6835 cpus_and(*mask, *mask, *cpu_map);
6836 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006837 if (sg)
6838 *sg = &per_cpu(sched_group_core, group);
6839 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006840}
6841#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006842static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006843cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6844 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006845{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006846 if (sg)
6847 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006848 return cpu;
6849}
6850#endif
6851
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006853static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006854
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006855static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006856cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6857 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006859 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006860#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07006861 *mask = cpu_coregroup_map(cpu);
6862 cpus_and(*mask, *mask, *cpu_map);
6863 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006864#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07006865 *mask = per_cpu(cpu_sibling_map, cpu);
6866 cpus_and(*mask, *mask, *cpu_map);
6867 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006869 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006871 if (sg)
6872 *sg = &per_cpu(sched_group_phys, group);
6873 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874}
6875
6876#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006877/*
6878 * The init_sched_build_groups can't handle what we want to do with node
6879 * groups, so roll our own. Now each node has its own list of groups which
6880 * gets dynamically allocated.
6881 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07006883static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006884
6885static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006886static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006887
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006888static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006889 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006891 int group;
6892
Mike Travis7c16ec52008-04-04 18:11:11 -07006893 *nodemask = node_to_cpumask(cpu_to_node(cpu));
6894 cpus_and(*nodemask, *nodemask, *cpu_map);
6895 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006896
6897 if (sg)
6898 *sg = &per_cpu(sched_group_allnodes, group);
6899 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006901
Siddha, Suresh B08069032006-03-27 01:15:23 -08006902static void init_numa_sched_groups_power(struct sched_group *group_head)
6903{
6904 struct sched_group *sg = group_head;
6905 int j;
6906
6907 if (!sg)
6908 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006909 do {
6910 for_each_cpu_mask(j, sg->cpumask) {
6911 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006912
Andi Kleen3a5c3592007-10-15 17:00:14 +02006913 sd = &per_cpu(phys_domains, j);
6914 if (j != first_cpu(sd->groups->cpumask)) {
6915 /*
6916 * Only add "power" once for each
6917 * physical package.
6918 */
6919 continue;
6920 }
6921
6922 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006923 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006924 sg = sg->next;
6925 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006926}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006927#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006929#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006930/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07006931static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006932{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006933 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006934
6935 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006936 struct sched_group **sched_group_nodes
6937 = sched_group_nodes_bycpu[cpu];
6938
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006939 if (!sched_group_nodes)
6940 continue;
6941
6942 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006943 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6944
Mike Travis7c16ec52008-04-04 18:11:11 -07006945 *nodemask = node_to_cpumask(i);
6946 cpus_and(*nodemask, *nodemask, *cpu_map);
6947 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006948 continue;
6949
6950 if (sg == NULL)
6951 continue;
6952 sg = sg->next;
6953next_sg:
6954 oldsg = sg;
6955 sg = sg->next;
6956 kfree(oldsg);
6957 if (oldsg != sched_group_nodes[i])
6958 goto next_sg;
6959 }
6960 kfree(sched_group_nodes);
6961 sched_group_nodes_bycpu[cpu] = NULL;
6962 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006963}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006964#else /* !CONFIG_NUMA */
Mike Travis7c16ec52008-04-04 18:11:11 -07006965static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006966{
6967}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006968#endif /* CONFIG_NUMA */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006969
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006971 * Initialize sched groups cpu_power.
6972 *
6973 * cpu_power indicates the capacity of sched group, which is used while
6974 * distributing the load between different sched groups in a sched domain.
6975 * Typically cpu_power for all the groups in a sched domain will be same unless
6976 * there are asymmetries in the topology. If there are asymmetries, group
6977 * having more cpu_power will pickup more load compared to the group having
6978 * less cpu_power.
6979 *
6980 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
6981 * the maximum number of tasks a group can handle in the presence of other idle
6982 * or lightly loaded groups in the same sched domain.
6983 */
6984static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6985{
6986 struct sched_domain *child;
6987 struct sched_group *group;
6988
6989 WARN_ON(!sd || !sd->groups);
6990
6991 if (cpu != first_cpu(sd->groups->cpumask))
6992 return;
6993
6994 child = sd->child;
6995
Eric Dumazet5517d862007-05-08 00:32:57 -07006996 sd->groups->__cpu_power = 0;
6997
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006998 /*
6999 * For perf policy, if the groups in child domain share resources
7000 * (for example cores sharing some portions of the cache hierarchy
7001 * or SMT), then set this domain groups cpu_power such that each group
7002 * can handle only one task, when there are other idle groups in the
7003 * same sched domain.
7004 */
7005 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7006 (child->flags &
7007 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007008 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007009 return;
7010 }
7011
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007012 /*
7013 * add cpu_power of each child group to this groups cpu_power
7014 */
7015 group = child->groups;
7016 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007017 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007018 group = group->next;
7019 } while (group != child->groups);
7020}
7021
7022/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007023 * Initializers for schedule domains
7024 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7025 */
7026
7027#define SD_INIT(sd, type) sd_init_##type(sd)
7028#define SD_INIT_FUNC(type) \
7029static noinline void sd_init_##type(struct sched_domain *sd) \
7030{ \
7031 memset(sd, 0, sizeof(*sd)); \
7032 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007033 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007034}
7035
7036SD_INIT_FUNC(CPU)
7037#ifdef CONFIG_NUMA
7038 SD_INIT_FUNC(ALLNODES)
7039 SD_INIT_FUNC(NODE)
7040#endif
7041#ifdef CONFIG_SCHED_SMT
7042 SD_INIT_FUNC(SIBLING)
7043#endif
7044#ifdef CONFIG_SCHED_MC
7045 SD_INIT_FUNC(MC)
7046#endif
7047
7048/*
7049 * To minimize stack usage kmalloc room for cpumasks and share the
7050 * space as the usage in build_sched_domains() dictates. Used only
7051 * if the amount of space is significant.
7052 */
7053struct allmasks {
7054 cpumask_t tmpmask; /* make this one first */
7055 union {
7056 cpumask_t nodemask;
7057 cpumask_t this_sibling_map;
7058 cpumask_t this_core_map;
7059 };
7060 cpumask_t send_covered;
7061
7062#ifdef CONFIG_NUMA
7063 cpumask_t domainspan;
7064 cpumask_t covered;
7065 cpumask_t notcovered;
7066#endif
7067};
7068
7069#if NR_CPUS > 128
7070#define SCHED_CPUMASK_ALLOC 1
7071#define SCHED_CPUMASK_FREE(v) kfree(v)
7072#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7073#else
7074#define SCHED_CPUMASK_ALLOC 0
7075#define SCHED_CPUMASK_FREE(v)
7076#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7077#endif
7078
7079#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7080 ((unsigned long)(a) + offsetof(struct allmasks, v))
7081
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007082static int default_relax_domain_level = -1;
7083
7084static int __init setup_relax_domain_level(char *str)
7085{
Li Zefan30e0e172008-05-13 10:27:17 +08007086 unsigned long val;
7087
7088 val = simple_strtoul(str, NULL, 0);
7089 if (val < SD_LV_MAX)
7090 default_relax_domain_level = val;
7091
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007092 return 1;
7093}
7094__setup("relax_domain_level=", setup_relax_domain_level);
7095
7096static void set_domain_attribute(struct sched_domain *sd,
7097 struct sched_domain_attr *attr)
7098{
7099 int request;
7100
7101 if (!attr || attr->relax_domain_level < 0) {
7102 if (default_relax_domain_level < 0)
7103 return;
7104 else
7105 request = default_relax_domain_level;
7106 } else
7107 request = attr->relax_domain_level;
7108 if (request < sd->level) {
7109 /* turn off idle balance on this domain */
7110 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7111 } else {
7112 /* turn on idle balance on this domain */
7113 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7114 }
7115}
7116
Mike Travis7c16ec52008-04-04 18:11:11 -07007117/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007118 * Build sched domains for a given set of cpus and attach the sched domains
7119 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007121static int __build_sched_domains(const cpumask_t *cpu_map,
7122 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123{
7124 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007125 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007126 SCHED_CPUMASK_DECLARE(allmasks);
7127 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007128#ifdef CONFIG_NUMA
7129 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007130 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007131
7132 /*
7133 * Allocate the per-node list of sched groups
7134 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007135 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007136 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007137 if (!sched_group_nodes) {
7138 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007139 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007140 }
John Hawkesd1b55132005-09-06 15:18:14 -07007141#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142
Gregory Haskinsdc938522008-01-25 21:08:26 +01007143 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007144 if (!rd) {
7145 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007146#ifdef CONFIG_NUMA
7147 kfree(sched_group_nodes);
7148#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007149 return -ENOMEM;
7150 }
7151
Mike Travis7c16ec52008-04-04 18:11:11 -07007152#if SCHED_CPUMASK_ALLOC
7153 /* get space for all scratch cpumask variables */
7154 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7155 if (!allmasks) {
7156 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7157 kfree(rd);
7158#ifdef CONFIG_NUMA
7159 kfree(sched_group_nodes);
7160#endif
7161 return -ENOMEM;
7162 }
7163#endif
7164 tmpmask = (cpumask_t *)allmasks;
7165
7166
7167#ifdef CONFIG_NUMA
7168 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7169#endif
7170
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007172 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007174 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007176 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177
Mike Travis7c16ec52008-04-04 18:11:11 -07007178 *nodemask = node_to_cpumask(cpu_to_node(i));
7179 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180
7181#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007182 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007183 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007184 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007185 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007186 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007187 sd->span = *cpu_map;
Mike Travis7c16ec52008-04-04 18:11:11 -07007188 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007189 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007190 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007191 } else
7192 p = NULL;
7193
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007195 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007196 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007197 sched_domain_node_span(cpu_to_node(i), &sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007198 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007199 if (p)
7200 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007201 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202#endif
7203
7204 p = sd;
7205 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007206 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007207 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007208 sd->span = *nodemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007210 if (p)
7211 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007212 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007214#ifdef CONFIG_SCHED_MC
7215 p = sd;
7216 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007217 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007218 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007219 sd->span = cpu_coregroup_map(i);
7220 cpus_and(sd->span, sd->span, *cpu_map);
7221 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007222 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007223 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007224#endif
7225
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226#ifdef CONFIG_SCHED_SMT
7227 p = sd;
7228 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007229 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007230 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007231 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007232 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007234 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007235 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236#endif
7237 }
7238
7239#ifdef CONFIG_SCHED_SMT
7240 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007241 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007242 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7243 SCHED_CPUMASK_VAR(send_covered, allmasks);
7244
7245 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7246 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7247 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 continue;
7249
Ingo Molnardd41f592007-07-09 18:51:59 +02007250 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007251 &cpu_to_cpu_group,
7252 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 }
7254#endif
7255
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007256#ifdef CONFIG_SCHED_MC
7257 /* Set up multi-core groups */
7258 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007259 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7260 SCHED_CPUMASK_VAR(send_covered, allmasks);
7261
7262 *this_core_map = cpu_coregroup_map(i);
7263 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7264 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007265 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007266
Ingo Molnardd41f592007-07-09 18:51:59 +02007267 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007268 &cpu_to_core_group,
7269 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007270 }
7271#endif
7272
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273 /* Set up physical groups */
7274 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007275 SCHED_CPUMASK_VAR(nodemask, allmasks);
7276 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277
Mike Travis7c16ec52008-04-04 18:11:11 -07007278 *nodemask = node_to_cpumask(i);
7279 cpus_and(*nodemask, *nodemask, *cpu_map);
7280 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 continue;
7282
Mike Travis7c16ec52008-04-04 18:11:11 -07007283 init_sched_build_groups(nodemask, cpu_map,
7284 &cpu_to_phys_group,
7285 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286 }
7287
7288#ifdef CONFIG_NUMA
7289 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007290 if (sd_allnodes) {
7291 SCHED_CPUMASK_VAR(send_covered, allmasks);
7292
7293 init_sched_build_groups(cpu_map, cpu_map,
7294 &cpu_to_allnodes_group,
7295 send_covered, tmpmask);
7296 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007297
7298 for (i = 0; i < MAX_NUMNODES; i++) {
7299 /* Set up node groups */
7300 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007301 SCHED_CPUMASK_VAR(nodemask, allmasks);
7302 SCHED_CPUMASK_VAR(domainspan, allmasks);
7303 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007304 int j;
7305
Mike Travis7c16ec52008-04-04 18:11:11 -07007306 *nodemask = node_to_cpumask(i);
7307 cpus_clear(*covered);
7308
7309 cpus_and(*nodemask, *nodemask, *cpu_map);
7310 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007311 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007312 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007313 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007314
Mike Travis4bdbaad32008-04-15 16:35:52 -07007315 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007316 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007317
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007318 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007319 if (!sg) {
7320 printk(KERN_WARNING "Can not alloc domain group for "
7321 "node %d\n", i);
7322 goto error;
7323 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007324 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007325 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007326 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007327
John Hawkes9c1cfda2005-09-06 15:18:14 -07007328 sd = &per_cpu(node_domains, j);
7329 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007330 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007331 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007332 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007333 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007334 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007335 prev = sg;
7336
7337 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007338 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007339 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007340 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007341
Mike Travis7c16ec52008-04-04 18:11:11 -07007342 cpus_complement(*notcovered, *covered);
7343 cpus_and(*tmpmask, *notcovered, *cpu_map);
7344 cpus_and(*tmpmask, *tmpmask, *domainspan);
7345 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007346 break;
7347
Mike Travis7c16ec52008-04-04 18:11:11 -07007348 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7349 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007350 continue;
7351
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007352 sg = kmalloc_node(sizeof(struct sched_group),
7353 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007354 if (!sg) {
7355 printk(KERN_WARNING
7356 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007357 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007358 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007359 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007360 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007361 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007362 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007363 prev->next = sg;
7364 prev = sg;
7365 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367#endif
7368
7369 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007370#ifdef CONFIG_SCHED_SMT
7371 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007372 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7373
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007374 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007375 }
7376#endif
7377#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007378 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007379 struct sched_domain *sd = &per_cpu(core_domains, i);
7380
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007381 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007382 }
7383#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007385 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007386 struct sched_domain *sd = &per_cpu(phys_domains, i);
7387
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007388 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 }
7390
John Hawkes9c1cfda2005-09-06 15:18:14 -07007391#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007392 for (i = 0; i < MAX_NUMNODES; i++)
7393 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007394
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007395 if (sd_allnodes) {
7396 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007397
Mike Travis7c16ec52008-04-04 18:11:11 -07007398 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7399 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007400 init_numa_sched_groups_power(sg);
7401 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007402#endif
7403
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007405 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 struct sched_domain *sd;
7407#ifdef CONFIG_SCHED_SMT
7408 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007409#elif defined(CONFIG_SCHED_MC)
7410 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411#else
7412 sd = &per_cpu(phys_domains, i);
7413#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007414 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007416
Mike Travis7c16ec52008-04-04 18:11:11 -07007417 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007418 return 0;
7419
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007420#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007421error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007422 free_sched_groups(cpu_map, tmpmask);
7423 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007424 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007425#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426}
Paul Jackson029190c2007-10-18 23:40:20 -07007427
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007428static int build_sched_domains(const cpumask_t *cpu_map)
7429{
7430 return __build_sched_domains(cpu_map, NULL);
7431}
7432
Paul Jackson029190c2007-10-18 23:40:20 -07007433static cpumask_t *doms_cur; /* current sched domains */
7434static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007435static struct sched_domain_attr *dattr_cur;
7436 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007437
7438/*
7439 * Special case: If a kmalloc of a doms_cur partition (array of
7440 * cpumask_t) fails, then fallback to a single sched domain,
7441 * as determined by the single cpumask_t fallback_doms.
7442 */
7443static cpumask_t fallback_doms;
7444
Heiko Carstens22e52b02008-03-12 18:31:59 +01007445void __attribute__((weak)) arch_update_cpu_topology(void)
7446{
7447}
7448
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007449/*
Max Krasnyansky5c8e1ed2008-05-29 11:17:01 -07007450 * Free current domain masks.
7451 * Called after all cpus are attached to NULL domain.
7452 */
7453static void free_sched_domains(void)
7454{
7455 ndoms_cur = 0;
7456 if (doms_cur != &fallback_doms)
7457 kfree(doms_cur);
7458 doms_cur = &fallback_doms;
7459}
7460
7461/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007462 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007463 * For now this just excludes isolated cpus, but could be used to
7464 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007465 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007466static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007467{
Milton Miller73785472007-10-24 18:23:48 +02007468 int err;
7469
Heiko Carstens22e52b02008-03-12 18:31:59 +01007470 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007471 ndoms_cur = 1;
7472 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7473 if (!doms_cur)
7474 doms_cur = &fallback_doms;
7475 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007476 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007477 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007478 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007479
7480 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007481}
7482
Mike Travis7c16ec52008-04-04 18:11:11 -07007483static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7484 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485{
Mike Travis7c16ec52008-04-04 18:11:11 -07007486 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007487}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007489/*
7490 * Detach sched domains from a group of cpus specified in cpu_map
7491 * These cpus will now be attached to the NULL domain
7492 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007493static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007494{
Mike Travis7c16ec52008-04-04 18:11:11 -07007495 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007496 int i;
7497
Milton Miller6382bc92007-10-15 17:00:19 +02007498 unregister_sched_domain_sysctl();
7499
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007500 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007501 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007502 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007503 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007504}
7505
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007506/* handle null as "default" */
7507static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7508 struct sched_domain_attr *new, int idx_new)
7509{
7510 struct sched_domain_attr tmp;
7511
7512 /* fast path */
7513 if (!new && !cur)
7514 return 1;
7515
7516 tmp = SD_ATTR_INIT;
7517 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7518 new ? (new + idx_new) : &tmp,
7519 sizeof(struct sched_domain_attr));
7520}
7521
Paul Jackson029190c2007-10-18 23:40:20 -07007522/*
7523 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007524 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007525 * doms_new[] to the current sched domain partitioning, doms_cur[].
7526 * It destroys each deleted domain and builds each new domain.
7527 *
7528 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007529 * The masks don't intersect (don't overlap.) We should setup one
7530 * sched domain for each mask. CPUs not in any of the cpumasks will
7531 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007532 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7533 * it as it is.
7534 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007535 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7536 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007537 * failed the kmalloc call, then it can pass in doms_new == NULL,
7538 * and partition_sched_domains() will fallback to the single partition
7539 * 'fallback_doms'.
7540 *
7541 * Call with hotplug lock held
7542 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007543void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7544 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007545{
7546 int i, j;
7547
Heiko Carstens712555e2008-04-28 11:33:07 +02007548 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007549
Milton Miller73785472007-10-24 18:23:48 +02007550 /* always unregister in case we don't destroy any domains */
7551 unregister_sched_domain_sysctl();
7552
Paul Jackson029190c2007-10-18 23:40:20 -07007553 if (doms_new == NULL) {
7554 ndoms_new = 1;
7555 doms_new = &fallback_doms;
7556 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007557 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007558 }
7559
7560 /* Destroy deleted domains */
7561 for (i = 0; i < ndoms_cur; i++) {
7562 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007563 if (cpus_equal(doms_cur[i], doms_new[j])
7564 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007565 goto match1;
7566 }
7567 /* no match - a current sched domain not in new doms_new[] */
7568 detach_destroy_domains(doms_cur + i);
7569match1:
7570 ;
7571 }
7572
7573 /* Build new domains */
7574 for (i = 0; i < ndoms_new; i++) {
7575 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007576 if (cpus_equal(doms_new[i], doms_cur[j])
7577 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007578 goto match2;
7579 }
7580 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007581 __build_sched_domains(doms_new + i,
7582 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007583match2:
7584 ;
7585 }
7586
7587 /* Remember the new sched domains */
7588 if (doms_cur != &fallback_doms)
7589 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007590 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007591 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007592 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007593 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007594
7595 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007596
Heiko Carstens712555e2008-04-28 11:33:07 +02007597 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007598}
7599
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007600#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007601int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007602{
7603 int err;
7604
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007605 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007606 mutex_lock(&sched_domains_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007607 detach_destroy_domains(&cpu_online_map);
Max Krasnyansky5c8e1ed2008-05-29 11:17:01 -07007608 free_sched_domains();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007609 err = arch_init_sched_domains(&cpu_online_map);
Heiko Carstens712555e2008-04-28 11:33:07 +02007610 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007611 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007612
7613 return err;
7614}
7615
7616static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7617{
7618 int ret;
7619
7620 if (buf[0] != '0' && buf[0] != '1')
7621 return -EINVAL;
7622
7623 if (smt)
7624 sched_smt_power_savings = (buf[0] == '1');
7625 else
7626 sched_mc_power_savings = (buf[0] == '1');
7627
7628 ret = arch_reinit_sched_domains();
7629
7630 return ret ? ret : count;
7631}
7632
Adrian Bunk6707de002007-08-12 18:08:19 +02007633#ifdef CONFIG_SCHED_MC
7634static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7635{
7636 return sprintf(page, "%u\n", sched_mc_power_savings);
7637}
7638static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7639 const char *buf, size_t count)
7640{
7641 return sched_power_savings_store(buf, count, 0);
7642}
7643static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7644 sched_mc_power_savings_store);
7645#endif
7646
7647#ifdef CONFIG_SCHED_SMT
7648static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7649{
7650 return sprintf(page, "%u\n", sched_smt_power_savings);
7651}
7652static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7653 const char *buf, size_t count)
7654{
7655 return sched_power_savings_store(buf, count, 1);
7656}
7657static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7658 sched_smt_power_savings_store);
7659#endif
7660
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007661int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7662{
7663 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007664
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007665#ifdef CONFIG_SCHED_SMT
7666 if (smt_capable())
7667 err = sysfs_create_file(&cls->kset.kobj,
7668 &attr_sched_smt_power_savings.attr);
7669#endif
7670#ifdef CONFIG_SCHED_MC
7671 if (!err && mc_capable())
7672 err = sysfs_create_file(&cls->kset.kobj,
7673 &attr_sched_mc_power_savings.attr);
7674#endif
7675 return err;
7676}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007677#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007678
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007680 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007682 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683 * which will prevent rebalancing while the sched domains are recalculated.
7684 */
7685static int update_sched_domains(struct notifier_block *nfb,
7686 unsigned long action, void *hcpu)
7687{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007688 int cpu = (int)(long)hcpu;
7689
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007692 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007693 disable_runtime(cpu_rq(cpu));
7694 /* fall-through */
7695 case CPU_UP_PREPARE:
7696 case CPU_UP_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007697 detach_destroy_domains(&cpu_online_map);
Max Krasnyansky5c8e1ed2008-05-29 11:17:01 -07007698 free_sched_domains();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 return NOTIFY_OK;
7700
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007701
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007703 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007705 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007706 enable_runtime(cpu_rq(cpu));
7707 /* fall-through */
7708 case CPU_UP_CANCELED:
7709 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007711 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712 /*
7713 * Fall through and re-initialise the domains.
7714 */
7715 break;
7716 default:
7717 return NOTIFY_DONE;
7718 }
7719
Max Krasnyansky5c8e1ed2008-05-29 11:17:01 -07007720#ifndef CONFIG_CPUSETS
7721 /*
7722 * Create default domain partitioning if cpusets are disabled.
7723 * Otherwise we let cpusets rebuild the domains based on the
7724 * current setup.
7725 */
7726
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007728 arch_init_sched_domains(&cpu_online_map);
Max Krasnyansky5c8e1ed2008-05-29 11:17:01 -07007729#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730
7731 return NOTIFY_OK;
7732}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733
7734void __init sched_init_smp(void)
7735{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007736 cpumask_t non_isolated_cpus;
7737
Mike Travis434d53b2008-04-04 18:11:04 -07007738#if defined(CONFIG_NUMA)
7739 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7740 GFP_KERNEL);
7741 BUG_ON(sched_group_nodes_bycpu == NULL);
7742#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007743 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007744 mutex_lock(&sched_domains_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007745 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007746 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007747 if (cpus_empty(non_isolated_cpus))
7748 cpu_set(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007749 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007750 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751 /* XXX: Theoretical race here - CPU may be hotplugged now */
7752 hotcpu_notifier(update_sched_domains, 0);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007753 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007754
7755 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07007756 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007757 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007758 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759}
7760#else
7761void __init sched_init_smp(void)
7762{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007763 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764}
7765#endif /* CONFIG_SMP */
7766
7767int in_sched_functions(unsigned long addr)
7768{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769 return in_lock_functions(addr) ||
7770 (addr >= (unsigned long)__sched_text_start
7771 && addr < (unsigned long)__sched_text_end);
7772}
7773
Alexey Dobriyana9957442007-10-15 17:00:13 +02007774static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007775{
7776 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007777 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02007778#ifdef CONFIG_FAIR_GROUP_SCHED
7779 cfs_rq->rq = rq;
7780#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007781 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007782}
7783
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007784static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7785{
7786 struct rt_prio_array *array;
7787 int i;
7788
7789 array = &rt_rq->active;
7790 for (i = 0; i < MAX_RT_PRIO; i++) {
Dmitry Adamushko20b63312008-06-11 00:58:30 +02007791 INIT_LIST_HEAD(array->queue + i);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007792 __clear_bit(i, array->bitmap);
7793 }
7794 /* delimiter for bitsearch: */
7795 __set_bit(MAX_RT_PRIO, array->bitmap);
7796
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007797#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007798 rt_rq->highest_prio = MAX_RT_PRIO;
7799#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007800#ifdef CONFIG_SMP
7801 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007802 rt_rq->overloaded = 0;
7803#endif
7804
7805 rt_rq->rt_time = 0;
7806 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007807 rt_rq->rt_runtime = 0;
7808 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007809
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007810#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007811 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007812 rt_rq->rq = rq;
7813#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007814}
7815
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007816#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007817static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7818 struct sched_entity *se, int cpu, int add,
7819 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007820{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007821 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007822 tg->cfs_rq[cpu] = cfs_rq;
7823 init_cfs_rq(cfs_rq, rq);
7824 cfs_rq->tg = tg;
7825 if (add)
7826 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7827
7828 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007829 /* se could be NULL for init_task_group */
7830 if (!se)
7831 return;
7832
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007833 if (!parent)
7834 se->cfs_rq = &rq->cfs;
7835 else
7836 se->cfs_rq = parent->my_q;
7837
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007838 se->my_q = cfs_rq;
7839 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02007840 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007841 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007842}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007843#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007844
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007845#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007846static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
7847 struct sched_rt_entity *rt_se, int cpu, int add,
7848 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007849{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007850 struct rq *rq = cpu_rq(cpu);
7851
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007852 tg->rt_rq[cpu] = rt_rq;
7853 init_rt_rq(rt_rq, rq);
7854 rt_rq->tg = tg;
7855 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007856 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007857 if (add)
7858 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7859
7860 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007861 if (!rt_se)
7862 return;
7863
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007864 if (!parent)
7865 rt_se->rt_rq = &rq->rt;
7866 else
7867 rt_se->rt_rq = parent->my_q;
7868
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007869 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007870 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007871 INIT_LIST_HEAD(&rt_se->run_list);
7872}
7873#endif
7874
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875void __init sched_init(void)
7876{
Ingo Molnardd41f592007-07-09 18:51:59 +02007877 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007878 unsigned long alloc_size = 0, ptr;
7879
7880#ifdef CONFIG_FAIR_GROUP_SCHED
7881 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7882#endif
7883#ifdef CONFIG_RT_GROUP_SCHED
7884 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7885#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007886#ifdef CONFIG_USER_SCHED
7887 alloc_size *= 2;
7888#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007889 /*
7890 * As sched_init() is called before page_alloc is setup,
7891 * we use alloc_bootmem().
7892 */
7893 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07007894 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07007895
7896#ifdef CONFIG_FAIR_GROUP_SCHED
7897 init_task_group.se = (struct sched_entity **)ptr;
7898 ptr += nr_cpu_ids * sizeof(void **);
7899
7900 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
7901 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007902
7903#ifdef CONFIG_USER_SCHED
7904 root_task_group.se = (struct sched_entity **)ptr;
7905 ptr += nr_cpu_ids * sizeof(void **);
7906
7907 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
7908 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007909#endif /* CONFIG_USER_SCHED */
7910#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007911#ifdef CONFIG_RT_GROUP_SCHED
7912 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
7913 ptr += nr_cpu_ids * sizeof(void **);
7914
7915 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007916 ptr += nr_cpu_ids * sizeof(void **);
7917
7918#ifdef CONFIG_USER_SCHED
7919 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
7920 ptr += nr_cpu_ids * sizeof(void **);
7921
7922 root_task_group.rt_rq = (struct rt_rq **)ptr;
7923 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007924#endif /* CONFIG_USER_SCHED */
7925#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007926 }
Ingo Molnardd41f592007-07-09 18:51:59 +02007927
Gregory Haskins57d885f2008-01-25 21:08:18 +01007928#ifdef CONFIG_SMP
7929 init_defrootdomain();
7930#endif
7931
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007932 init_rt_bandwidth(&def_rt_bandwidth,
7933 global_rt_period(), global_rt_runtime());
7934
7935#ifdef CONFIG_RT_GROUP_SCHED
7936 init_rt_bandwidth(&init_task_group.rt_bandwidth,
7937 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007938#ifdef CONFIG_USER_SCHED
7939 init_rt_bandwidth(&root_task_group.rt_bandwidth,
7940 global_rt_period(), RUNTIME_INF);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007941#endif /* CONFIG_USER_SCHED */
7942#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007943
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007944#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007945 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007946 INIT_LIST_HEAD(&init_task_group.children);
7947
7948#ifdef CONFIG_USER_SCHED
7949 INIT_LIST_HEAD(&root_task_group.children);
7950 init_task_group.parent = &root_task_group;
7951 list_add(&init_task_group.siblings, &root_task_group.children);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007952#endif /* CONFIG_USER_SCHED */
7953#endif /* CONFIG_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007954
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007955 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007956 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957
7958 rq = cpu_rq(i);
7959 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07007960 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07007961 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007962 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007963 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007964#ifdef CONFIG_FAIR_GROUP_SCHED
7965 init_task_group.shares = init_task_group_load;
7966 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007967#ifdef CONFIG_CGROUP_SCHED
7968 /*
7969 * How much cpu bandwidth does init_task_group get?
7970 *
7971 * In case of task-groups formed thr' the cgroup filesystem, it
7972 * gets 100% of the cpu resources in the system. This overall
7973 * system cpu resource is divided among the tasks of
7974 * init_task_group and its child task-groups in a fair manner,
7975 * based on each entity's (task or task-group's) weight
7976 * (se->load.weight).
7977 *
7978 * In other words, if init_task_group has 10 tasks of weight
7979 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7980 * then A0's share of the cpu resource is:
7981 *
7982 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
7983 *
7984 * We achieve this by letting init_task_group's tasks sit
7985 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
7986 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007987 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007988#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007989 root_task_group.shares = NICE_0_LOAD;
7990 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007991 /*
7992 * In case of task-groups formed thr' the user id of tasks,
7993 * init_task_group represents tasks belonging to root user.
7994 * Hence it forms a sibling of all subsequent groups formed.
7995 * In this case, init_task_group gets only a fraction of overall
7996 * system cpu resource, based on the weight assigned to root
7997 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
7998 * by letting tasks of init_task_group sit in a separate cfs_rq
7999 * (init_cfs_rq) and having one entity represent this group of
8000 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8001 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008002 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008003 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008004 &per_cpu(init_sched_entity, i), i, 1,
8005 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008006
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008007#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008008#endif /* CONFIG_FAIR_GROUP_SCHED */
8009
8010 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008011#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008012 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008013#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008014 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008015#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008016 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008017 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008018 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008019 &per_cpu(init_sched_rt_entity, i), i, 1,
8020 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008021#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008022#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023
Ingo Molnardd41f592007-07-09 18:51:59 +02008024 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8025 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008027 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008028 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008030 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008032 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04008033 rq->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034 rq->migration_thread = NULL;
8035 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008036 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008038 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040 }
8041
Peter Williams2dd73a42006-06-27 02:54:34 -07008042 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008043
Avi Kivitye107be32007-07-26 13:40:43 +02008044#ifdef CONFIG_PREEMPT_NOTIFIERS
8045 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8046#endif
8047
Christoph Lameterc9819f42006-12-10 02:20:25 -08008048#ifdef CONFIG_SMP
8049 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8050#endif
8051
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008052#ifdef CONFIG_RT_MUTEXES
8053 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8054#endif
8055
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056 /*
8057 * The boot idle thread does lazy MMU switching as well:
8058 */
8059 atomic_inc(&init_mm.mm_count);
8060 enter_lazy_tlb(&init_mm, current);
8061
8062 /*
8063 * Make us the idle thread. Technically, schedule() should not be
8064 * called from this thread, however somewhere below it might be,
8065 * but because we are the idle thread, we just pick up running again
8066 * when this runqueue becomes "idle".
8067 */
8068 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008069 /*
8070 * During early bootup we pretend to be a normal task:
8071 */
8072 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008073
8074 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075}
8076
8077#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8078void __might_sleep(char *file, int line)
8079{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008080#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081 static unsigned long prev_jiffy; /* ratelimiting */
8082
8083 if ((in_atomic() || irqs_disabled()) &&
8084 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8085 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8086 return;
8087 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008088 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089 " context at %s:%d\n", file, line);
8090 printk("in_atomic():%d, irqs_disabled():%d\n",
8091 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008092 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008093 if (irqs_disabled())
8094 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008095 dump_stack();
8096 }
8097#endif
8098}
8099EXPORT_SYMBOL(__might_sleep);
8100#endif
8101
8102#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008103static void normalize_task(struct rq *rq, struct task_struct *p)
8104{
8105 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02008106
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008107 update_rq_clock(rq);
8108 on_rq = p->se.on_rq;
8109 if (on_rq)
8110 deactivate_task(rq, p, 0);
8111 __setscheduler(rq, p, SCHED_NORMAL, 0);
8112 if (on_rq) {
8113 activate_task(rq, p, 0);
8114 resched_task(rq->curr);
8115 }
8116}
8117
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118void normalize_rt_tasks(void)
8119{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008120 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008121 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008122 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008123
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008124 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008125 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008126 /*
8127 * Only normalize user tasks:
8128 */
8129 if (!p->mm)
8130 continue;
8131
Ingo Molnardd41f592007-07-09 18:51:59 +02008132 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008133#ifdef CONFIG_SCHEDSTATS
8134 p->se.wait_start = 0;
8135 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008136 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008137#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008138
8139 if (!rt_task(p)) {
8140 /*
8141 * Renice negative nice level userspace
8142 * tasks back to 0:
8143 */
8144 if (TASK_NICE(p) < 0 && p->mm)
8145 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008146 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008149 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008150 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151
Ingo Molnar178be792007-10-15 17:00:18 +02008152 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008153
Ingo Molnarb29739f2006-06-27 02:54:51 -07008154 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008155 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008156 } while_each_thread(g, p);
8157
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008158 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159}
8160
8161#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008162
8163#ifdef CONFIG_IA64
8164/*
8165 * These functions are only useful for the IA64 MCA handling.
8166 *
8167 * They can only be called when the whole system has been
8168 * stopped - every CPU needs to be quiescent, and no scheduling
8169 * activity can take place. Using them for anything else would
8170 * be a serious bug, and as a result, they aren't even visible
8171 * under any other configuration.
8172 */
8173
8174/**
8175 * curr_task - return the current task for a given cpu.
8176 * @cpu: the processor in question.
8177 *
8178 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8179 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008180struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008181{
8182 return cpu_curr(cpu);
8183}
8184
8185/**
8186 * set_curr_task - set the current task for a given cpu.
8187 * @cpu: the processor in question.
8188 * @p: the task pointer to set.
8189 *
8190 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008191 * are serviced on a separate stack. It allows the architecture to switch the
8192 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008193 * must be called with all CPU's synchronized, and interrupts disabled, the
8194 * and caller must save the original value of the current task (see
8195 * curr_task() above) and restore that value before reenabling interrupts and
8196 * re-starting the system.
8197 *
8198 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8199 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008200void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008201{
8202 cpu_curr(cpu) = p;
8203}
8204
8205#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008206
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008207#ifdef CONFIG_FAIR_GROUP_SCHED
8208static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008209{
8210 int i;
8211
8212 for_each_possible_cpu(i) {
8213 if (tg->cfs_rq)
8214 kfree(tg->cfs_rq[i]);
8215 if (tg->se)
8216 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008217 }
8218
8219 kfree(tg->cfs_rq);
8220 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008221}
8222
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008223static
8224int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008225{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008226 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008227 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008228 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008229 int i;
8230
Mike Travis434d53b2008-04-04 18:11:04 -07008231 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008232 if (!tg->cfs_rq)
8233 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008234 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008235 if (!tg->se)
8236 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008237
8238 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008239
8240 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008241 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008242
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008243 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8244 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008245 if (!cfs_rq)
8246 goto err;
8247
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008248 se = kmalloc_node(sizeof(struct sched_entity),
8249 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008250 if (!se)
8251 goto err;
8252
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008253 parent_se = parent ? parent->se[i] : NULL;
8254 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008255 }
8256
8257 return 1;
8258
8259 err:
8260 return 0;
8261}
8262
8263static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8264{
8265 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8266 &cpu_rq(cpu)->leaf_cfs_rq_list);
8267}
8268
8269static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8270{
8271 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8272}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008273#else /* !CONFG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008274static inline void free_fair_sched_group(struct task_group *tg)
8275{
8276}
8277
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008278static inline
8279int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008280{
8281 return 1;
8282}
8283
8284static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8285{
8286}
8287
8288static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8289{
8290}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008291#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008292
8293#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008294static void free_rt_sched_group(struct task_group *tg)
8295{
8296 int i;
8297
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008298 destroy_rt_bandwidth(&tg->rt_bandwidth);
8299
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008300 for_each_possible_cpu(i) {
8301 if (tg->rt_rq)
8302 kfree(tg->rt_rq[i]);
8303 if (tg->rt_se)
8304 kfree(tg->rt_se[i]);
8305 }
8306
8307 kfree(tg->rt_rq);
8308 kfree(tg->rt_se);
8309}
8310
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008311static
8312int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008313{
8314 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008315 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008316 struct rq *rq;
8317 int i;
8318
Mike Travis434d53b2008-04-04 18:11:04 -07008319 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008320 if (!tg->rt_rq)
8321 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008322 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008323 if (!tg->rt_se)
8324 goto err;
8325
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008326 init_rt_bandwidth(&tg->rt_bandwidth,
8327 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008328
8329 for_each_possible_cpu(i) {
8330 rq = cpu_rq(i);
8331
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008332 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8333 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8334 if (!rt_rq)
8335 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008336
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008337 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8338 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8339 if (!rt_se)
8340 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008341
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008342 parent_se = parent ? parent->rt_se[i] : NULL;
8343 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008344 }
8345
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008346 return 1;
8347
8348 err:
8349 return 0;
8350}
8351
8352static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8353{
8354 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8355 &cpu_rq(cpu)->leaf_rt_rq_list);
8356}
8357
8358static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8359{
8360 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8361}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008362#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008363static inline void free_rt_sched_group(struct task_group *tg)
8364{
8365}
8366
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008367static inline
8368int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008369{
8370 return 1;
8371}
8372
8373static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8374{
8375}
8376
8377static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8378{
8379}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008380#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008381
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008382#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008383static void free_sched_group(struct task_group *tg)
8384{
8385 free_fair_sched_group(tg);
8386 free_rt_sched_group(tg);
8387 kfree(tg);
8388}
8389
8390/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008391struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008392{
8393 struct task_group *tg;
8394 unsigned long flags;
8395 int i;
8396
8397 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8398 if (!tg)
8399 return ERR_PTR(-ENOMEM);
8400
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008401 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008402 goto err;
8403
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008404 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008405 goto err;
8406
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008407 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008408 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008409 register_fair_sched_group(tg, i);
8410 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008411 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008412 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008413
8414 WARN_ON(!parent); /* root should already exist */
8415
8416 tg->parent = parent;
8417 list_add_rcu(&tg->siblings, &parent->children);
8418 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008419 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008420
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008421 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008422
8423err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008424 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008425 return ERR_PTR(-ENOMEM);
8426}
8427
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008428/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008429static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008430{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008431 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008432 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008433}
8434
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008435/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008436void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008437{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008438 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008439 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008440
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008441 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008442 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008443 unregister_fair_sched_group(tg, i);
8444 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008445 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008446 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008447 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008448 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008449
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008450 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008451 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008452}
8453
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008454/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008455 * The caller of this function should have put the task in its new group
8456 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8457 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008458 */
8459void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008460{
8461 int on_rq, running;
8462 unsigned long flags;
8463 struct rq *rq;
8464
8465 rq = task_rq_lock(tsk, &flags);
8466
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008467 update_rq_clock(rq);
8468
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008469 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008470 on_rq = tsk->se.on_rq;
8471
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008472 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008473 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008474 if (unlikely(running))
8475 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008476
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008477 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008478
Peter Zijlstra810b3812008-02-29 15:21:01 -05008479#ifdef CONFIG_FAIR_GROUP_SCHED
8480 if (tsk->sched_class->moved_group)
8481 tsk->sched_class->moved_group(tsk);
8482#endif
8483
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008484 if (unlikely(running))
8485 tsk->sched_class->set_curr_task(rq);
8486 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008487 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008488
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008489 task_rq_unlock(rq, &flags);
8490}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008491#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008492
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008493#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008494static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008495{
8496 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008497 int on_rq;
8498
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008499 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008500 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008501 dequeue_entity(cfs_rq, se, 0);
8502
8503 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008504 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008505
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008506 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008507 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008508}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008509
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008510static void set_se_shares(struct sched_entity *se, unsigned long shares)
8511{
8512 struct cfs_rq *cfs_rq = se->cfs_rq;
8513 struct rq *rq = cfs_rq->rq;
8514 unsigned long flags;
8515
8516 spin_lock_irqsave(&rq->lock, flags);
8517 __set_se_shares(se, shares);
8518 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008519}
8520
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008521static DEFINE_MUTEX(shares_mutex);
8522
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008523int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008524{
8525 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008526 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008527
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008528 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008529 * We can't change the weight of the root cgroup.
8530 */
8531 if (!tg->se[0])
8532 return -EINVAL;
8533
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008534 if (shares < MIN_SHARES)
8535 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008536 else if (shares > MAX_SHARES)
8537 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008538
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008539 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008540 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008541 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008542
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008543 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008544 for_each_possible_cpu(i)
8545 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008546 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008547 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008548
8549 /* wait for any ongoing reference to this group to finish */
8550 synchronize_sched();
8551
8552 /*
8553 * Now we are free to modify the group's share on each cpu
8554 * w/o tripping rebalance_share or load_balance_fair.
8555 */
8556 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008557 for_each_possible_cpu(i) {
8558 /*
8559 * force a rebalance
8560 */
8561 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008562 set_se_shares(tg->se[i], shares);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008563 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008564
8565 /*
8566 * Enable load balance activity on this group, by inserting it back on
8567 * each cpu's rq->leaf_cfs_rq_list.
8568 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008569 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008570 for_each_possible_cpu(i)
8571 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008572 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008573 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008574done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008575 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008576 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008577}
8578
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008579unsigned long sched_group_shares(struct task_group *tg)
8580{
8581 return tg->shares;
8582}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008583#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008584
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008585#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008586/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008587 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008588 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008589static DEFINE_MUTEX(rt_constraints_mutex);
8590
8591static unsigned long to_ratio(u64 period, u64 runtime)
8592{
8593 if (runtime == RUNTIME_INF)
8594 return 1ULL << 16;
8595
Roman Zippel6f6d6a12008-05-01 04:34:28 -07008596 return div64_u64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008597}
8598
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008599#ifdef CONFIG_CGROUP_SCHED
8600static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8601{
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008602 struct task_group *tgi, *parent = tg->parent;
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008603 unsigned long total = 0;
8604
8605 if (!parent) {
8606 if (global_rt_period() < period)
8607 return 0;
8608
8609 return to_ratio(period, runtime) <
8610 to_ratio(global_rt_period(), global_rt_runtime());
8611 }
8612
8613 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8614 return 0;
8615
8616 rcu_read_lock();
8617 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8618 if (tgi == tg)
8619 continue;
8620
8621 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8622 tgi->rt_bandwidth.rt_runtime);
8623 }
8624 rcu_read_unlock();
8625
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008626 return total + to_ratio(period, runtime) <=
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008627 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8628 parent->rt_bandwidth.rt_runtime);
8629}
8630#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008631static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008632{
8633 struct task_group *tgi;
8634 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008635 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008636 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008637
8638 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008639 list_for_each_entry_rcu(tgi, &task_groups, list) {
8640 if (tgi == tg)
8641 continue;
8642
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008643 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8644 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008645 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008646 rcu_read_unlock();
8647
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008648 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008649}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008650#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008651
Dhaval Giani521f1a242008-02-28 15:21:56 +05308652/* Must be called with tasklist_lock held */
8653static inline int tg_has_rt_tasks(struct task_group *tg)
8654{
8655 struct task_struct *g, *p;
8656 do_each_thread(g, p) {
8657 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8658 return 1;
8659 } while_each_thread(g, p);
8660 return 0;
8661}
8662
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008663static int tg_set_bandwidth(struct task_group *tg,
8664 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008665{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008666 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008667
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008668 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308669 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008670 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308671 err = -EBUSY;
8672 goto unlock;
8673 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008674 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8675 err = -EINVAL;
8676 goto unlock;
8677 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008678
8679 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008680 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8681 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008682
8683 for_each_possible_cpu(i) {
8684 struct rt_rq *rt_rq = tg->rt_rq[i];
8685
8686 spin_lock(&rt_rq->rt_runtime_lock);
8687 rt_rq->rt_runtime = rt_runtime;
8688 spin_unlock(&rt_rq->rt_runtime_lock);
8689 }
8690 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008691 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308692 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008693 mutex_unlock(&rt_constraints_mutex);
8694
8695 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008696}
8697
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008698int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8699{
8700 u64 rt_runtime, rt_period;
8701
8702 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8703 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8704 if (rt_runtime_us < 0)
8705 rt_runtime = RUNTIME_INF;
8706
8707 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8708}
8709
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008710long sched_group_rt_runtime(struct task_group *tg)
8711{
8712 u64 rt_runtime_us;
8713
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008714 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008715 return -1;
8716
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008717 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008718 do_div(rt_runtime_us, NSEC_PER_USEC);
8719 return rt_runtime_us;
8720}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008721
8722int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8723{
8724 u64 rt_runtime, rt_period;
8725
8726 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8727 rt_runtime = tg->rt_bandwidth.rt_runtime;
8728
8729 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8730}
8731
8732long sched_group_rt_period(struct task_group *tg)
8733{
8734 u64 rt_period_us;
8735
8736 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8737 do_div(rt_period_us, NSEC_PER_USEC);
8738 return rt_period_us;
8739}
8740
8741static int sched_rt_global_constraints(void)
8742{
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008743 struct task_group *tg = &root_task_group;
8744 u64 rt_runtime, rt_period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008745 int ret = 0;
8746
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008747 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8748 rt_runtime = tg->rt_bandwidth.rt_runtime;
8749
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008750 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008751 if (!__rt_schedulable(tg, rt_period, rt_runtime))
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008752 ret = -EINVAL;
8753 mutex_unlock(&rt_constraints_mutex);
8754
8755 return ret;
8756}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008757#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008758static int sched_rt_global_constraints(void)
8759{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008760 unsigned long flags;
8761 int i;
8762
8763 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
8764 for_each_possible_cpu(i) {
8765 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8766
8767 spin_lock(&rt_rq->rt_runtime_lock);
8768 rt_rq->rt_runtime = global_rt_runtime();
8769 spin_unlock(&rt_rq->rt_runtime_lock);
8770 }
8771 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
8772
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008773 return 0;
8774}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008775#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008776
8777int sched_rt_handler(struct ctl_table *table, int write,
8778 struct file *filp, void __user *buffer, size_t *lenp,
8779 loff_t *ppos)
8780{
8781 int ret;
8782 int old_period, old_runtime;
8783 static DEFINE_MUTEX(mutex);
8784
8785 mutex_lock(&mutex);
8786 old_period = sysctl_sched_rt_period;
8787 old_runtime = sysctl_sched_rt_runtime;
8788
8789 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8790
8791 if (!ret && write) {
8792 ret = sched_rt_global_constraints();
8793 if (ret) {
8794 sysctl_sched_rt_period = old_period;
8795 sysctl_sched_rt_runtime = old_runtime;
8796 } else {
8797 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8798 def_rt_bandwidth.rt_period =
8799 ns_to_ktime(global_rt_period());
8800 }
8801 }
8802 mutex_unlock(&mutex);
8803
8804 return ret;
8805}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008806
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008807#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008808
8809/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008810static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008811{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008812 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8813 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008814}
8815
8816static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008817cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008818{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008819 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008820
Paul Menage2b01dfe2007-10-24 18:23:50 +02008821 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008822 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008823 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008824 return &init_task_group.css;
8825 }
8826
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008827 parent = cgroup_tg(cgrp->parent);
8828 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008829 if (IS_ERR(tg))
8830 return ERR_PTR(-ENOMEM);
8831
8832 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008833 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008834
8835 return &tg->css;
8836}
8837
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008838static void
8839cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008840{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008841 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008842
8843 sched_destroy_group(tg);
8844}
8845
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008846static int
8847cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8848 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008849{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008850#ifdef CONFIG_RT_GROUP_SCHED
8851 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008852 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008853 return -EINVAL;
8854#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008855 /* We don't support RT-tasks being in separate groups */
8856 if (tsk->sched_class != &fair_sched_class)
8857 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008858#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008859
8860 return 0;
8861}
8862
8863static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008864cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008865 struct cgroup *old_cont, struct task_struct *tsk)
8866{
8867 sched_move_task(tsk);
8868}
8869
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008870#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07008871static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02008872 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008873{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008874 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008875}
8876
Paul Menagef4c753b2008-04-29 00:59:56 -07008877static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008878{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008879 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008880
8881 return (u64) tg->shares;
8882}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008883#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008884
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008885#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07008886static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07008887 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008888{
Paul Menage06ecb272008-04-29 01:00:06 -07008889 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008890}
8891
Paul Menage06ecb272008-04-29 01:00:06 -07008892static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008893{
Paul Menage06ecb272008-04-29 01:00:06 -07008894 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008895}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008896
8897static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8898 u64 rt_period_us)
8899{
8900 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
8901}
8902
8903static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
8904{
8905 return sched_group_rt_period(cgroup_tg(cgrp));
8906}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008907#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008908
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008909static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008910#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008911 {
8912 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008913 .read_u64 = cpu_shares_read_u64,
8914 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008915 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008916#endif
8917#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008918 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008919 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008920 .read_s64 = cpu_rt_runtime_read,
8921 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008922 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008923 {
8924 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008925 .read_u64 = cpu_rt_period_read_uint,
8926 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008927 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008928#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008929};
8930
8931static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8932{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008933 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008934}
8935
8936struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008937 .name = "cpu",
8938 .create = cpu_cgroup_create,
8939 .destroy = cpu_cgroup_destroy,
8940 .can_attach = cpu_cgroup_can_attach,
8941 .attach = cpu_cgroup_attach,
8942 .populate = cpu_cgroup_populate,
8943 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008944 .early_init = 1,
8945};
8946
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008947#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008948
8949#ifdef CONFIG_CGROUP_CPUACCT
8950
8951/*
8952 * CPU accounting code for task groups.
8953 *
8954 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8955 * (balbir@in.ibm.com).
8956 */
8957
8958/* track cpu usage of a group of tasks */
8959struct cpuacct {
8960 struct cgroup_subsys_state css;
8961 /* cpuusage holds pointer to a u64-type object on every cpu */
8962 u64 *cpuusage;
8963};
8964
8965struct cgroup_subsys cpuacct_subsys;
8966
8967/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308968static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008969{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308970 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008971 struct cpuacct, css);
8972}
8973
8974/* return cpu accounting group to which this task belongs */
8975static inline struct cpuacct *task_ca(struct task_struct *tsk)
8976{
8977 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8978 struct cpuacct, css);
8979}
8980
8981/* create a new cpu accounting group */
8982static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05308983 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008984{
8985 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
8986
8987 if (!ca)
8988 return ERR_PTR(-ENOMEM);
8989
8990 ca->cpuusage = alloc_percpu(u64);
8991 if (!ca->cpuusage) {
8992 kfree(ca);
8993 return ERR_PTR(-ENOMEM);
8994 }
8995
8996 return &ca->css;
8997}
8998
8999/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009000static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309001cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009002{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309003 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009004
9005 free_percpu(ca->cpuusage);
9006 kfree(ca);
9007}
9008
9009/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309010static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009011{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309012 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009013 u64 totalcpuusage = 0;
9014 int i;
9015
9016 for_each_possible_cpu(i) {
9017 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9018
9019 /*
9020 * Take rq->lock to make 64-bit addition safe on 32-bit
9021 * platforms.
9022 */
9023 spin_lock_irq(&cpu_rq(i)->lock);
9024 totalcpuusage += *cpuusage;
9025 spin_unlock_irq(&cpu_rq(i)->lock);
9026 }
9027
9028 return totalcpuusage;
9029}
9030
Dhaval Giani0297b802008-02-29 10:02:44 +05309031static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9032 u64 reset)
9033{
9034 struct cpuacct *ca = cgroup_ca(cgrp);
9035 int err = 0;
9036 int i;
9037
9038 if (reset) {
9039 err = -EINVAL;
9040 goto out;
9041 }
9042
9043 for_each_possible_cpu(i) {
9044 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9045
9046 spin_lock_irq(&cpu_rq(i)->lock);
9047 *cpuusage = 0;
9048 spin_unlock_irq(&cpu_rq(i)->lock);
9049 }
9050out:
9051 return err;
9052}
9053
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009054static struct cftype files[] = {
9055 {
9056 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009057 .read_u64 = cpuusage_read,
9058 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009059 },
9060};
9061
Dhaval Giani32cd7562008-02-29 10:02:43 +05309062static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009063{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309064 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009065}
9066
9067/*
9068 * charge this task's execution time to its accounting group.
9069 *
9070 * called with rq->lock held.
9071 */
9072static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9073{
9074 struct cpuacct *ca;
9075
9076 if (!cpuacct_subsys.active)
9077 return;
9078
9079 ca = task_ca(tsk);
9080 if (ca) {
9081 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9082
9083 *cpuusage += cputime;
9084 }
9085}
9086
9087struct cgroup_subsys cpuacct_subsys = {
9088 .name = "cpuacct",
9089 .create = cpuacct_create,
9090 .destroy = cpuacct_destroy,
9091 .populate = cpuacct_populate,
9092 .subsys_id = cpuacct_subsys_id,
9093};
9094#endif /* CONFIG_CGROUP_CPUACCT */