blob: 327f91c63c992c6141c0971c9b1b1f5fd7240134 [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>
Alexey Dobriyanb5aadf72008-10-06 13:23:43 +040058#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020060#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/syscalls.h>
62#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070063#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080064#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070065#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070066#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020067#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020068#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010069#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070070#include <linux/tick.h>
Mike Travis434d53b2008-04-04 18:11:04 -070071#include <linux/bootmem.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020072#include <linux/debugfs.h>
73#include <linux/ctype.h>
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020074#include <linux/ftrace.h>
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -040075#include <trace/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Eric Dumazet5517d862007-05-08 00:32:57 -070077#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020078#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Gregory Haskins6e0534f2008-05-12 21:21:01 +020080#include "sched_cpupri.h"
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
83 * Convert user-nice values [ -20 ... 0 ... 19 ]
84 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
85 * and back.
86 */
87#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
88#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
89#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
90
91/*
92 * 'User priority' is the nice value converted to something we
93 * can work with better when scaling various scheduler parameters,
94 * it's a [ 0 ... 39 ] range.
95 */
96#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
97#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
98#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
99
100/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100101 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100103#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200105#define NICE_0_LOAD SCHED_LOAD_SCALE
106#define NICE_0_SHIFT SCHED_LOAD_SHIFT
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * These are the 'tuning knobs' of the scheduler:
110 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200111 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * Timeslices get refilled after they expire.
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700115
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200116/*
117 * single value that denotes runtime == period, ie unlimited time.
118 */
119#define RUNTIME_INF ((u64)~0ULL)
120
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -0500121DEFINE_TRACE(sched_wait_task);
122DEFINE_TRACE(sched_wakeup);
123DEFINE_TRACE(sched_wakeup_new);
124DEFINE_TRACE(sched_switch);
125DEFINE_TRACE(sched_migrate_task);
126
Eric Dumazet5517d862007-05-08 00:32:57 -0700127#ifdef CONFIG_SMP
128/*
129 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
130 * Since cpu_power is a 'constant', we can use a reciprocal divide.
131 */
132static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
133{
134 return reciprocal_divide(load, sg->reciprocal_cpu_power);
135}
136
137/*
138 * Each time a sched group cpu_power is changed,
139 * we must compute its reciprocal value
140 */
141static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
142{
143 sg->__cpu_power += val;
144 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
145}
146#endif
147
Ingo Molnare05606d2007-07-09 18:51:59 +0200148static inline int rt_policy(int policy)
149{
Roel Kluin3f33a7c2008-05-13 23:44:11 +0200150 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
Ingo Molnare05606d2007-07-09 18:51:59 +0200151 return 1;
152 return 0;
153}
154
155static inline int task_has_rt_policy(struct task_struct *p)
156{
157 return rt_policy(p->policy);
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200161 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200163struct rt_prio_array {
164 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
165 struct list_head queue[MAX_RT_PRIO];
166};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200168struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100169 /* nests inside the rq lock: */
170 spinlock_t rt_runtime_lock;
171 ktime_t rt_period;
172 u64 rt_runtime;
173 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200174};
175
176static struct rt_bandwidth def_rt_bandwidth;
177
178static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
179
180static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
181{
182 struct rt_bandwidth *rt_b =
183 container_of(timer, struct rt_bandwidth, rt_period_timer);
184 ktime_t now;
185 int overrun;
186 int idle = 0;
187
188 for (;;) {
189 now = hrtimer_cb_get_time(timer);
190 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
191
192 if (!overrun)
193 break;
194
195 idle = do_sched_rt_period_timer(rt_b, overrun);
196 }
197
198 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
199}
200
201static
202void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
203{
204 rt_b->rt_period = ns_to_ktime(period);
205 rt_b->rt_runtime = runtime;
206
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200207 spin_lock_init(&rt_b->rt_runtime_lock);
208
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200209 hrtimer_init(&rt_b->rt_period_timer,
210 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
211 rt_b->rt_period_timer.function = sched_rt_period_timer;
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +0200212 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_UNLOCKED;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200213}
214
Krzysztof Heltc8bfff62008-09-05 23:46:19 +0200215static inline int rt_bandwidth_enabled(void)
216{
217 return sysctl_sched_rt_runtime >= 0;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200218}
219
220static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
221{
222 ktime_t now;
223
Peter Zijlstra0b148fa2008-08-19 12:33:04 +0200224 if (rt_bandwidth_enabled() && rt_b->rt_runtime == RUNTIME_INF)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200225 return;
226
227 if (hrtimer_active(&rt_b->rt_period_timer))
228 return;
229
230 spin_lock(&rt_b->rt_runtime_lock);
231 for (;;) {
232 if (hrtimer_active(&rt_b->rt_period_timer))
233 break;
234
235 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
236 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700237 hrtimer_start_expires(&rt_b->rt_period_timer,
238 HRTIMER_MODE_ABS);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200239 }
240 spin_unlock(&rt_b->rt_runtime_lock);
241}
242
243#ifdef CONFIG_RT_GROUP_SCHED
244static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
245{
246 hrtimer_cancel(&rt_b->rt_period_timer);
247}
248#endif
249
Heiko Carstens712555e2008-04-28 11:33:07 +0200250/*
251 * sched_domains_mutex serializes calls to arch_init_sched_domains,
252 * detach_destroy_domains and partition_sched_domains.
253 */
254static DEFINE_MUTEX(sched_domains_mutex);
255
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100256#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200257
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700258#include <linux/cgroup.h>
259
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200260struct cfs_rq;
261
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100262static LIST_HEAD(task_groups);
263
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200264/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200265struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100266#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700267 struct cgroup_subsys_state css;
268#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100269
270#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200271 /* schedulable entities of this group on each cpu */
272 struct sched_entity **se;
273 /* runqueue "owned" by this group on each cpu */
274 struct cfs_rq **cfs_rq;
275 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100276#endif
277
278#ifdef CONFIG_RT_GROUP_SCHED
279 struct sched_rt_entity **rt_se;
280 struct rt_rq **rt_rq;
281
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200282 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100283#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100284
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100285 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100286 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200287
288 struct task_group *parent;
289 struct list_head siblings;
290 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200291};
292
Dhaval Giani354d60c2008-04-19 19:44:59 +0200293#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200294
295/*
296 * Root task group.
297 * Every UID task group (including init_task_group aka UID-0) will
298 * be a child to this group.
299 */
300struct task_group root_task_group;
301
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100302#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200303/* Default task group's sched entity on each cpu */
304static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
305/* Default task group's cfs_rq on each cpu */
306static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200307#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100308
309#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100310static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
311static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200312#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +0200313#else /* !CONFIG_USER_SCHED */
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200314#define root_task_group init_task_group
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +0200315#endif /* CONFIG_USER_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100316
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100317/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100318 * a task group's cpu shares.
319 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100320static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100321
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100322#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100323#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100324# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200325#else /* !CONFIG_USER_SCHED */
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100326# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200327#endif /* CONFIG_USER_SCHED */
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200328
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800329/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800330 * A weight of 0 or 1 can cause arithmetics problems.
331 * A weight of a cfs_rq is the sum of weights of which entities
332 * are queued on this cfs_rq, so a weight of a entity should not be
333 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800334 * (The default weight is 1024 - so there's no practical
335 * limitation from this.)
336 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200337#define MIN_SHARES 2
Lai Jiangshan2e084782008-06-12 16:42:58 +0800338#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200339
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100340static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100341#endif
342
343/* Default task group.
344 * Every task in system belong to this group at bootup.
345 */
Mike Travis434d53b2008-04-04 18:11:04 -0700346struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200347
348/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200349static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200350{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200351 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200352
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100353#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200354 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100355#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700356 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
357 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200358#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100359 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200360#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200361 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200362}
363
364/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100365static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200366{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100367#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100368 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
369 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100370#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100371
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100372#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100373 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
374 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100375#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200376}
377
378#else
379
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100380static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Peter Zijlstra83378262008-06-27 13:41:37 +0200381static inline struct task_group *task_group(struct task_struct *p)
382{
383 return NULL;
384}
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200385
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100386#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200387
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200388/* CFS-related fields in a runqueue */
389struct cfs_rq {
390 struct load_weight load;
391 unsigned long nr_running;
392
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200393 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200394 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200395
396 struct rb_root tasks_timeline;
397 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200398
399 struct list_head tasks;
400 struct list_head *balance_iterator;
401
402 /*
403 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200404 * It is set to NULL otherwise (i.e when none are currently running).
405 */
Peter Zijlstra47932412008-11-04 21:25:09 +0100406 struct sched_entity *curr, *next, *last;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200407
Peter Zijlstra5ac5c4d2008-11-10 10:46:32 +0100408 unsigned int nr_spread_over;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200409
Ingo Molnar62160e32007-10-15 17:00:03 +0200410#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200411 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
412
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100413 /*
414 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200415 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
416 * (like users, containers etc.)
417 *
418 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
419 * list is used during load balance.
420 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100421 struct list_head leaf_cfs_rq_list;
422 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200423
424#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200425 /*
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200426 * the part of load.weight contributed by tasks
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200427 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200428 unsigned long task_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200429
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200430 /*
431 * h_load = weight * f(tg)
432 *
433 * Where f(tg) is the recursive weight fraction assigned to
434 * this group.
435 */
436 unsigned long h_load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200437
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200438 /*
439 * this cpu's part of tg->shares
440 */
441 unsigned long shares;
Peter Zijlstraf1d239f2008-06-27 13:41:38 +0200442
443 /*
444 * load.weight at the time we set shares
445 */
446 unsigned long rq_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200447#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200448#endif
449};
450
451/* Real-Time classes' related field in a runqueue: */
452struct rt_rq {
453 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100454 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100455#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100456 int highest_prio; /* highest queued rt task prio */
457#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100458#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100459 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100460 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100461#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100462 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100463 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200464 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100465 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200466 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100467
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100468#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100469 unsigned long rt_nr_boosted;
470
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100471 struct rq *rq;
472 struct list_head leaf_rt_rq_list;
473 struct task_group *tg;
474 struct sched_rt_entity *rt_se;
475#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200476};
477
Gregory Haskins57d885f2008-01-25 21:08:18 +0100478#ifdef CONFIG_SMP
479
480/*
481 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100482 * variables. Each exclusive cpuset essentially defines an island domain by
483 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100484 * exclusive cpuset is created, we also create and attach a new root-domain
485 * object.
486 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100487 */
488struct root_domain {
489 atomic_t refcount;
490 cpumask_t span;
491 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100492
Ingo Molnar0eab9142008-01-25 21:08:19 +0100493 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100494 * The "RT overload" flag: it gets set if a CPU has more than
495 * one runnable RT task.
496 */
497 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100498 atomic_t rto_count;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200499#ifdef CONFIG_SMP
500 struct cpupri cpupri;
501#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +0100502};
503
Gregory Haskinsdc938522008-01-25 21:08:26 +0100504/*
505 * By default the system creates a single root-domain with all cpus as
506 * members (mimicking the global state we have today).
507 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100508static struct root_domain def_root_domain;
509
510#endif
511
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200512/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * This is the main, per-CPU runqueue data structure.
514 *
515 * Locking rule: those places that want to lock multiple runqueues
516 * (such as the load balancing or the thread migration code), lock
517 * acquire operations must be ordered by ascending &runqueue.
518 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700519struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200520 /* runqueue lock: */
521 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /*
524 * nr_running and cpu_load should be in the same cacheline because
525 * remote CPUs use both these fields when doing load calculation.
526 */
527 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200528 #define CPU_LOAD_IDX_MAX 5
529 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700530 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700531#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200532 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700533 unsigned char in_nohz_recently;
534#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200535 /* capture load from *all* tasks on this cpu: */
536 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200537 unsigned long nr_load_updates;
538 u64 nr_switches;
539
540 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100541 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100542
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200543#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200544 /* list of leaf cfs_rq on this cpu: */
545 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100546#endif
547#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100548 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
552 * This is part of a global counter where only the total sum
553 * over all CPUs matters. A task can increase this counter on
554 * one CPU and if it got migrated afterwards it may decrease
555 * it on another CPU. Always updated under the runqueue lock:
556 */
557 unsigned long nr_uninterruptible;
558
Ingo Molnar36c8b582006-07-03 00:25:41 -0700559 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800560 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200562
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200563 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 atomic_t nr_iowait;
566
567#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100568 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct sched_domain *sd;
570
571 /* For active balancing */
572 int active_balance;
573 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200574 /* cpu of this runqueue: */
575 int cpu;
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400576 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Peter Zijlstraa8a51d52008-06-27 13:41:26 +0200578 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Ingo Molnar36c8b582006-07-03 00:25:41 -0700580 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 struct list_head migration_queue;
582#endif
583
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100584#ifdef CONFIG_SCHED_HRTICK
Peter Zijlstra31656512008-07-18 18:01:23 +0200585#ifdef CONFIG_SMP
586 int hrtick_csd_pending;
587 struct call_single_data hrtick_csd;
588#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100589 struct hrtimer hrtick_timer;
590#endif
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592#ifdef CONFIG_SCHEDSTATS
593 /* latency stats */
594 struct sched_info rq_sched_info;
595
596 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200597 unsigned int yld_exp_empty;
598 unsigned int yld_act_empty;
599 unsigned int yld_both_empty;
600 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200603 unsigned int sched_switch;
604 unsigned int sched_count;
605 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200608 unsigned int ttwu_count;
609 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200610
611 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200612 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#endif
614};
615
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700616static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Peter Zijlstra15afe092008-09-20 23:38:02 +0200618static inline void check_preempt_curr(struct rq *rq, struct task_struct *p, int sync)
Ingo Molnardd41f592007-07-09 18:51:59 +0200619{
Peter Zijlstra15afe092008-09-20 23:38:02 +0200620 rq->curr->sched_class->check_preempt_curr(rq, p, sync);
Ingo Molnardd41f592007-07-09 18:51:59 +0200621}
622
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700623static inline int cpu_of(struct rq *rq)
624{
625#ifdef CONFIG_SMP
626 return rq->cpu;
627#else
628 return 0;
629#endif
630}
631
Ingo Molnar20d315d2007-07-09 18:51:58 +0200632/*
Nick Piggin674311d2005-06-25 14:57:27 -0700633 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700634 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700635 *
636 * The domain tree of any CPU may only be accessed from within
637 * preempt-disabled sections.
638 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700639#define for_each_domain(cpu, __sd) \
640 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
643#define this_rq() (&__get_cpu_var(runqueues))
644#define task_rq(p) cpu_rq(task_cpu(p))
645#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
646
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200647static inline void update_rq_clock(struct rq *rq)
648{
649 rq->clock = sched_clock_cpu(cpu_of(rq));
650}
651
Ingo Molnare436d802007-07-19 21:28:35 +0200652/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200653 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
654 */
655#ifdef CONFIG_SCHED_DEBUG
656# define const_debug __read_mostly
657#else
658# define const_debug static const
659#endif
660
Ingo Molnar017730c2008-05-12 21:20:52 +0200661/**
662 * runqueue_is_locked
663 *
664 * Returns true if the current cpu runqueue is locked.
665 * This interface allows printk to be called with the runqueue lock
666 * held and know whether or not it is OK to wake up the klogd.
667 */
668int runqueue_is_locked(void)
669{
670 int cpu = get_cpu();
671 struct rq *rq = cpu_rq(cpu);
672 int ret;
673
674 ret = spin_is_locked(&rq->lock);
675 put_cpu();
676 return ret;
677}
678
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200679/*
680 * Debugging: various feature bits
681 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200682
683#define SCHED_FEAT(name, enabled) \
684 __SCHED_FEAT_##name ,
685
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200686enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200687#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200688};
689
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200690#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200691
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200692#define SCHED_FEAT(name, enabled) \
693 (1UL << __SCHED_FEAT_##name) * enabled |
694
695const_debug unsigned int sysctl_sched_features =
696#include "sched_features.h"
697 0;
698
699#undef SCHED_FEAT
700
701#ifdef CONFIG_SCHED_DEBUG
702#define SCHED_FEAT(name, enabled) \
703 #name ,
704
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700705static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200706#include "sched_features.h"
707 NULL
708};
709
710#undef SCHED_FEAT
711
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700712static int sched_feat_open(struct inode *inode, struct file *filp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200713{
714 filp->private_data = inode->i_private;
715 return 0;
716}
717
718static ssize_t
719sched_feat_read(struct file *filp, char __user *ubuf,
720 size_t cnt, loff_t *ppos)
721{
722 char *buf;
723 int r = 0;
724 int len = 0;
725 int i;
726
727 for (i = 0; sched_feat_names[i]; i++) {
728 len += strlen(sched_feat_names[i]);
729 len += 4;
730 }
731
732 buf = kmalloc(len + 2, GFP_KERNEL);
733 if (!buf)
734 return -ENOMEM;
735
736 for (i = 0; sched_feat_names[i]; i++) {
737 if (sysctl_sched_features & (1UL << i))
738 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
739 else
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200740 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200741 }
742
743 r += sprintf(buf + r, "\n");
744 WARN_ON(r >= len + 2);
745
746 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
747
748 kfree(buf);
749
750 return r;
751}
752
753static ssize_t
754sched_feat_write(struct file *filp, const char __user *ubuf,
755 size_t cnt, loff_t *ppos)
756{
757 char buf[64];
758 char *cmp = buf;
759 int neg = 0;
760 int i;
761
762 if (cnt > 63)
763 cnt = 63;
764
765 if (copy_from_user(&buf, ubuf, cnt))
766 return -EFAULT;
767
768 buf[cnt] = 0;
769
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200770 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200771 neg = 1;
772 cmp += 3;
773 }
774
775 for (i = 0; sched_feat_names[i]; i++) {
776 int len = strlen(sched_feat_names[i]);
777
778 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
779 if (neg)
780 sysctl_sched_features &= ~(1UL << i);
781 else
782 sysctl_sched_features |= (1UL << i);
783 break;
784 }
785 }
786
787 if (!sched_feat_names[i])
788 return -EINVAL;
789
790 filp->f_pos += cnt;
791
792 return cnt;
793}
794
795static struct file_operations sched_feat_fops = {
796 .open = sched_feat_open,
797 .read = sched_feat_read,
798 .write = sched_feat_write,
799};
800
801static __init int sched_init_debug(void)
802{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200803 debugfs_create_file("sched_features", 0644, NULL, NULL,
804 &sched_feat_fops);
805
806 return 0;
807}
808late_initcall(sched_init_debug);
809
810#endif
811
812#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200813
814/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100815 * Number of tasks to iterate in a single balance run.
816 * Limited because this is done with IRQs disabled.
817 */
818const_debug unsigned int sysctl_sched_nr_migrate = 32;
819
820/*
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200821 * ratelimit for updating the group shares.
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200822 * default: 0.25ms
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200823 */
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200824unsigned int sysctl_sched_shares_ratelimit = 250000;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200825
826/*
Peter Zijlstraffda12a2008-10-17 19:27:02 +0200827 * Inject some fuzzyness into changing the per-cpu group shares
828 * this avoids remote rq-locks at the expense of fairness.
829 * default: 4
830 */
831unsigned int sysctl_sched_shares_thresh = 4;
832
833/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100834 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100835 * default: 1s
836 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100837unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100838
Ingo Molnar6892b752008-02-13 14:02:36 +0100839static __read_mostly int scheduler_running;
840
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100841/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100842 * part of the period that we allow rt tasks to run in us.
843 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100844 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100845int sysctl_sched_rt_runtime = 950000;
846
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200847static inline u64 global_rt_period(void)
848{
849 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
850}
851
852static inline u64 global_rt_runtime(void)
853{
roel kluine26873b2008-07-22 16:51:15 -0400854 if (sysctl_sched_rt_runtime < 0)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200855 return RUNTIME_INF;
856
857 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
858}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700861# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700863#ifndef finish_arch_switch
864# define finish_arch_switch(prev) do { } while (0)
865#endif
866
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100867static inline int task_current(struct rq *rq, struct task_struct *p)
868{
869 return rq->curr == p;
870}
871
Nick Piggin4866cde2005-06-25 14:57:23 -0700872#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700873static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700874{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100875 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700876}
877
Ingo Molnar70b97a72006-07-03 00:25:42 -0700878static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700879{
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{
Ingo Molnarda04c032005-09-13 11:17:59 +0200884#ifdef CONFIG_DEBUG_SPINLOCK
885 /* this is a valid case when another task releases the spinlock */
886 rq->lock.owner = current;
887#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700888 /*
889 * If we are tracking spinlock dependencies then we have to
890 * fix up the runqueue lock - which gets 'carried over' from
891 * prev into current:
892 */
893 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
894
Nick Piggin4866cde2005-06-25 14:57:23 -0700895 spin_unlock_irq(&rq->lock);
896}
897
898#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700899static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700900{
901#ifdef CONFIG_SMP
902 return p->oncpu;
903#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100904 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700905#endif
906}
907
Ingo Molnar70b97a72006-07-03 00:25:42 -0700908static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700909{
910#ifdef CONFIG_SMP
911 /*
912 * We can optimise this out completely for !SMP, because the
913 * SMP rebalancing from interrupt is the only thing that cares
914 * here.
915 */
916 next->oncpu = 1;
917#endif
918#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
919 spin_unlock_irq(&rq->lock);
920#else
921 spin_unlock(&rq->lock);
922#endif
923}
924
Ingo Molnar70b97a72006-07-03 00:25:42 -0700925static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700926{
927#ifdef CONFIG_SMP
928 /*
929 * After ->oncpu is cleared, the task can be moved to a different CPU.
930 * We must ensure this doesn't happen until the switch is completely
931 * finished.
932 */
933 smp_wmb();
934 prev->oncpu = 0;
935#endif
936#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
937 local_irq_enable();
938#endif
939}
940#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700943 * __task_rq_lock - lock the runqueue a given task resides on.
944 * Must be called interrupts disabled.
945 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700946static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700947 __acquires(rq->lock)
948{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200949 for (;;) {
950 struct rq *rq = task_rq(p);
951 spin_lock(&rq->lock);
952 if (likely(rq == task_rq(p)))
953 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700954 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700955 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700956}
957
958/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100960 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * explicitly disabling preemption.
962 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700963static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 __acquires(rq->lock)
965{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700966 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Andi Kleen3a5c3592007-10-15 17:00:14 +0200968 for (;;) {
969 local_irq_save(*flags);
970 rq = task_rq(p);
971 spin_lock(&rq->lock);
972 if (likely(rq == task_rq(p)))
973 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Oleg Nesterovad474ca2008-11-10 15:39:30 +0100978void task_rq_unlock_wait(struct task_struct *p)
979{
980 struct rq *rq = task_rq(p);
981
982 smp_mb(); /* spin-unlock-wait is not a full memory barrier */
983 spin_unlock_wait(&rq->lock);
984}
985
Alexey Dobriyana9957442007-10-15 17:00:13 +0200986static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700987 __releases(rq->lock)
988{
989 spin_unlock(&rq->lock);
990}
991
Ingo Molnar70b97a72006-07-03 00:25:42 -0700992static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 __releases(rq->lock)
994{
995 spin_unlock_irqrestore(&rq->lock, *flags);
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800999 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001001static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 __acquires(rq->lock)
1003{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001004 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 local_irq_disable();
1007 rq = this_rq();
1008 spin_lock(&rq->lock);
1009
1010 return rq;
1011}
1012
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001013#ifdef CONFIG_SCHED_HRTICK
1014/*
1015 * Use HR-timers to deliver accurate preemption points.
1016 *
1017 * Its all a bit involved since we cannot program an hrt while holding the
1018 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1019 * reschedule event.
1020 *
1021 * When we get rescheduled we reprogram the hrtick_timer outside of the
1022 * rq->lock.
1023 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001024
1025/*
1026 * Use hrtick when:
1027 * - enabled by features
1028 * - hrtimer is actually high res
1029 */
1030static inline int hrtick_enabled(struct rq *rq)
1031{
1032 if (!sched_feat(HRTICK))
1033 return 0;
Ingo Molnarba420592008-07-20 11:02:06 +02001034 if (!cpu_active(cpu_of(rq)))
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001035 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001036 return hrtimer_is_hres_active(&rq->hrtick_timer);
1037}
1038
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001039static void hrtick_clear(struct rq *rq)
1040{
1041 if (hrtimer_active(&rq->hrtick_timer))
1042 hrtimer_cancel(&rq->hrtick_timer);
1043}
1044
1045/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001046 * High-resolution timer tick.
1047 * Runs from hardirq context with interrupts disabled.
1048 */
1049static enum hrtimer_restart hrtick(struct hrtimer *timer)
1050{
1051 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1052
1053 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1054
1055 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001056 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001057 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1058 spin_unlock(&rq->lock);
1059
1060 return HRTIMER_NORESTART;
1061}
1062
Rabin Vincent95e904c72008-05-11 05:55:33 +05301063#ifdef CONFIG_SMP
Peter Zijlstra31656512008-07-18 18:01:23 +02001064/*
1065 * called from hardirq (IPI) context
1066 */
1067static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001068{
Peter Zijlstra31656512008-07-18 18:01:23 +02001069 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001070
Peter Zijlstra31656512008-07-18 18:01:23 +02001071 spin_lock(&rq->lock);
1072 hrtimer_restart(&rq->hrtick_timer);
1073 rq->hrtick_csd_pending = 0;
1074 spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001075}
1076
Peter Zijlstra31656512008-07-18 18:01:23 +02001077/*
1078 * Called to set the hrtick timer state.
1079 *
1080 * called with rq->lock held and irqs disabled
1081 */
1082static void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001083{
Peter Zijlstra31656512008-07-18 18:01:23 +02001084 struct hrtimer *timer = &rq->hrtick_timer;
1085 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001086
Arjan van de Vencc584b22008-09-01 15:02:30 -07001087 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +02001088
1089 if (rq == this_rq()) {
1090 hrtimer_restart(timer);
1091 } else if (!rq->hrtick_csd_pending) {
1092 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd);
1093 rq->hrtick_csd_pending = 1;
1094 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001095}
1096
1097static int
1098hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1099{
1100 int cpu = (int)(long)hcpu;
1101
1102 switch (action) {
1103 case CPU_UP_CANCELED:
1104 case CPU_UP_CANCELED_FROZEN:
1105 case CPU_DOWN_PREPARE:
1106 case CPU_DOWN_PREPARE_FROZEN:
1107 case CPU_DEAD:
1108 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +02001109 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001110 return NOTIFY_OK;
1111 }
1112
1113 return NOTIFY_DONE;
1114}
1115
Rakib Mullickfa748202008-09-22 14:55:45 -07001116static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001117{
1118 hotcpu_notifier(hotplug_hrtick, 0);
1119}
Peter Zijlstra31656512008-07-18 18:01:23 +02001120#else
1121/*
1122 * Called to set the hrtick timer state.
1123 *
1124 * called with rq->lock held and irqs disabled
1125 */
1126static void hrtick_start(struct rq *rq, u64 delay)
1127{
1128 hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), HRTIMER_MODE_REL);
1129}
1130
Andrew Morton006c75f2008-09-22 14:55:46 -07001131static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +02001132{
1133}
Rabin Vincent95e904c72008-05-11 05:55:33 +05301134#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001135
1136static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001137{
Peter Zijlstra31656512008-07-18 18:01:23 +02001138#ifdef CONFIG_SMP
1139 rq->hrtick_csd_pending = 0;
1140
1141 rq->hrtick_csd.flags = 0;
1142 rq->hrtick_csd.func = __hrtick_start;
1143 rq->hrtick_csd.info = rq;
1144#endif
1145
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001146 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1147 rq->hrtick_timer.function = hrtick;
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +02001148 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_PERCPU;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001149}
Andrew Morton006c75f2008-09-22 14:55:46 -07001150#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001151static inline void hrtick_clear(struct rq *rq)
1152{
1153}
1154
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001155static inline void init_rq_hrtick(struct rq *rq)
1156{
1157}
1158
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001159static inline void init_hrtick(void)
1160{
1161}
Andrew Morton006c75f2008-09-22 14:55:46 -07001162#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001163
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001164/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001165 * resched_task - mark a task 'to be rescheduled now'.
1166 *
1167 * On UP this means the setting of the need_resched flag, on SMP it
1168 * might also involve a cross-CPU call to trigger the scheduler on
1169 * the target CPU.
1170 */
1171#ifdef CONFIG_SMP
1172
1173#ifndef tsk_is_polling
1174#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1175#endif
1176
Peter Zijlstra31656512008-07-18 18:01:23 +02001177static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001178{
1179 int cpu;
1180
1181 assert_spin_locked(&task_rq(p)->lock);
1182
Peter Zijlstra31656512008-07-18 18:01:23 +02001183 if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001184 return;
1185
Peter Zijlstra31656512008-07-18 18:01:23 +02001186 set_tsk_thread_flag(p, TIF_NEED_RESCHED);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001187
1188 cpu = task_cpu(p);
1189 if (cpu == smp_processor_id())
1190 return;
1191
1192 /* NEED_RESCHED must be visible before we test polling */
1193 smp_mb();
1194 if (!tsk_is_polling(p))
1195 smp_send_reschedule(cpu);
1196}
1197
1198static void resched_cpu(int cpu)
1199{
1200 struct rq *rq = cpu_rq(cpu);
1201 unsigned long flags;
1202
1203 if (!spin_trylock_irqsave(&rq->lock, flags))
1204 return;
1205 resched_task(cpu_curr(cpu));
1206 spin_unlock_irqrestore(&rq->lock, flags);
1207}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001208
1209#ifdef CONFIG_NO_HZ
1210/*
1211 * When add_timer_on() enqueues a timer into the timer wheel of an
1212 * idle CPU then this timer might expire before the next timer event
1213 * which is scheduled to wake up that CPU. In case of a completely
1214 * idle system the next event might even be infinite time into the
1215 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1216 * leaves the inner idle loop so the newly added timer is taken into
1217 * account when the CPU goes back to idle and evaluates the timer
1218 * wheel for the next timer event.
1219 */
1220void wake_up_idle_cpu(int cpu)
1221{
1222 struct rq *rq = cpu_rq(cpu);
1223
1224 if (cpu == smp_processor_id())
1225 return;
1226
1227 /*
1228 * This is safe, as this function is called with the timer
1229 * wheel base lock of (cpu) held. When the CPU is on the way
1230 * to idle and has not yet set rq->curr to idle then it will
1231 * be serialized on the timer wheel base lock and take the new
1232 * timer into account automatically.
1233 */
1234 if (rq->curr != rq->idle)
1235 return;
1236
1237 /*
1238 * We can set TIF_RESCHED on the idle task of the other CPU
1239 * lockless. The worst case is that the other CPU runs the
1240 * idle task through an additional NOOP schedule()
1241 */
1242 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1243
1244 /* NEED_RESCHED must be visible before we test polling */
1245 smp_mb();
1246 if (!tsk_is_polling(rq->idle))
1247 smp_send_reschedule(cpu);
1248}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001249#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001250
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001251#else /* !CONFIG_SMP */
Peter Zijlstra31656512008-07-18 18:01:23 +02001252static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001253{
1254 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001255 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001256}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001257#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001258
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001259#if BITS_PER_LONG == 32
1260# define WMULT_CONST (~0UL)
1261#else
1262# define WMULT_CONST (1UL << 32)
1263#endif
1264
1265#define WMULT_SHIFT 32
1266
Ingo Molnar194081e2007-08-09 11:16:51 +02001267/*
1268 * Shift right and round:
1269 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001270#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001271
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001272/*
1273 * delta *= weight / lw
1274 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001275static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001276calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1277 struct load_weight *lw)
1278{
1279 u64 tmp;
1280
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001281 if (!lw->inv_weight) {
1282 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1283 lw->inv_weight = 1;
1284 else
1285 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1286 / (lw->weight+1);
1287 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001288
1289 tmp = (u64)delta_exec * weight;
1290 /*
1291 * Check whether we'd overflow the 64-bit multiplication:
1292 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001293 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001294 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001295 WMULT_SHIFT/2);
1296 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001297 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001298
Ingo Molnarecf691d2007-08-02 17:41:40 +02001299 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001300}
1301
Ingo Molnar10919852007-10-15 17:00:04 +02001302static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001303{
1304 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001305 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001306}
1307
Ingo Molnar10919852007-10-15 17:00:04 +02001308static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001309{
1310 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001311 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001312}
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001315 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1316 * of tasks with abnormal "nice" values across CPUs the contribution that
1317 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001318 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001319 * scaled version of the new time slice allocation that they receive on time
1320 * slice expiry etc.
1321 */
1322
Ingo Molnardd41f592007-07-09 18:51:59 +02001323#define WEIGHT_IDLEPRIO 2
1324#define WMULT_IDLEPRIO (1 << 31)
1325
1326/*
1327 * Nice levels are multiplicative, with a gentle 10% change for every
1328 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1329 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1330 * that remained on nice 0.
1331 *
1332 * The "10% effect" is relative and cumulative: from _any_ nice level,
1333 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001334 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1335 * If a task goes up by ~10% and another task goes down by ~10% then
1336 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001337 */
1338static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001339 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1340 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1341 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1342 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1343 /* 0 */ 1024, 820, 655, 526, 423,
1344 /* 5 */ 335, 272, 215, 172, 137,
1345 /* 10 */ 110, 87, 70, 56, 45,
1346 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001347};
1348
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001349/*
1350 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1351 *
1352 * In cases where the weight does not change often, we can use the
1353 * precalculated inverse to speed up arithmetics by turning divisions
1354 * into multiplications:
1355 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001356static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001357 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1358 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1359 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1360 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1361 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1362 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1363 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1364 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001365};
Peter Williams2dd73a42006-06-27 02:54:34 -07001366
Ingo Molnardd41f592007-07-09 18:51:59 +02001367static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1368
1369/*
1370 * runqueue iterator, to support SMP load-balancing between different
1371 * scheduling classes, without having to expose their internal data
1372 * structures to the load-balancing proper:
1373 */
1374struct rq_iterator {
1375 void *arg;
1376 struct task_struct *(*start)(void *);
1377 struct task_struct *(*next)(void *);
1378};
1379
Peter Williamse1d14842007-10-24 18:23:51 +02001380#ifdef CONFIG_SMP
1381static unsigned long
1382balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1383 unsigned long max_load_move, struct sched_domain *sd,
1384 enum cpu_idle_type idle, int *all_pinned,
1385 int *this_best_prio, struct rq_iterator *iterator);
1386
1387static int
1388iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1389 struct sched_domain *sd, enum cpu_idle_type idle,
1390 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001391#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001392
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001393#ifdef CONFIG_CGROUP_CPUACCT
1394static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1395#else
1396static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1397#endif
1398
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001399static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1400{
1401 update_load_add(&rq->load, load);
1402}
1403
1404static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1405{
1406 update_load_sub(&rq->load, load);
1407}
1408
Ingo Molnar7940ca32008-08-19 13:40:47 +02001409#if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
Peter Zijlstraeb755802008-08-19 12:33:05 +02001410typedef int (*tg_visitor)(struct task_group *, void *);
1411
1412/*
1413 * Iterate the full tree, calling @down when first entering a node and @up when
1414 * leaving it for the final time.
1415 */
1416static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
1417{
1418 struct task_group *parent, *child;
1419 int ret;
1420
1421 rcu_read_lock();
1422 parent = &root_task_group;
1423down:
1424 ret = (*down)(parent, data);
1425 if (ret)
1426 goto out_unlock;
1427 list_for_each_entry_rcu(child, &parent->children, siblings) {
1428 parent = child;
1429 goto down;
1430
1431up:
1432 continue;
1433 }
1434 ret = (*up)(parent, data);
1435 if (ret)
1436 goto out_unlock;
1437
1438 child = parent;
1439 parent = parent->parent;
1440 if (parent)
1441 goto up;
1442out_unlock:
1443 rcu_read_unlock();
1444
1445 return ret;
1446}
1447
1448static int tg_nop(struct task_group *tg, void *data)
1449{
1450 return 0;
1451}
1452#endif
1453
Gregory Haskinse7693a32008-01-25 21:08:09 +01001454#ifdef CONFIG_SMP
1455static unsigned long source_load(int cpu, int type);
1456static unsigned long target_load(int cpu, int type);
Gregory Haskinse7693a32008-01-25 21:08:09 +01001457static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001458
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001459static unsigned long cpu_avg_load_per_task(int cpu)
1460{
1461 struct rq *rq = cpu_rq(cpu);
1462
1463 if (rq->nr_running)
1464 rq->avg_load_per_task = rq->load.weight / rq->nr_running;
1465
1466 return rq->avg_load_per_task;
1467}
1468
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001469#ifdef CONFIG_FAIR_GROUP_SCHED
1470
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001471static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1472
1473/*
1474 * Calculate and set the cpu's group shares.
1475 */
1476static void
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001477update_group_shares_cpu(struct task_group *tg, int cpu,
1478 unsigned long sd_shares, unsigned long sd_rq_weight)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001479{
1480 int boost = 0;
1481 unsigned long shares;
1482 unsigned long rq_weight;
1483
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001484 if (!tg->se[cpu])
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001485 return;
1486
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001487 rq_weight = tg->cfs_rq[cpu]->load.weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001488
1489 /*
1490 * If there are currently no tasks on the cpu pretend there is one of
1491 * average load so that when a new task gets to run here it will not
1492 * get delayed by group starvation.
1493 */
1494 if (!rq_weight) {
1495 boost = 1;
1496 rq_weight = NICE_0_LOAD;
1497 }
1498
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001499 if (unlikely(rq_weight > sd_rq_weight))
1500 rq_weight = sd_rq_weight;
1501
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001502 /*
1503 * \Sum shares * rq_weight
1504 * shares = -----------------------
1505 * \Sum rq_weight
1506 *
1507 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001508 shares = (sd_shares * rq_weight) / (sd_rq_weight + 1);
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001509 shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001510
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001511 if (abs(shares - tg->se[cpu]->load.weight) >
1512 sysctl_sched_shares_thresh) {
1513 struct rq *rq = cpu_rq(cpu);
1514 unsigned long flags;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001515
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001516 spin_lock_irqsave(&rq->lock, flags);
1517 /*
1518 * record the actual number of shares, not the boosted amount.
1519 */
1520 tg->cfs_rq[cpu]->shares = boost ? 0 : shares;
1521 tg->cfs_rq[cpu]->rq_weight = rq_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001522
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001523 __set_se_shares(tg->se[cpu], shares);
1524 spin_unlock_irqrestore(&rq->lock, flags);
1525 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001526}
1527
1528/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001529 * Re-compute the task group their per cpu shares over the given domain.
1530 * This needs to be done in a bottom-up fashion because the rq weight of a
1531 * parent group depends on the shares of its child groups.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001532 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001533static int tg_shares_up(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001534{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001535 unsigned long rq_weight = 0;
1536 unsigned long shares = 0;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001537 struct sched_domain *sd = data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001538 int i;
1539
1540 for_each_cpu_mask(i, sd->span) {
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001541 rq_weight += tg->cfs_rq[i]->load.weight;
1542 shares += tg->cfs_rq[i]->shares;
1543 }
1544
1545 if ((!shares && rq_weight) || shares > tg->shares)
1546 shares = tg->shares;
1547
1548 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1549 shares = tg->shares;
1550
Peter Zijlstracd809172008-06-27 13:41:34 +02001551 if (!rq_weight)
1552 rq_weight = cpus_weight(sd->span) * NICE_0_LOAD;
1553
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001554 for_each_cpu_mask(i, sd->span)
1555 update_group_shares_cpu(tg, i, shares, rq_weight);
Peter Zijlstraeb755802008-08-19 12:33:05 +02001556
1557 return 0;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001558}
1559
1560/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001561 * Compute the cpu's hierarchical load factor for each task group.
1562 * This needs to be done in a top-down fashion because the load of a child
1563 * group is a fraction of its parents load.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001564 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001565static int tg_load_down(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001566{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001567 unsigned long load;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001568 long cpu = (long)data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001569
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001570 if (!tg->parent) {
1571 load = cpu_rq(cpu)->load.weight;
1572 } else {
1573 load = tg->parent->cfs_rq[cpu]->h_load;
1574 load *= tg->cfs_rq[cpu]->shares;
1575 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1576 }
1577
1578 tg->cfs_rq[cpu]->h_load = load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001579
Peter Zijlstraeb755802008-08-19 12:33:05 +02001580 return 0;
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001581}
1582
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001583static void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001584{
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001585 u64 now = cpu_clock(raw_smp_processor_id());
1586 s64 elapsed = now - sd->last_update;
1587
1588 if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
1589 sd->last_update = now;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001590 walk_tg_tree(tg_nop, tg_shares_up, sd);
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001591 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001592}
1593
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001594static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1595{
1596 spin_unlock(&rq->lock);
1597 update_shares(sd);
1598 spin_lock(&rq->lock);
1599}
1600
Peter Zijlstraeb755802008-08-19 12:33:05 +02001601static void update_h_load(long cpu)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001602{
Peter Zijlstraeb755802008-08-19 12:33:05 +02001603 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001604}
1605
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001606#else
1607
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001608static inline void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001609{
1610}
1611
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001612static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1613{
1614}
1615
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001616#endif
1617
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001618#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001619
1620#ifdef CONFIG_FAIR_GROUP_SCHED
1621static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1622{
Vegard Nossum30432092008-06-27 21:35:50 +02001623#ifdef CONFIG_SMP
Ingo Molnar34e83e82008-06-27 15:42:36 +02001624 cfs_rq->shares = shares;
1625#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001626}
1627#endif
1628
Ingo Molnardd41f592007-07-09 18:51:59 +02001629#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001630#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001631#include "sched_fair.c"
1632#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001633#ifdef CONFIG_SCHED_DEBUG
1634# include "sched_debug.c"
1635#endif
1636
1637#define sched_class_highest (&rt_sched_class)
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001638#define for_each_class(class) \
1639 for (class = sched_class_highest; class; class = class->next)
Ingo Molnardd41f592007-07-09 18:51:59 +02001640
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001641static void inc_nr_running(struct rq *rq)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001642{
1643 rq->nr_running++;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001644}
1645
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001646static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001647{
1648 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001649}
1650
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001651static void set_load_weight(struct task_struct *p)
1652{
1653 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001654 p->se.load.weight = prio_to_weight[0] * 2;
1655 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1656 return;
1657 }
1658
1659 /*
1660 * SCHED_IDLE tasks get minimal weight:
1661 */
1662 if (p->policy == SCHED_IDLE) {
1663 p->se.load.weight = WEIGHT_IDLEPRIO;
1664 p->se.load.inv_weight = WMULT_IDLEPRIO;
1665 return;
1666 }
1667
1668 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1669 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001670}
1671
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001672static void update_avg(u64 *avg, u64 sample)
1673{
1674 s64 diff = sample - *avg;
1675 *avg += diff >> 3;
1676}
1677
Ingo Molnar8159f872007-08-09 11:16:49 +02001678static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001679{
1680 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001681 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001682 p->se.on_rq = 1;
1683}
1684
Ingo Molnar69be72c2007-08-09 11:16:49 +02001685static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001686{
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001687 if (sleep && p->se.last_wakeup) {
1688 update_avg(&p->se.avg_overlap,
1689 p->se.sum_exec_runtime - p->se.last_wakeup);
1690 p->se.last_wakeup = 0;
1691 }
1692
Ankita Garg46ac22b2008-07-01 14:30:06 +05301693 sched_info_dequeued(p);
Ingo Molnarf02231e2007-08-09 11:16:48 +02001694 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001695 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001696}
1697
1698/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001699 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001700 */
Ingo Molnar14531182007-07-09 18:51:59 +02001701static inline int __normal_prio(struct task_struct *p)
1702{
Ingo Molnardd41f592007-07-09 18:51:59 +02001703 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001704}
1705
1706/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001707 * Calculate the expected normal priority: i.e. priority
1708 * without taking RT-inheritance into account. Might be
1709 * boosted by interactivity modifiers. Changes upon fork,
1710 * setprio syscalls, and whenever the interactivity
1711 * estimator recalculates.
1712 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001713static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001714{
1715 int prio;
1716
Ingo Molnare05606d2007-07-09 18:51:59 +02001717 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001718 prio = MAX_RT_PRIO-1 - p->rt_priority;
1719 else
1720 prio = __normal_prio(p);
1721 return prio;
1722}
1723
1724/*
1725 * Calculate the current priority, i.e. the priority
1726 * taken into account by the scheduler. This value might
1727 * be boosted by RT tasks, or might be boosted by
1728 * interactivity modifiers. Will be RT if the task got
1729 * RT-boosted. If not then it returns p->normal_prio.
1730 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001731static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001732{
1733 p->normal_prio = normal_prio(p);
1734 /*
1735 * If we are RT tasks or we were boosted to RT priority,
1736 * keep the priority unchanged. Otherwise, update priority
1737 * to the normal priority:
1738 */
1739 if (!rt_prio(p->prio))
1740 return p->normal_prio;
1741 return p->prio;
1742}
1743
1744/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001745 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001747static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001749 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001750 rq->nr_uninterruptible--;
1751
Ingo Molnar8159f872007-08-09 11:16:49 +02001752 enqueue_task(rq, p, wakeup);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001753 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 * deactivate_task - remove a task from the runqueue.
1758 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001759static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001761 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001762 rq->nr_uninterruptible++;
1763
Ingo Molnar69be72c2007-08-09 11:16:49 +02001764 dequeue_task(rq, p, sleep);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001765 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768/**
1769 * task_curr - is this task currently executing on a CPU?
1770 * @p: the task in question.
1771 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001772inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 return cpu_curr(task_cpu(p)) == p;
1775}
1776
Ingo Molnardd41f592007-07-09 18:51:59 +02001777static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1778{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001779 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001780#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001781 /*
1782 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1783 * successfuly executed on another CPU. We must ensure that updates of
1784 * per-task data have been completed by this moment.
1785 */
1786 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001787 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001788#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001789}
1790
Steven Rostedtcb469842008-01-25 21:08:22 +01001791static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1792 const struct sched_class *prev_class,
1793 int oldprio, int running)
1794{
1795 if (prev_class != p->sched_class) {
1796 if (prev_class->switched_from)
1797 prev_class->switched_from(rq, p, running);
1798 p->sched_class->switched_to(rq, p, running);
1799 } else
1800 p->sched_class->prio_changed(rq, p, oldprio, running);
1801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001804
Thomas Gleixnere958b362008-06-04 23:22:32 +02001805/* Used instead of source_load when we know the type == 0 */
1806static unsigned long weighted_cpuload(const int cpu)
1807{
1808 return cpu_rq(cpu)->load.weight;
1809}
1810
Ingo Molnarcc367732007-10-15 17:00:18 +02001811/*
1812 * Is this task likely cache-hot:
1813 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001814static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001815task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1816{
1817 s64 delta;
1818
Ingo Molnarf540a602008-03-15 17:10:34 +01001819 /*
1820 * Buddy candidates are cache hot:
1821 */
Peter Zijlstra47932412008-11-04 21:25:09 +01001822 if (sched_feat(CACHE_HOT_BUDDY) &&
1823 (&p->se == cfs_rq_of(&p->se)->next ||
1824 &p->se == cfs_rq_of(&p->se)->last))
Ingo Molnarf540a602008-03-15 17:10:34 +01001825 return 1;
1826
Ingo Molnarcc367732007-10-15 17:00:18 +02001827 if (p->sched_class != &fair_sched_class)
1828 return 0;
1829
Ingo Molnar6bc16652007-10-15 17:00:18 +02001830 if (sysctl_sched_migration_cost == -1)
1831 return 1;
1832 if (sysctl_sched_migration_cost == 0)
1833 return 0;
1834
Ingo Molnarcc367732007-10-15 17:00:18 +02001835 delta = now - p->se.exec_start;
1836
1837 return delta < (s64)sysctl_sched_migration_cost;
1838}
1839
1840
Ingo Molnardd41f592007-07-09 18:51:59 +02001841void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001842{
Ingo Molnardd41f592007-07-09 18:51:59 +02001843 int old_cpu = task_cpu(p);
1844 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001845 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1846 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001847 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001848
1849 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001850
1851#ifdef CONFIG_SCHEDSTATS
1852 if (p->se.wait_start)
1853 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001854 if (p->se.sleep_start)
1855 p->se.sleep_start -= clock_offset;
1856 if (p->se.block_start)
1857 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001858 if (old_cpu != new_cpu) {
1859 schedstat_inc(p, se.nr_migrations);
1860 if (task_hot(p, old_rq->clock, NULL))
1861 schedstat_inc(p, se.nr_forced2_migrations);
1862 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001863#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001864 p->se.vruntime -= old_cfsrq->min_vruntime -
1865 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001866
1867 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001868}
1869
Ingo Molnar70b97a72006-07-03 00:25:42 -07001870struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Ingo Molnar36c8b582006-07-03 00:25:41 -07001873 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 int dest_cpu;
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001877};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879/*
1880 * The task's runqueue lock must be held.
1881 * Returns true if you have to wait for migration thread.
1882 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001883static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001884migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001886 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 /*
1889 * If the task is not on a runqueue (and not running), then
1890 * it is sufficient to simply update the task's cpu field.
1891 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001892 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 set_task_cpu(p, dest_cpu);
1894 return 0;
1895 }
1896
1897 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 req->task = p;
1899 req->dest_cpu = dest_cpu;
1900 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return 1;
1903}
1904
1905/*
1906 * wait_task_inactive - wait for a thread to unschedule.
1907 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07001908 * If @match_state is nonzero, it's the @p->state value just checked and
1909 * not expected to change. If it changes, i.e. @p might have woken up,
1910 * then return zero. When we succeed in waiting for @p to be off its CPU,
1911 * we return a positive number (its total switch count). If a second call
1912 * a short while later returns the same number, the caller can be sure that
1913 * @p has remained unscheduled the whole time.
1914 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 * The caller must ensure that the task *will* unschedule sometime soon,
1916 * else this function might spin for a *long* time. This function can't
1917 * be called with interrupts off, or it may introduce deadlock with
1918 * smp_call_function() if an IPI is sent by the same process we are
1919 * waiting to become inactive.
1920 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001921unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
1923 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001924 int running, on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001925 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001926 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Andi Kleen3a5c3592007-10-15 17:00:14 +02001928 for (;;) {
1929 /*
1930 * We do the initial early heuristics without holding
1931 * any task-queue locks at all. We'll only try to get
1932 * the runqueue lock when things look like they will
1933 * work out!
1934 */
1935 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001936
Andi Kleen3a5c3592007-10-15 17:00:14 +02001937 /*
1938 * If the task is actively running on another CPU
1939 * still, just relax and busy-wait without holding
1940 * any locks.
1941 *
1942 * NOTE! Since we don't hold any locks, it's not
1943 * even sure that "rq" stays as the right runqueue!
1944 * But we don't care, since "task_running()" will
1945 * return false if the runqueue has changed and p
1946 * is actually now running somewhere else!
1947 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001948 while (task_running(rq, p)) {
1949 if (match_state && unlikely(p->state != match_state))
1950 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02001951 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07001952 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001953
Andi Kleen3a5c3592007-10-15 17:00:14 +02001954 /*
1955 * Ok, time to look more closely! We need the rq
1956 * lock now, to be *sure*. If we're wrong, we'll
1957 * just go back and repeat.
1958 */
1959 rq = task_rq_lock(p, &flags);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04001960 trace_sched_wait_task(rq, p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001961 running = task_running(rq, p);
1962 on_rq = p->se.on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001963 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07001964 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07001965 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Andi Kleen3a5c3592007-10-15 17:00:14 +02001966 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001967
Andi Kleen3a5c3592007-10-15 17:00:14 +02001968 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07001969 * If it changed from the expected state, bail out now.
1970 */
1971 if (unlikely(!ncsw))
1972 break;
1973
1974 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02001975 * Was it really running after all now that we
1976 * checked with the proper locks actually held?
1977 *
1978 * Oops. Go back and try again..
1979 */
1980 if (unlikely(running)) {
1981 cpu_relax();
1982 continue;
1983 }
1984
1985 /*
1986 * It's not enough that it's not actively running,
1987 * it must be off the runqueue _entirely_, and not
1988 * preempted!
1989 *
1990 * So if it wa still runnable (but just not actively
1991 * running right now), it's preempted, and we should
1992 * yield - it could be a while.
1993 */
1994 if (unlikely(on_rq)) {
1995 schedule_timeout_uninterruptible(1);
1996 continue;
1997 }
1998
1999 /*
2000 * Ahh, all good. It wasn't running, and it wasn't
2001 * runnable, which means that it will never become
2002 * running in the future either. We're all done!
2003 */
2004 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002006
2007 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
2010/***
2011 * kick_process - kick a running thread to enter/exit the kernel
2012 * @p: the to-be-kicked thread
2013 *
2014 * Cause a process which is running on another CPU to enter
2015 * kernel-mode, without any delay. (to get signals handled.)
2016 *
2017 * NOTE: this function doesnt have to take the runqueue lock,
2018 * because all it wants to ensure is that the remote task enters
2019 * the kernel. If the IPI races and the task has been migrated
2020 * to another CPU then no harm is done and the purpose has been
2021 * achieved as well.
2022 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002023void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 int cpu;
2026
2027 preempt_disable();
2028 cpu = task_cpu(p);
2029 if ((cpu != smp_processor_id()) && task_curr(p))
2030 smp_send_reschedule(cpu);
2031 preempt_enable();
2032}
2033
2034/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002035 * Return a low guess at the load of a migration-source cpu weighted
2036 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 *
2038 * We want to under-estimate the load of migration sources, to
2039 * balance conservatively.
2040 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002041static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002042{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002043 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002044 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002045
Peter Zijlstra93b75212008-06-27 13:41:33 +02002046 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002047 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002048
Ingo Molnardd41f592007-07-09 18:51:59 +02002049 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
2052/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002053 * Return a high guess at the load of a migration-target cpu weighted
2054 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002056static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002057{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002058 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002059 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002060
Peter Zijlstra93b75212008-06-27 13:41:33 +02002061 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002062 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002063
Ingo Molnardd41f592007-07-09 18:51:59 +02002064 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002065}
2066
2067/*
Nick Piggin147cbb42005-06-25 14:57:19 -07002068 * find_idlest_group finds and returns the least busy CPU group within the
2069 * domain.
2070 */
2071static struct sched_group *
2072find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2073{
2074 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2075 unsigned long min_load = ULONG_MAX, this_load = 0;
2076 int load_idx = sd->forkexec_idx;
2077 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2078
2079 do {
2080 unsigned long load, avg_load;
2081 int local_group;
2082 int i;
2083
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002084 /* Skip over this group if it has no CPUs allowed */
2085 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002086 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002087
Nick Piggin147cbb42005-06-25 14:57:19 -07002088 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002089
2090 /* Tally up the load of all CPUs in the group */
2091 avg_load = 0;
2092
Mike Travis363ab6f2008-05-12 21:21:13 +02002093 for_each_cpu_mask_nr(i, group->cpumask) {
Nick Piggin147cbb42005-06-25 14:57:19 -07002094 /* Bias balancing toward cpus of our domain */
2095 if (local_group)
2096 load = source_load(i, load_idx);
2097 else
2098 load = target_load(i, load_idx);
2099
2100 avg_load += load;
2101 }
2102
2103 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002104 avg_load = sg_div_cpu_power(group,
2105 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002106
2107 if (local_group) {
2108 this_load = avg_load;
2109 this = group;
2110 } else if (avg_load < min_load) {
2111 min_load = avg_load;
2112 idlest = group;
2113 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002114 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002115
2116 if (!idlest || 100*this_load < imbalance*min_load)
2117 return NULL;
2118 return idlest;
2119}
2120
2121/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002122 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002123 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002124static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002125find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2126 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002127{
2128 unsigned long load, min_load = ULONG_MAX;
2129 int idlest = -1;
2130 int i;
2131
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002132 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002133 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002134
Mike Travis363ab6f2008-05-12 21:21:13 +02002135 for_each_cpu_mask_nr(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002136 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002137
2138 if (load < min_load || (load == min_load && i == this_cpu)) {
2139 min_load = load;
2140 idlest = i;
2141 }
2142 }
2143
2144 return idlest;
2145}
2146
Nick Piggin476d1392005-06-25 14:57:29 -07002147/*
2148 * sched_balance_self: balance the current task (running on cpu) in domains
2149 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2150 * SD_BALANCE_EXEC.
2151 *
2152 * Balance, ie. select the least loaded group.
2153 *
2154 * Returns the target CPU number, or the same CPU if no balancing is needed.
2155 *
2156 * preempt must be disabled.
2157 */
2158static int sched_balance_self(int cpu, int flag)
2159{
2160 struct task_struct *t = current;
2161 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002162
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002163 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002164 /*
2165 * If power savings logic is enabled for a domain, stop there.
2166 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002167 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2168 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002169 if (tmp->flags & flag)
2170 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002171 }
Nick Piggin476d1392005-06-25 14:57:29 -07002172
Peter Zijlstra039a1c42008-06-27 13:41:25 +02002173 if (sd)
2174 update_shares(sd);
2175
Nick Piggin476d1392005-06-25 14:57:29 -07002176 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002177 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002178 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002179 int new_cpu, weight;
2180
2181 if (!(sd->flags & flag)) {
2182 sd = sd->child;
2183 continue;
2184 }
Nick Piggin476d1392005-06-25 14:57:29 -07002185
2186 span = sd->span;
2187 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002188 if (!group) {
2189 sd = sd->child;
2190 continue;
2191 }
Nick Piggin476d1392005-06-25 14:57:29 -07002192
Mike Travis7c16ec52008-04-04 18:11:11 -07002193 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002194 if (new_cpu == -1 || new_cpu == cpu) {
2195 /* Now try balancing at a lower domain level of cpu */
2196 sd = sd->child;
2197 continue;
2198 }
Nick Piggin476d1392005-06-25 14:57:29 -07002199
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002200 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002201 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002202 sd = NULL;
2203 weight = cpus_weight(span);
2204 for_each_domain(cpu, tmp) {
2205 if (weight <= cpus_weight(tmp->span))
2206 break;
2207 if (tmp->flags & flag)
2208 sd = tmp;
2209 }
2210 /* while loop will break here if sd == NULL */
2211 }
2212
2213 return cpu;
2214}
2215
2216#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218/***
2219 * try_to_wake_up - wake up a thread
2220 * @p: the to-be-woken-up thread
2221 * @state: the mask of task states that can be woken
2222 * @sync: do a synchronous wakeup?
2223 *
2224 * Put it on the run-queue if it's not already there. The "current"
2225 * thread is always on the run-queue (except when the actual
2226 * re-schedule is in progress), and as such you're allowed to do
2227 * the simpler "current->state = TASK_RUNNING" to mark yourself
2228 * runnable without the overhead of this.
2229 *
2230 * returns failure only if the task is already active.
2231 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002232static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Ingo Molnarcc367732007-10-15 17:00:18 +02002234 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 unsigned long flags;
2236 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002237 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Ingo Molnarb85d0662008-03-16 20:03:22 +01002239 if (!sched_feat(SYNC_WAKEUPS))
2240 sync = 0;
2241
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002242#ifdef CONFIG_SMP
2243 if (sched_feat(LB_WAKEUP_UPDATE)) {
2244 struct sched_domain *sd;
2245
2246 this_cpu = raw_smp_processor_id();
2247 cpu = task_cpu(p);
2248
2249 for_each_domain(this_cpu, sd) {
2250 if (cpu_isset(cpu, sd->span)) {
2251 update_shares(sd);
2252 break;
2253 }
2254 }
2255 }
2256#endif
2257
Linus Torvalds04e2f172008-02-23 18:05:03 -08002258 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 rq = task_rq_lock(p, &flags);
2260 old_state = p->state;
2261 if (!(old_state & state))
2262 goto out;
2263
Ingo Molnardd41f592007-07-09 18:51:59 +02002264 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 goto out_running;
2266
2267 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002268 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 this_cpu = smp_processor_id();
2270
2271#ifdef CONFIG_SMP
2272 if (unlikely(task_running(rq, p)))
2273 goto out_activate;
2274
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002275 cpu = p->sched_class->select_task_rq(p, sync);
2276 if (cpu != orig_cpu) {
2277 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 task_rq_unlock(rq, &flags);
2279 /* might preempt at this point */
2280 rq = task_rq_lock(p, &flags);
2281 old_state = p->state;
2282 if (!(old_state & state))
2283 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002284 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 goto out_running;
2286
2287 this_cpu = smp_processor_id();
2288 cpu = task_cpu(p);
2289 }
2290
Gregory Haskinse7693a32008-01-25 21:08:09 +01002291#ifdef CONFIG_SCHEDSTATS
2292 schedstat_inc(rq, ttwu_count);
2293 if (cpu == this_cpu)
2294 schedstat_inc(rq, ttwu_local);
2295 else {
2296 struct sched_domain *sd;
2297 for_each_domain(this_cpu, sd) {
2298 if (cpu_isset(cpu, sd->span)) {
2299 schedstat_inc(sd, ttwu_wake_remote);
2300 break;
2301 }
2302 }
2303 }
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002304#endif /* CONFIG_SCHEDSTATS */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306out_activate:
2307#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002308 schedstat_inc(p, se.nr_wakeups);
2309 if (sync)
2310 schedstat_inc(p, se.nr_wakeups_sync);
2311 if (orig_cpu != cpu)
2312 schedstat_inc(p, se.nr_wakeups_migrate);
2313 if (cpu == this_cpu)
2314 schedstat_inc(p, se.nr_wakeups_local);
2315 else
2316 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002317 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002318 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 success = 1;
2320
2321out_running:
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002322 trace_sched_wakeup(rq, p);
Peter Zijlstra15afe092008-09-20 23:38:02 +02002323 check_preempt_curr(rq, p, sync);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002326#ifdef CONFIG_SMP
2327 if (p->sched_class->task_wake_up)
2328 p->sched_class->task_wake_up(rq, p);
2329#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330out:
Gregory Haskins2087a1a2008-06-27 14:30:00 -06002331 current->se.last_wakeup = current->se.sum_exec_runtime;
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 task_rq_unlock(rq, &flags);
2334
2335 return success;
2336}
2337
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002338int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002340 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342EXPORT_SYMBOL(wake_up_process);
2343
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002344int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 return try_to_wake_up(p, state, 0);
2347}
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349/*
2350 * Perform scheduler related setup for a newly forked process p.
2351 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002352 *
2353 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002355static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
Ingo Molnardd41f592007-07-09 18:51:59 +02002357 p->se.exec_start = 0;
2358 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002359 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002360 p->se.last_wakeup = 0;
2361 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002362
2363#ifdef CONFIG_SCHEDSTATS
2364 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002365 p->se.sum_sleep_runtime = 0;
2366 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002367 p->se.block_start = 0;
2368 p->se.sleep_max = 0;
2369 p->se.block_max = 0;
2370 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002371 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002372 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002373#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002374
Peter Zijlstrafa717062008-01-25 21:08:27 +01002375 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002376 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002377 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002378
Avi Kivitye107be32007-07-26 13:40:43 +02002379#ifdef CONFIG_PREEMPT_NOTIFIERS
2380 INIT_HLIST_HEAD(&p->preempt_notifiers);
2381#endif
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /*
2384 * We mark the process as running here, but have not actually
2385 * inserted it onto the runqueue yet. This guarantees that
2386 * nobody will actually run it, and a signal or other external
2387 * event cannot wake it up and insert it on the runqueue either.
2388 */
2389 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002390}
2391
2392/*
2393 * fork()/clone()-time setup:
2394 */
2395void sched_fork(struct task_struct *p, int clone_flags)
2396{
2397 int cpu = get_cpu();
2398
2399 __sched_fork(p);
2400
2401#ifdef CONFIG_SMP
2402 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2403#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02002404 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002405
2406 /*
2407 * Make sure we do not leak PI boosting priority to the child:
2408 */
2409 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002410 if (!rt_prio(p->prio))
2411 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002412
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002413#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002414 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002415 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002417#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002418 p->oncpu = 0;
2419#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002421 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002422 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002424 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
2427/*
2428 * wake_up_new_task - wake up a newly created task for the first time.
2429 *
2430 * This function will do some initial scheduler statistics housekeeping
2431 * that must be done for every newly created context, then puts the task
2432 * on the runqueue and wakes it.
2433 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002434void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
2436 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002437 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002441 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 p->prio = effective_prio(p);
2444
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002445 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002446 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002449 * Let the scheduling class do new task startup
2450 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002452 p->sched_class->task_new(rq, p);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002453 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002455 trace_sched_wakeup_new(rq, p);
Peter Zijlstra15afe092008-09-20 23:38:02 +02002456 check_preempt_curr(rq, p, 0);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002457#ifdef CONFIG_SMP
2458 if (p->sched_class->task_wake_up)
2459 p->sched_class->task_wake_up(rq, p);
2460#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002461 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
Avi Kivitye107be32007-07-26 13:40:43 +02002464#ifdef CONFIG_PREEMPT_NOTIFIERS
2465
2466/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002467 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2468 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002469 */
2470void preempt_notifier_register(struct preempt_notifier *notifier)
2471{
2472 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2473}
2474EXPORT_SYMBOL_GPL(preempt_notifier_register);
2475
2476/**
2477 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002478 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002479 *
2480 * This is safe to call from within a preemption notifier.
2481 */
2482void preempt_notifier_unregister(struct preempt_notifier *notifier)
2483{
2484 hlist_del(&notifier->link);
2485}
2486EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2487
2488static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2489{
2490 struct preempt_notifier *notifier;
2491 struct hlist_node *node;
2492
2493 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2494 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2495}
2496
2497static void
2498fire_sched_out_preempt_notifiers(struct task_struct *curr,
2499 struct task_struct *next)
2500{
2501 struct preempt_notifier *notifier;
2502 struct hlist_node *node;
2503
2504 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2505 notifier->ops->sched_out(notifier, next);
2506}
2507
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002508#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002509
2510static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2511{
2512}
2513
2514static void
2515fire_sched_out_preempt_notifiers(struct task_struct *curr,
2516 struct task_struct *next)
2517{
2518}
2519
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002520#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002523 * prepare_task_switch - prepare to switch tasks
2524 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002525 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002526 * @next: the task we are going to switch to.
2527 *
2528 * This is called with the rq lock held and interrupts off. It must
2529 * be paired with a subsequent finish_task_switch after the context
2530 * switch.
2531 *
2532 * prepare_task_switch sets up locking and calls architecture specific
2533 * hooks.
2534 */
Avi Kivitye107be32007-07-26 13:40:43 +02002535static inline void
2536prepare_task_switch(struct rq *rq, struct task_struct *prev,
2537 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002538{
Avi Kivitye107be32007-07-26 13:40:43 +02002539 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002540 prepare_lock_switch(rq, next);
2541 prepare_arch_switch(next);
2542}
2543
2544/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002546 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 * @prev: the thread we just switched away from.
2548 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002549 * finish_task_switch must be called after the context switch, paired
2550 * with a prepare_task_switch call before the context switch.
2551 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2552 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 *
2554 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002555 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 * with the lock held can cause deadlocks; see schedule() for
2557 * details.)
2558 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002559static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 __releases(rq->lock)
2561{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002563 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 rq->prev_mm = NULL;
2566
2567 /*
2568 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002569 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002570 * schedule one last time. The schedule call will never return, and
2571 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002572 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 * still held, otherwise prev could be scheduled on another cpu, die
2574 * there before we look at prev->state, and then the reference would
2575 * be dropped twice.
2576 * Manfred Spraul <manfred@colorfullife.com>
2577 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002578 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002579 finish_arch_switch(prev);
2580 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002581#ifdef CONFIG_SMP
2582 if (current->sched_class->post_schedule)
2583 current->sched_class->post_schedule(rq);
2584#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002585
Avi Kivitye107be32007-07-26 13:40:43 +02002586 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (mm)
2588 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002589 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002590 /*
2591 * Remove function-return probe instances associated with this
2592 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002593 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002594 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
2599/**
2600 * schedule_tail - first thing a freshly forked thread must call.
2601 * @prev: the thread we just switched away from.
2602 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002603asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 __releases(rq->lock)
2605{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002606 struct rq *rq = this_rq();
2607
Nick Piggin4866cde2005-06-25 14:57:23 -07002608 finish_task_switch(rq, prev);
2609#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2610 /* In this case, finish_task_switch does not reenable preemption */
2611 preempt_enable();
2612#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002614 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
2617/*
2618 * context_switch - switch to the new MM and the new
2619 * thread's register state.
2620 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002621static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002622context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002623 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
Ingo Molnardd41f592007-07-09 18:51:59 +02002625 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Avi Kivitye107be32007-07-26 13:40:43 +02002627 prepare_task_switch(rq, prev, next);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002628 trace_sched_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002629 mm = next->mm;
2630 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002631 /*
2632 * For paravirt, this is coupled with an exit in switch_to to
2633 * combine the page table reload and the switch backend into
2634 * one hypercall.
2635 */
2636 arch_enter_lazy_cpu_mode();
2637
Ingo Molnardd41f592007-07-09 18:51:59 +02002638 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 next->active_mm = oldmm;
2640 atomic_inc(&oldmm->mm_count);
2641 enter_lazy_tlb(oldmm, next);
2642 } else
2643 switch_mm(oldmm, mm, next);
2644
Ingo Molnardd41f592007-07-09 18:51:59 +02002645 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 rq->prev_mm = oldmm;
2648 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002649 /*
2650 * Since the runqueue lock will be released by the next
2651 * task (which is an invalid locking op but in the case
2652 * of the scheduler it's an obvious special-case), so we
2653 * do an early lockdep release here:
2654 */
2655#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002656 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002657#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
2659 /* Here we just switch the register state and the stack. */
2660 switch_to(prev, next, prev);
2661
Ingo Molnardd41f592007-07-09 18:51:59 +02002662 barrier();
2663 /*
2664 * this_rq must be evaluated again because prev may have moved
2665 * CPUs since it called schedule(), thus the 'rq' on its stack
2666 * frame will be invalid.
2667 */
2668 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
2671/*
2672 * nr_running, nr_uninterruptible and nr_context_switches:
2673 *
2674 * externally visible scheduler statistics: current number of runnable
2675 * threads, current number of uninterruptible-sleeping threads, total
2676 * number of context switches performed since bootup.
2677 */
2678unsigned long nr_running(void)
2679{
2680 unsigned long i, sum = 0;
2681
2682 for_each_online_cpu(i)
2683 sum += cpu_rq(i)->nr_running;
2684
2685 return sum;
2686}
2687
2688unsigned long nr_uninterruptible(void)
2689{
2690 unsigned long i, sum = 0;
2691
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002692 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 sum += cpu_rq(i)->nr_uninterruptible;
2694
2695 /*
2696 * Since we read the counters lockless, it might be slightly
2697 * inaccurate. Do not allow it to go below zero though:
2698 */
2699 if (unlikely((long)sum < 0))
2700 sum = 0;
2701
2702 return sum;
2703}
2704
2705unsigned long long nr_context_switches(void)
2706{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002707 int i;
2708 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002710 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 sum += cpu_rq(i)->nr_switches;
2712
2713 return sum;
2714}
2715
2716unsigned long nr_iowait(void)
2717{
2718 unsigned long i, sum = 0;
2719
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002720 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2722
2723 return sum;
2724}
2725
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002726unsigned long nr_active(void)
2727{
2728 unsigned long i, running = 0, uninterruptible = 0;
2729
2730 for_each_online_cpu(i) {
2731 running += cpu_rq(i)->nr_running;
2732 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2733 }
2734
2735 if (unlikely((long)uninterruptible < 0))
2736 uninterruptible = 0;
2737
2738 return running + uninterruptible;
2739}
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002742 * Update rq->cpu_load[] statistics. This function is usually called every
2743 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002744 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002745static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002746{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002747 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002748 int i, scale;
2749
2750 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002751
2752 /* Update our load: */
2753 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2754 unsigned long old_load, new_load;
2755
2756 /* scale is effectively 1 << i now, and >> i divides by scale */
2757
2758 old_load = this_rq->cpu_load[i];
2759 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002760 /*
2761 * Round up the averaging division if load is increasing. This
2762 * prevents us from getting stuck on 9 if the load is 10, for
2763 * example.
2764 */
2765 if (new_load > old_load)
2766 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002767 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2768 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002769}
2770
Ingo Molnardd41f592007-07-09 18:51:59 +02002771#ifdef CONFIG_SMP
2772
Ingo Molnar48f24c42006-07-03 00:25:40 -07002773/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 * double_rq_lock - safely lock two runqueues
2775 *
2776 * Note this does not disable interrupts like task_rq_lock,
2777 * you need to do so manually before calling.
2778 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002779static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 __acquires(rq1->lock)
2781 __acquires(rq2->lock)
2782{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002783 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (rq1 == rq2) {
2785 spin_lock(&rq1->lock);
2786 __acquire(rq2->lock); /* Fake it out ;) */
2787 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002788 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 spin_lock(&rq1->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002790 spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 } else {
2792 spin_lock(&rq2->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002793 spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
2795 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002796 update_rq_clock(rq1);
2797 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
2799
2800/*
2801 * double_rq_unlock - safely unlock two runqueues
2802 *
2803 * Note this does not restore interrupts like task_rq_unlock,
2804 * you need to do so manually after calling.
2805 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002806static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 __releases(rq1->lock)
2808 __releases(rq2->lock)
2809{
2810 spin_unlock(&rq1->lock);
2811 if (rq1 != rq2)
2812 spin_unlock(&rq2->lock);
2813 else
2814 __release(rq2->lock);
2815}
2816
2817/*
2818 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2819 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002820static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 __releases(this_rq->lock)
2822 __acquires(busiest->lock)
2823 __acquires(this_rq->lock)
2824{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002825 int ret = 0;
2826
Kirill Korotaev054b9102006-12-10 02:20:11 -08002827 if (unlikely(!irqs_disabled())) {
2828 /* printk() doesn't work good under rq->lock */
2829 spin_unlock(&this_rq->lock);
2830 BUG_ON(1);
2831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002833 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 spin_unlock(&this_rq->lock);
2835 spin_lock(&busiest->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002836 spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002837 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 } else
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002839 spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002841 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02002844static void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
2845 __releases(busiest->lock)
2846{
2847 spin_unlock(&busiest->lock);
2848 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
2849}
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 * If dest_cpu is allowed for this process, migrate the task to it.
2853 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002854 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 * the cpu_allowed mask is restored.
2856 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002857static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002859 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002861 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 rq = task_rq_lock(p, &flags);
2864 if (!cpu_isset(dest_cpu, p->cpus_allowed)
Max Krasnyanskye761b772008-07-15 04:43:49 -07002865 || unlikely(!cpu_active(dest_cpu)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 goto out;
2867
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002868 trace_sched_migrate_task(rq, p, dest_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 /* force the process onto the specified CPU */
2870 if (migrate_task(p, dest_cpu, &req)) {
2871 /* Need to wait for migration thread (might exit: take ref). */
2872 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 get_task_struct(mt);
2875 task_rq_unlock(rq, &flags);
2876 wake_up_process(mt);
2877 put_task_struct(mt);
2878 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return;
2881 }
2882out:
2883 task_rq_unlock(rq, &flags);
2884}
2885
2886/*
Nick Piggin476d1392005-06-25 14:57:29 -07002887 * sched_exec - execve() is a valuable balancing opportunity, because at
2888 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 */
2890void sched_exec(void)
2891{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002893 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002895 if (new_cpu != this_cpu)
2896 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
2899/*
2900 * pull_task - move a task from a remote runqueue to the local runqueue.
2901 * Both runqueues must be locked.
2902 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002903static void pull_task(struct rq *src_rq, struct task_struct *p,
2904 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002906 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002908 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 /*
2910 * Note that idle threads have a prio of MAX_PRIO, for this test
2911 * to be always true for them.
2912 */
Peter Zijlstra15afe092008-09-20 23:38:02 +02002913 check_preempt_curr(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914}
2915
2916/*
2917 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2918 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002919static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002920int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002921 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002922 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
2924 /*
2925 * We do not migrate tasks that are:
2926 * 1) running (obviously), or
2927 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2928 * 3) are cache-hot on their current CPU.
2929 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002930 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2931 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002933 }
Nick Piggin81026792005-06-25 14:57:07 -07002934 *all_pinned = 0;
2935
Ingo Molnarcc367732007-10-15 17:00:18 +02002936 if (task_running(rq, p)) {
2937 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002938 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Ingo Molnarda84d962007-10-15 17:00:18 +02002941 /*
2942 * Aggressive migration if:
2943 * 1) task is cache cold, or
2944 * 2) too many balance attempts have failed.
2945 */
2946
Ingo Molnar6bc16652007-10-15 17:00:18 +02002947 if (!task_hot(p, rq->clock, sd) ||
2948 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002949#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002950 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002951 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002952 schedstat_inc(p, se.nr_forced_migrations);
2953 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002954#endif
2955 return 1;
2956 }
2957
Ingo Molnarcc367732007-10-15 17:00:18 +02002958 if (task_hot(p, rq->clock, sd)) {
2959 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002960 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 return 1;
2963}
2964
Peter Williamse1d14842007-10-24 18:23:51 +02002965static unsigned long
2966balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2967 unsigned long max_load_move, struct sched_domain *sd,
2968 enum cpu_idle_type idle, int *all_pinned,
2969 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002970{
Peter Zijlstra051c6762008-06-27 13:41:31 +02002971 int loops = 0, pulled = 0, pinned = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002972 struct task_struct *p;
2973 long rem_load_move = max_load_move;
2974
Peter Williamse1d14842007-10-24 18:23:51 +02002975 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002976 goto out;
2977
2978 pinned = 1;
2979
2980 /*
2981 * Start the load-balancing iterator:
2982 */
2983 p = iterator->start(iterator->arg);
2984next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002985 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002986 goto out;
Peter Zijlstra051c6762008-06-27 13:41:31 +02002987
2988 if ((p->se.load.weight >> 1) > rem_load_move ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002989 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002990 p = iterator->next(iterator->arg);
2991 goto next;
2992 }
2993
2994 pull_task(busiest, p, this_rq, this_cpu);
2995 pulled++;
2996 rem_load_move -= p->se.load.weight;
2997
2998 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002999 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003000 */
Peter Williamse1d14842007-10-24 18:23:51 +02003001 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003002 if (p->prio < *this_best_prio)
3003 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003004 p = iterator->next(iterator->arg);
3005 goto next;
3006 }
3007out:
3008 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003009 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003010 * so we can safely collect pull_task() stats here rather than
3011 * inside pull_task().
3012 */
3013 schedstat_add(sd, lb_gained[idle], pulled);
3014
3015 if (all_pinned)
3016 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003017
3018 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003019}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021/*
Peter Williams43010652007-08-09 11:16:46 +02003022 * move_tasks tries to move up to max_load_move weighted load from busiest to
3023 * this_rq, as part of a balancing operation within domain "sd".
3024 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 *
3026 * Called with both runqueues locked.
3027 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003028static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003029 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003030 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003031 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003033 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003034 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003035 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Ingo Molnardd41f592007-07-09 18:51:59 +02003037 do {
Peter Williams43010652007-08-09 11:16:46 +02003038 total_load_moved +=
3039 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003040 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003041 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003042 class = class->next;
Gregory Haskinsc4acb2c2008-06-27 14:29:55 -06003043
3044 if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
3045 break;
3046
Peter Williams43010652007-08-09 11:16:46 +02003047 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Peter Williams43010652007-08-09 11:16:46 +02003049 return total_load_moved > 0;
3050}
3051
Peter Williamse1d14842007-10-24 18:23:51 +02003052static int
3053iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3054 struct sched_domain *sd, enum cpu_idle_type idle,
3055 struct rq_iterator *iterator)
3056{
3057 struct task_struct *p = iterator->start(iterator->arg);
3058 int pinned = 0;
3059
3060 while (p) {
3061 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3062 pull_task(busiest, p, this_rq, this_cpu);
3063 /*
3064 * Right now, this is only the second place pull_task()
3065 * is called, so we can safely collect pull_task()
3066 * stats here rather than inside pull_task().
3067 */
3068 schedstat_inc(sd, lb_gained[idle]);
3069
3070 return 1;
3071 }
3072 p = iterator->next(iterator->arg);
3073 }
3074
3075 return 0;
3076}
3077
Peter Williams43010652007-08-09 11:16:46 +02003078/*
3079 * move_one_task tries to move exactly one task from busiest to this_rq, as
3080 * part of active balancing operations within "domain".
3081 * Returns 1 if successful and 0 otherwise.
3082 *
3083 * Called with both runqueues locked.
3084 */
3085static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3086 struct sched_domain *sd, enum cpu_idle_type idle)
3087{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003088 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003089
3090 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003091 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003092 return 1;
3093
3094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
3097/*
3098 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003099 * domain. It calculates and returns the amount of weighted load which
3100 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 */
3102static struct sched_group *
3103find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003104 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003105 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
3107 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3108 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003109 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003110 unsigned long busiest_load_per_task, busiest_nr_running;
3111 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003112 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003113#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3114 int power_savings_balance = 1;
3115 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3116 unsigned long min_nr_running = ULONG_MAX;
3117 struct sched_group *group_min = NULL, *group_leader = NULL;
3118#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003121 busiest_load_per_task = busiest_nr_running = 0;
3122 this_load_per_task = this_nr_running = 0;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003123
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003124 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003125 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003126 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003127 load_idx = sd->newidle_idx;
3128 else
3129 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
3131 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003132 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 int local_group;
3134 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003135 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003136 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003137 unsigned long sum_nr_running, sum_weighted_load;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003138 unsigned long sum_avg_load_per_task;
3139 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141 local_group = cpu_isset(this_cpu, group->cpumask);
3142
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003143 if (local_group)
3144 balance_cpu = first_cpu(group->cpumask);
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003147 sum_weighted_load = sum_nr_running = avg_load = 0;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003148 sum_avg_load_per_task = avg_load_per_task = 0;
3149
Ken Chen908a7c12007-10-17 16:55:11 +02003150 max_cpu_load = 0;
3151 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Mike Travis363ab6f2008-05-12 21:21:13 +02003153 for_each_cpu_mask_nr(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003154 struct rq *rq;
3155
3156 if (!cpu_isset(i, *cpus))
3157 continue;
3158
3159 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003160
Suresh Siddha9439aab2007-07-19 21:28:35 +02003161 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003162 *sd_idle = 0;
3163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003165 if (local_group) {
3166 if (idle_cpu(i) && !first_idle_cpu) {
3167 first_idle_cpu = 1;
3168 balance_cpu = i;
3169 }
3170
Nick Piggina2000572006-02-10 01:51:02 -08003171 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003172 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003173 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003174 if (load > max_cpu_load)
3175 max_cpu_load = load;
3176 if (min_cpu_load > load)
3177 min_cpu_load = load;
3178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
3180 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003181 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003182 sum_weighted_load += weighted_cpuload(i);
Peter Zijlstra408ed062008-06-27 13:41:28 +02003183
3184 sum_avg_load_per_task += cpu_avg_load_per_task(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 }
3186
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003187 /*
3188 * First idle cpu or the first cpu(busiest) in this sched group
3189 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003190 * domains. In the newly idle case, we will allow all the cpu's
3191 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003192 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003193 if (idle != CPU_NEWLY_IDLE && local_group &&
3194 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003195 *balance = 0;
3196 goto ret;
3197 }
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003200 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
3202 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003203 avg_load = sg_div_cpu_power(group,
3204 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Peter Zijlstra408ed062008-06-27 13:41:28 +02003206
3207 /*
3208 * Consider the group unbalanced when the imbalance is larger
3209 * than the average weight of two tasks.
3210 *
3211 * APZ: with cgroup the avg task weight can vary wildly and
3212 * might not be a suitable number - should we keep a
3213 * normalized nr_running number somewhere that negates
3214 * the hierarchy?
3215 */
3216 avg_load_per_task = sg_div_cpu_power(group,
3217 sum_avg_load_per_task * SCHED_LOAD_SCALE);
3218
3219 if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
Ken Chen908a7c12007-10-17 16:55:11 +02003220 __group_imb = 1;
3221
Eric Dumazet5517d862007-05-08 00:32:57 -07003222 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (local_group) {
3225 this_load = avg_load;
3226 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003227 this_nr_running = sum_nr_running;
3228 this_load_per_task = sum_weighted_load;
3229 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003230 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 max_load = avg_load;
3232 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003233 busiest_nr_running = sum_nr_running;
3234 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003235 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003237
3238#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3239 /*
3240 * Busy processors will not participate in power savings
3241 * balance.
3242 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003243 if (idle == CPU_NOT_IDLE ||
3244 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3245 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003246
3247 /*
3248 * If the local group is idle or completely loaded
3249 * no need to do power savings balance at this domain
3250 */
3251 if (local_group && (this_nr_running >= group_capacity ||
3252 !this_nr_running))
3253 power_savings_balance = 0;
3254
Ingo Molnardd41f592007-07-09 18:51:59 +02003255 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003256 * If a group is already running at full capacity or idle,
3257 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003258 */
3259 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003260 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003261 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003262
Ingo Molnardd41f592007-07-09 18:51:59 +02003263 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003264 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003265 * This is the group from where we need to pick up the load
3266 * for saving power
3267 */
3268 if ((sum_nr_running < min_nr_running) ||
3269 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003270 first_cpu(group->cpumask) <
3271 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003272 group_min = group;
3273 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003274 min_load_per_task = sum_weighted_load /
3275 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003276 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003277
Ingo Molnardd41f592007-07-09 18:51:59 +02003278 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003279 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003280 * capacity but still has some space to pick up some load
3281 * from other group and save more power
3282 */
3283 if (sum_nr_running <= group_capacity - 1) {
3284 if (sum_nr_running > leader_nr_running ||
3285 (sum_nr_running == leader_nr_running &&
3286 first_cpu(group->cpumask) >
3287 first_cpu(group_leader->cpumask))) {
3288 group_leader = group;
3289 leader_nr_running = sum_nr_running;
3290 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003291 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003292group_next:
3293#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 group = group->next;
3295 } while (group != sd->groups);
3296
Peter Williams2dd73a42006-06-27 02:54:34 -07003297 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 goto out_balanced;
3299
3300 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3301
3302 if (this_load >= avg_load ||
3303 100*max_load <= sd->imbalance_pct*this_load)
3304 goto out_balanced;
3305
Peter Williams2dd73a42006-06-27 02:54:34 -07003306 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003307 if (group_imb)
3308 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3309
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 /*
3311 * We're trying to get all the cpus to the average_load, so we don't
3312 * want to push ourselves above the average load, nor do we wish to
3313 * reduce the max loaded cpu below the average load, as either of these
3314 * actions would just result in more rebalancing later, and ping-pong
3315 * tasks around. Thus we look for the minimum possible imbalance.
3316 * Negative imbalances (*we* are more loaded than anyone else) will
3317 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003318 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 * appear as very large values with unsigned longs.
3320 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003321 if (max_load <= busiest_load_per_task)
3322 goto out_balanced;
3323
3324 /*
3325 * In the presence of smp nice balancing, certain scenarios can have
3326 * max load less than avg load(as we skip the groups at or below
3327 * its cpu_power, while calculating max_load..)
3328 */
3329 if (max_load < avg_load) {
3330 *imbalance = 0;
3331 goto small_imbalance;
3332 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003333
3334 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003335 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003338 *imbalance = min(max_pull * busiest->__cpu_power,
3339 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 / SCHED_LOAD_SCALE;
3341
Peter Williams2dd73a42006-06-27 02:54:34 -07003342 /*
3343 * if *imbalance is less than the average load per runnable task
3344 * there is no gaurantee that any tasks will be moved so we'll have
3345 * a think about bumping its value to force at least one task to be
3346 * moved
3347 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003348 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003349 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003350 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Peter Williams2dd73a42006-06-27 02:54:34 -07003352small_imbalance:
3353 pwr_move = pwr_now = 0;
3354 imbn = 2;
3355 if (this_nr_running) {
3356 this_load_per_task /= this_nr_running;
3357 if (busiest_load_per_task > this_load_per_task)
3358 imbn = 1;
3359 } else
Peter Zijlstra408ed062008-06-27 13:41:28 +02003360 this_load_per_task = cpu_avg_load_per_task(this_cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07003361
Peter Zijlstra01c8c572008-10-24 11:06:12 +02003362 if (max_load - this_load + busiest_load_per_task >=
Ingo Molnardd41f592007-07-09 18:51:59 +02003363 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003364 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 return busiest;
3366 }
3367
3368 /*
3369 * OK, we don't have enough imbalance to justify moving tasks,
3370 * however we may be able to increase total CPU power used by
3371 * moving them.
3372 */
3373
Eric Dumazet5517d862007-05-08 00:32:57 -07003374 pwr_now += busiest->__cpu_power *
3375 min(busiest_load_per_task, max_load);
3376 pwr_now += this->__cpu_power *
3377 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 pwr_now /= SCHED_LOAD_SCALE;
3379
3380 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003381 tmp = sg_div_cpu_power(busiest,
3382 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003384 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003385 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
3387 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003388 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003389 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003390 tmp = sg_div_cpu_power(this,
3391 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003393 tmp = sg_div_cpu_power(this,
3394 busiest_load_per_task * SCHED_LOAD_SCALE);
3395 pwr_move += this->__cpu_power *
3396 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 pwr_move /= SCHED_LOAD_SCALE;
3398
3399 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003400 if (pwr_move > pwr_now)
3401 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 }
3403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return busiest;
3405
3406out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003407#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003408 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003409 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003411 if (this == group_leader && group_leader != group_min) {
3412 *imbalance = min_load_per_task;
3413 return group_min;
3414 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003415#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003416ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 *imbalance = 0;
3418 return NULL;
3419}
3420
3421/*
3422 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3423 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003424static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003425find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003426 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003428 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003429 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 int i;
3431
Mike Travis363ab6f2008-05-12 21:21:13 +02003432 for_each_cpu_mask_nr(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003433 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003434
3435 if (!cpu_isset(i, *cpus))
3436 continue;
3437
Ingo Molnar48f24c42006-07-03 00:25:40 -07003438 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003439 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Ingo Molnardd41f592007-07-09 18:51:59 +02003441 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003442 continue;
3443
Ingo Molnardd41f592007-07-09 18:51:59 +02003444 if (wl > max_load) {
3445 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003446 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 }
3448 }
3449
3450 return busiest;
3451}
3452
3453/*
Nick Piggin77391d72005-06-25 14:57:30 -07003454 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3455 * so long as it is large enough.
3456 */
3457#define MAX_PINNED_INTERVAL 512
3458
3459/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3461 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003463static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003464 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003465 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
Peter Williams43010652007-08-09 11:16:46 +02003467 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003470 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003471 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003472
Mike Travis7c16ec52008-04-04 18:11:11 -07003473 cpus_setall(*cpus);
3474
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003475 /*
3476 * When power savings policy is enabled for the parent domain, idle
3477 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003478 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003479 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003480 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003481 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003482 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003483 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Ingo Molnar2d723762007-10-15 17:00:12 +02003485 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003487redo:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02003488 update_shares(sd);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003489 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003490 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003491
Chen, Kenneth W06066712006-12-10 02:20:35 -08003492 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003493 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 if (!group) {
3496 schedstat_inc(sd, lb_nobusyg[idle]);
3497 goto out_balanced;
3498 }
3499
Mike Travis7c16ec52008-04-04 18:11:11 -07003500 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 if (!busiest) {
3502 schedstat_inc(sd, lb_nobusyq[idle]);
3503 goto out_balanced;
3504 }
3505
Nick Piggindb935db2005-06-25 14:57:11 -07003506 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
3508 schedstat_add(sd, lb_imbalance[idle], imbalance);
3509
Peter Williams43010652007-08-09 11:16:46 +02003510 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 if (busiest->nr_running > 1) {
3512 /*
3513 * Attempt to move tasks. If find_busiest_group has found
3514 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003515 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 * correctly treated as an imbalance.
3517 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003518 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003519 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003520 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003521 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003522 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003523 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003524
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003525 /*
3526 * some other cpu did the load balance for us.
3527 */
Peter Williams43010652007-08-09 11:16:46 +02003528 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003529 resched_cpu(this_cpu);
3530
Nick Piggin81026792005-06-25 14:57:07 -07003531 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003532 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003533 cpu_clear(cpu_of(busiest), *cpus);
3534 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003535 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003536 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
Nick Piggin81026792005-06-25 14:57:07 -07003539
Peter Williams43010652007-08-09 11:16:46 +02003540 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 schedstat_inc(sd, lb_failed[idle]);
3542 sd->nr_balance_failed++;
3543
3544 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003546 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003547
3548 /* don't kick the migration_thread, if the curr
3549 * task on busiest cpu can't be moved to this_cpu
3550 */
3551 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003552 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003553 all_pinned = 1;
3554 goto out_one_pinned;
3555 }
3556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 if (!busiest->active_balance) {
3558 busiest->active_balance = 1;
3559 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003560 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003562 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003563 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 wake_up_process(busiest->migration_thread);
3565
3566 /*
3567 * We've kicked active balancing, reset the failure
3568 * counter.
3569 */
Nick Piggin39507452005-06-25 14:57:09 -07003570 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 }
Nick Piggin81026792005-06-25 14:57:07 -07003572 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 sd->nr_balance_failed = 0;
3574
Nick Piggin81026792005-06-25 14:57:07 -07003575 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 /* We were unbalanced, so reset the balancing interval */
3577 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003578 } else {
3579 /*
3580 * If we've begun active balancing, start to back off. This
3581 * case may not be covered by the all_pinned logic if there
3582 * is only 1 task on the busy runqueue (because we don't call
3583 * move_tasks).
3584 */
3585 if (sd->balance_interval < sd->max_interval)
3586 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 }
3588
Peter Williams43010652007-08-09 11:16:46 +02003589 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003590 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003591 ld_moved = -1;
3592
3593 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
3595out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 schedstat_inc(sd, lb_balanced[idle]);
3597
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003598 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003599
3600out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003602 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3603 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 sd->balance_interval *= 2;
3605
Ingo Molnar48f24c42006-07-03 00:25:40 -07003606 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003607 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003608 ld_moved = -1;
3609 else
3610 ld_moved = 0;
3611out:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02003612 if (ld_moved)
3613 update_shares(sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003614 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
3617/*
3618 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3619 * tasks if there is an imbalance.
3620 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003621 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 * this_rq is locked.
3623 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003624static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003625load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3626 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003629 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003631 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003632 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003633 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003634
3635 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003636
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003637 /*
3638 * When power savings policy is enabled for the parent domain, idle
3639 * sibling can pick up load irrespective of busy siblings. In this case,
3640 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003641 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003642 */
3643 if (sd->flags & SD_SHARE_CPUPOWER &&
3644 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003645 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Ingo Molnar2d723762007-10-15 17:00:12 +02003647 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003648redo:
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02003649 update_shares_locked(this_rq, sd);
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003650 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003651 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003653 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003654 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 }
3656
Mike Travis7c16ec52008-04-04 18:11:11 -07003657 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003658 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003659 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003660 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 }
3662
Nick Piggindb935db2005-06-25 14:57:11 -07003663 BUG_ON(busiest == this_rq);
3664
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003665 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003666
Peter Williams43010652007-08-09 11:16:46 +02003667 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003668 if (busiest->nr_running > 1) {
3669 /* Attempt to move tasks */
3670 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003671 /* this_rq->clock is already updated */
3672 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003673 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003674 imbalance, sd, CPU_NEWLY_IDLE,
3675 &all_pinned);
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02003676 double_unlock_balance(this_rq, busiest);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003677
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003678 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003679 cpu_clear(cpu_of(busiest), *cpus);
3680 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003681 goto redo;
3682 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003683 }
3684
Peter Williams43010652007-08-09 11:16:46 +02003685 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003686 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003687 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3688 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003689 return -1;
3690 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003691 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02003693 update_shares_locked(this_rq, sd);
Peter Williams43010652007-08-09 11:16:46 +02003694 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003695
3696out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003697 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003698 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003699 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003700 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003701 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003702
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704}
3705
3706/*
3707 * idle_balance is called by schedule() if this_cpu is about to become
3708 * idle. Attempts to pull tasks from other CPUs.
3709 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003710static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
3712 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003713 int pulled_task = -1;
3714 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003715 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003718 unsigned long interval;
3719
3720 if (!(sd->flags & SD_LOAD_BALANCE))
3721 continue;
3722
3723 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003724 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003725 pulled_task = load_balance_newidle(this_cpu, this_rq,
3726 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003727
3728 interval = msecs_to_jiffies(sd->balance_interval);
3729 if (time_after(next_balance, sd->last_balance + interval))
3730 next_balance = sd->last_balance + interval;
3731 if (pulled_task)
3732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003734 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003735 /*
3736 * We are going idle. next_balance may be set based on
3737 * a busy processor. So reset next_balance.
3738 */
3739 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741}
3742
3743/*
3744 * active_load_balance is run by migration threads. It pushes running tasks
3745 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3746 * running on each physical CPU where possible, and avoids physical /
3747 * logical imbalances.
3748 *
3749 * Called with busiest_rq locked.
3750 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003751static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
Nick Piggin39507452005-06-25 14:57:09 -07003753 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003754 struct sched_domain *sd;
3755 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003756
Ingo Molnar48f24c42006-07-03 00:25:40 -07003757 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003758 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003759 return;
3760
3761 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
3763 /*
Nick Piggin39507452005-06-25 14:57:09 -07003764 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003765 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003766 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 */
Nick Piggin39507452005-06-25 14:57:09 -07003768 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Nick Piggin39507452005-06-25 14:57:09 -07003770 /* move a task from busiest_rq to target_rq */
3771 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003772 update_rq_clock(busiest_rq);
3773 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Nick Piggin39507452005-06-25 14:57:09 -07003775 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003776 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003777 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003778 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003779 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Ingo Molnar48f24c42006-07-03 00:25:40 -07003782 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003783 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Peter Williams43010652007-08-09 11:16:46 +02003785 if (move_one_task(target_rq, target_cpu, busiest_rq,
3786 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003787 schedstat_inc(sd, alb_pushed);
3788 else
3789 schedstat_inc(sd, alb_failed);
3790 }
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02003791 double_unlock_balance(busiest_rq, target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003794#ifdef CONFIG_NO_HZ
3795static struct {
3796 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003797 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003798} nohz ____cacheline_aligned = {
3799 .load_balancer = ATOMIC_INIT(-1),
3800 .cpu_mask = CPU_MASK_NONE,
3801};
3802
Christoph Lameter7835b982006-12-10 02:20:22 -08003803/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003804 * This routine will try to nominate the ilb (idle load balancing)
3805 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3806 * load balancing on behalf of all those cpus. If all the cpus in the system
3807 * go into this tickless mode, then there will be no ilb owner (as there is
3808 * no need for one) and all the cpus will sleep till the next wakeup event
3809 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003810 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003811 * For the ilb owner, tick is not stopped. And this tick will be used
3812 * for idle load balancing. ilb owner will still be part of
3813 * nohz.cpu_mask..
3814 *
3815 * While stopping the tick, this cpu will become the ilb owner if there
3816 * is no other owner. And will be the owner till that cpu becomes busy
3817 * or if all cpus in the system stop their ticks at which point
3818 * there is no need for ilb owner.
3819 *
3820 * When the ilb owner becomes busy, it nominates another owner, during the
3821 * next busy scheduler_tick()
3822 */
3823int select_nohz_load_balancer(int stop_tick)
3824{
3825 int cpu = smp_processor_id();
3826
3827 if (stop_tick) {
3828 cpu_set(cpu, nohz.cpu_mask);
3829 cpu_rq(cpu)->in_nohz_recently = 1;
3830
3831 /*
3832 * If we are going offline and still the leader, give up!
3833 */
Max Krasnyanskye761b772008-07-15 04:43:49 -07003834 if (!cpu_active(cpu) &&
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003835 atomic_read(&nohz.load_balancer) == cpu) {
3836 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3837 BUG();
3838 return 0;
3839 }
3840
3841 /* time for ilb owner also to sleep */
3842 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3843 if (atomic_read(&nohz.load_balancer) == cpu)
3844 atomic_set(&nohz.load_balancer, -1);
3845 return 0;
3846 }
3847
3848 if (atomic_read(&nohz.load_balancer) == -1) {
3849 /* make me the ilb owner */
3850 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3851 return 1;
3852 } else if (atomic_read(&nohz.load_balancer) == cpu)
3853 return 1;
3854 } else {
3855 if (!cpu_isset(cpu, nohz.cpu_mask))
3856 return 0;
3857
3858 cpu_clear(cpu, nohz.cpu_mask);
3859
3860 if (atomic_read(&nohz.load_balancer) == cpu)
3861 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3862 BUG();
3863 }
3864 return 0;
3865}
3866#endif
3867
3868static DEFINE_SPINLOCK(balancing);
3869
3870/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003871 * It checks each scheduling domain to see if it is due to be balanced,
3872 * and initiates a balancing operation if so.
3873 *
3874 * Balancing parameters are set up in arch_init_sched_domains.
3875 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003876static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003877{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003878 int balance = 1;
3879 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003880 unsigned long interval;
3881 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003882 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003883 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003884 int update_next_balance = 0;
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003885 int need_serialize;
Mike Travis7c16ec52008-04-04 18:11:11 -07003886 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003888 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 if (!(sd->flags & SD_LOAD_BALANCE))
3890 continue;
3891
3892 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003893 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 interval *= sd->busy_factor;
3895
3896 /* scale ms to jiffies */
3897 interval = msecs_to_jiffies(interval);
3898 if (unlikely(!interval))
3899 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003900 if (interval > HZ*NR_CPUS/10)
3901 interval = HZ*NR_CPUS/10;
3902
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003903 need_serialize = sd->flags & SD_SERIALIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003905 if (need_serialize) {
Christoph Lameter08c183f2006-12-10 02:20:29 -08003906 if (!spin_trylock(&balancing))
3907 goto out;
3908 }
3909
Christoph Lameterc9819f42006-12-10 02:20:25 -08003910 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003911 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003912 /*
3913 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003914 * longer idle, or one of our SMT siblings is
3915 * not idle.
3916 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003917 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003919 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 }
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003921 if (need_serialize)
Christoph Lameter08c183f2006-12-10 02:20:29 -08003922 spin_unlock(&balancing);
3923out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003924 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003925 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003926 update_next_balance = 1;
3927 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003928
3929 /*
3930 * Stop the load balance at this level. There is another
3931 * CPU in our sched group which is doing load balancing more
3932 * actively.
3933 */
3934 if (!balance)
3935 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003937
3938 /*
3939 * next_balance will be updated only when there is a need.
3940 * When the cpu is attached to null domain for ex, it will not be
3941 * updated.
3942 */
3943 if (likely(update_next_balance))
3944 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003945}
3946
3947/*
3948 * run_rebalance_domains is triggered when needed from the scheduler tick.
3949 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3950 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3951 */
3952static void run_rebalance_domains(struct softirq_action *h)
3953{
Ingo Molnardd41f592007-07-09 18:51:59 +02003954 int this_cpu = smp_processor_id();
3955 struct rq *this_rq = cpu_rq(this_cpu);
3956 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3957 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003958
Ingo Molnardd41f592007-07-09 18:51:59 +02003959 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003960
3961#ifdef CONFIG_NO_HZ
3962 /*
3963 * If this cpu is the owner for idle load balancing, then do the
3964 * balancing on behalf of the other idle cpus whose ticks are
3965 * stopped.
3966 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003967 if (this_rq->idle_at_tick &&
3968 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003969 cpumask_t cpus = nohz.cpu_mask;
3970 struct rq *rq;
3971 int balance_cpu;
3972
Ingo Molnardd41f592007-07-09 18:51:59 +02003973 cpu_clear(this_cpu, cpus);
Mike Travis363ab6f2008-05-12 21:21:13 +02003974 for_each_cpu_mask_nr(balance_cpu, cpus) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003975 /*
3976 * If this cpu gets work to do, stop the load balancing
3977 * work being done for other cpus. Next load
3978 * balancing owner will pick it up.
3979 */
3980 if (need_resched())
3981 break;
3982
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003983 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003984
3985 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003986 if (time_after(this_rq->next_balance, rq->next_balance))
3987 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003988 }
3989 }
3990#endif
3991}
3992
3993/*
3994 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3995 *
3996 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3997 * idle load balancing owner or decide to stop the periodic load balancing,
3998 * if the whole system is idle.
3999 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004000static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004001{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004002#ifdef CONFIG_NO_HZ
4003 /*
4004 * If we were in the nohz mode recently and busy at the current
4005 * scheduler tick, then check if we need to nominate new idle
4006 * load balancer.
4007 */
4008 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4009 rq->in_nohz_recently = 0;
4010
4011 if (atomic_read(&nohz.load_balancer) == cpu) {
4012 cpu_clear(cpu, nohz.cpu_mask);
4013 atomic_set(&nohz.load_balancer, -1);
4014 }
4015
4016 if (atomic_read(&nohz.load_balancer) == -1) {
4017 /*
4018 * simple selection for now: Nominate the
4019 * first cpu in the nohz list to be the next
4020 * ilb owner.
4021 *
4022 * TBD: Traverse the sched domains and nominate
4023 * the nearest cpu in the nohz.cpu_mask.
4024 */
4025 int ilb = first_cpu(nohz.cpu_mask);
4026
Mike Travis434d53b2008-04-04 18:11:04 -07004027 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004028 resched_cpu(ilb);
4029 }
4030 }
4031
4032 /*
4033 * If this cpu is idle and doing idle load balancing for all the
4034 * cpus with ticks stopped, is it time for that to stop?
4035 */
4036 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4037 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4038 resched_cpu(cpu);
4039 return;
4040 }
4041
4042 /*
4043 * If this cpu is idle and the idle load balancing is done by
4044 * someone else, then no need raise the SCHED_SOFTIRQ
4045 */
4046 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4047 cpu_isset(cpu, nohz.cpu_mask))
4048 return;
4049#endif
4050 if (time_after_eq(jiffies, rq->next_balance))
4051 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052}
Ingo Molnardd41f592007-07-09 18:51:59 +02004053
4054#else /* CONFIG_SMP */
4055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056/*
4057 * on UP we do not need to balance between CPUs:
4058 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004059static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060{
4061}
Ingo Molnardd41f592007-07-09 18:51:59 +02004062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063#endif
4064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065DEFINE_PER_CPU(struct kernel_stat, kstat);
4066
4067EXPORT_PER_CPU_SYMBOL(kstat);
4068
4069/*
Frank Mayharf06febc2008-09-12 09:54:39 -07004070 * Return any ns on the sched_clock that have not yet been banked in
4071 * @p in case that task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 */
Frank Mayharbb34d922008-09-12 09:54:39 -07004073unsigned long long task_delta_exec(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004076 struct rq *rq;
Frank Mayharbb34d922008-09-12 09:54:39 -07004077 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004078
Ingo Molnar41b86e92007-07-09 18:51:58 +02004079 rq = task_rq_lock(p, &flags);
Ingo Molnar15084872008-09-30 08:28:17 +02004080
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004081 if (task_current(rq, p)) {
Frank Mayharf06febc2008-09-12 09:54:39 -07004082 u64 delta_exec;
4083
Ingo Molnara8e504d2007-08-09 11:16:47 +02004084 update_rq_clock(rq);
4085 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004086 if ((s64)delta_exec > 0)
Frank Mayharbb34d922008-09-12 09:54:39 -07004087 ns = delta_exec;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004088 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07004089
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 task_rq_unlock(rq, &flags);
4091
4092 return ns;
4093}
4094
4095/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 * Account user cpu time to a process.
4097 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 * @cputime: the cpu time spent in user space since the last update
4099 */
4100void account_user_time(struct task_struct *p, cputime_t cputime)
4101{
4102 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4103 cputime64_t tmp;
4104
4105 p->utime = cputime_add(p->utime, cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07004106 account_group_user_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
4108 /* Add user time to cpustat. */
4109 tmp = cputime_to_cputime64(cputime);
4110 if (TASK_NICE(p) > 0)
4111 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4112 else
4113 cpustat->user = cputime64_add(cpustat->user, tmp);
Jonathan Lim49b5cf32008-07-25 01:48:40 -07004114 /* Account for user time used */
4115 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
4118/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004119 * Account guest cpu time to a process.
4120 * @p: the process that the cpu time gets accounted to
4121 * @cputime: the cpu time spent in virtual machine since the last update
4122 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004123static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004124{
4125 cputime64_t tmp;
4126 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4127
4128 tmp = cputime_to_cputime64(cputime);
4129
4130 p->utime = cputime_add(p->utime, cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07004131 account_group_user_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02004132 p->gtime = cputime_add(p->gtime, cputime);
4133
4134 cpustat->user = cputime64_add(cpustat->user, tmp);
4135 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4136}
4137
4138/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004139 * Account scaled user cpu time to a process.
4140 * @p: the process that the cpu time gets accounted to
4141 * @cputime: the cpu time spent in user space since the last update
4142 */
4143void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4144{
4145 p->utimescaled = cputime_add(p->utimescaled, cputime);
4146}
4147
4148/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 * Account system cpu time to a process.
4150 * @p: the process that the cpu time gets accounted to
4151 * @hardirq_offset: the offset to subtract from hardirq_count()
4152 * @cputime: the cpu time spent in kernel space since the last update
4153 */
4154void account_system_time(struct task_struct *p, int hardirq_offset,
4155 cputime_t cputime)
4156{
4157 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004158 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 cputime64_t tmp;
4160
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004161 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4162 account_guest_time(p, cputime);
4163 return;
4164 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004165
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 p->stime = cputime_add(p->stime, cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07004167 account_group_system_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
4169 /* Add system time to cpustat. */
4170 tmp = cputime_to_cputime64(cputime);
4171 if (hardirq_count() - hardirq_offset)
4172 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4173 else if (softirq_count())
4174 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004175 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004177 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4179 else
4180 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4181 /* Account for system time used */
4182 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183}
4184
4185/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004186 * Account scaled system cpu time to a process.
4187 * @p: the process that the cpu time gets accounted to
4188 * @hardirq_offset: the offset to subtract from hardirq_count()
4189 * @cputime: the cpu time spent in kernel space since the last update
4190 */
4191void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4192{
4193 p->stimescaled = cputime_add(p->stimescaled, cputime);
4194}
4195
4196/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 * Account for involuntary wait time.
4198 * @p: the process from which the cpu time has been stolen
4199 * @steal: the cpu time spent in involuntary wait
4200 */
4201void account_steal_time(struct task_struct *p, cputime_t steal)
4202{
4203 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4204 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004205 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
4207 if (p == rq->idle) {
4208 p->stime = cputime_add(p->stime, steal);
Frank Mayharf06febc2008-09-12 09:54:39 -07004209 account_group_system_time(p, steal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 if (atomic_read(&rq->nr_iowait) > 0)
4211 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4212 else
4213 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004214 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4216}
4217
Christoph Lameter7835b982006-12-10 02:20:22 -08004218/*
Balbir Singh49048622008-09-05 18:12:23 +02004219 * Use precise platform statistics if available:
4220 */
4221#ifdef CONFIG_VIRT_CPU_ACCOUNTING
4222cputime_t task_utime(struct task_struct *p)
4223{
4224 return p->utime;
4225}
4226
4227cputime_t task_stime(struct task_struct *p)
4228{
4229 return p->stime;
4230}
4231#else
4232cputime_t task_utime(struct task_struct *p)
4233{
4234 clock_t utime = cputime_to_clock_t(p->utime),
4235 total = utime + cputime_to_clock_t(p->stime);
4236 u64 temp;
4237
4238 /*
4239 * Use CFS's precise accounting:
4240 */
4241 temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
4242
4243 if (total) {
4244 temp *= utime;
4245 do_div(temp, total);
4246 }
4247 utime = (clock_t)temp;
4248
4249 p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
4250 return p->prev_utime;
4251}
4252
4253cputime_t task_stime(struct task_struct *p)
4254{
4255 clock_t stime;
4256
4257 /*
4258 * Use CFS's precise accounting. (we subtract utime from
4259 * the total, to make sure the total observed by userspace
4260 * grows monotonically - apps rely on that):
4261 */
4262 stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
4263 cputime_to_clock_t(task_utime(p));
4264
4265 if (stime >= 0)
4266 p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
4267
4268 return p->prev_stime;
4269}
4270#endif
4271
4272inline cputime_t task_gtime(struct task_struct *p)
4273{
4274 return p->gtime;
4275}
4276
4277/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004278 * This function gets called by the timer code, with HZ frequency.
4279 * We call it with interrupts disabled.
4280 *
4281 * It also gets called by the fork code, when changing the parent's
4282 * timeslices.
4283 */
4284void scheduler_tick(void)
4285{
Christoph Lameter7835b982006-12-10 02:20:22 -08004286 int cpu = smp_processor_id();
4287 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004288 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004289
4290 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004291
Ingo Molnardd41f592007-07-09 18:51:59 +02004292 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004293 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004294 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004295 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004296 spin_unlock(&rq->lock);
4297
Christoph Lametere418e1c2006-12-10 02:20:23 -08004298#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004299 rq->idle_at_tick = idle_cpu(cpu);
4300 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004301#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302}
4303
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004304#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
4305 defined(CONFIG_PREEMPT_TRACER))
4306
4307static inline unsigned long get_parent_ip(unsigned long addr)
4308{
4309 if (in_lock_functions(addr)) {
4310 addr = CALLER_ADDR2;
4311 if (in_lock_functions(addr))
4312 addr = CALLER_ADDR3;
4313 }
4314 return addr;
4315}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
Srinivasa Ds43627582008-02-23 15:24:04 -08004317void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004319#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 /*
4321 * Underflow?
4322 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004323 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4324 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004325#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004327#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 /*
4329 * Spinlock count overflowing soon?
4330 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004331 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4332 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004333#endif
4334 if (preempt_count() == val)
4335 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336}
4337EXPORT_SYMBOL(add_preempt_count);
4338
Srinivasa Ds43627582008-02-23 15:24:04 -08004339void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004341#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 /*
4343 * Underflow?
4344 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004345 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4346 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 /*
4348 * Is the spinlock portion underflowing?
4349 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004350 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4351 !(preempt_count() & PREEMPT_MASK)))
4352 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004353#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004354
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004355 if (preempt_count() == val)
4356 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 preempt_count() -= val;
4358}
4359EXPORT_SYMBOL(sub_preempt_count);
4360
4361#endif
4362
4363/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004364 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004366static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367{
Satyam Sharma838225b2007-10-24 18:23:50 +02004368 struct pt_regs *regs = get_irq_regs();
4369
4370 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4371 prev->comm, prev->pid, preempt_count());
4372
Ingo Molnardd41f592007-07-09 18:51:59 +02004373 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07004374 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02004375 if (irqs_disabled())
4376 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004377
4378 if (regs)
4379 show_regs(regs);
4380 else
4381 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004382}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
Ingo Molnardd41f592007-07-09 18:51:59 +02004384/*
4385 * Various schedule()-time debugging checks and statistics:
4386 */
4387static inline void schedule_debug(struct task_struct *prev)
4388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004390 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 * schedule() atomically, we ignore that path for now.
4392 * Otherwise, whine if we are scheduling when we should not be.
4393 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02004394 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02004395 __schedule_bug(prev);
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4398
Ingo Molnar2d723762007-10-15 17:00:12 +02004399 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004400#ifdef CONFIG_SCHEDSTATS
4401 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004402 schedstat_inc(this_rq(), bkl_count);
4403 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004404 }
4405#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004406}
4407
4408/*
4409 * Pick up the highest-prio task:
4410 */
4411static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004412pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004413{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004414 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004415 struct task_struct *p;
4416
4417 /*
4418 * Optimization: we know that if all tasks are in
4419 * the fair class we can call that function directly:
4420 */
4421 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004422 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004423 if (likely(p))
4424 return p;
4425 }
4426
4427 class = sched_class_highest;
4428 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004429 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004430 if (p)
4431 return p;
4432 /*
4433 * Will never be NULL as the idle class always
4434 * returns a non-NULL p:
4435 */
4436 class = class->next;
4437 }
4438}
4439
4440/*
4441 * schedule() is the main scheduler function.
4442 */
4443asmlinkage void __sched schedule(void)
4444{
4445 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004446 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004447 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02004448 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02004449
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450need_resched:
4451 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004452 cpu = smp_processor_id();
4453 rq = cpu_rq(cpu);
4454 rcu_qsctr_inc(cpu);
4455 prev = rq->curr;
4456 switch_count = &prev->nivcsw;
4457
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 release_kernel_lock(prev);
4459need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Ingo Molnardd41f592007-07-09 18:51:59 +02004461 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Peter Zijlstra31656512008-07-18 18:01:23 +02004463 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004464 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004465
Peter Zijlstra8cd162c2008-10-15 20:37:23 +02004466 spin_lock_irq(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004467 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004468 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
Ingo Molnardd41f592007-07-09 18:51:59 +02004470 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Oleg Nesterov16882c12008-06-08 21:20:41 +04004471 if (unlikely(signal_pending_state(prev->state, prev)))
Ingo Molnardd41f592007-07-09 18:51:59 +02004472 prev->state = TASK_RUNNING;
Oleg Nesterov16882c12008-06-08 21:20:41 +04004473 else
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004474 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004475 switch_count = &prev->nvcsw;
4476 }
4477
Steven Rostedt9a897c52008-01-25 21:08:22 +01004478#ifdef CONFIG_SMP
4479 if (prev->sched_class->pre_schedule)
4480 prev->sched_class->pre_schedule(rq, prev);
4481#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004482
Ingo Molnardd41f592007-07-09 18:51:59 +02004483 if (unlikely(!rq->nr_running))
4484 idle_balance(cpu, rq);
4485
Ingo Molnar31ee5292007-08-09 11:16:49 +02004486 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004487 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01004490 sched_info_switch(prev, next);
4491
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 rq->nr_switches++;
4493 rq->curr = next;
4494 ++*switch_count;
4495
Ingo Molnardd41f592007-07-09 18:51:59 +02004496 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004497 /*
4498 * the context switch might have flipped the stack from under
4499 * us, hence refresh the local variables.
4500 */
4501 cpu = smp_processor_id();
4502 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 } else
4504 spin_unlock_irq(&rq->lock);
4505
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004506 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004508
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 preempt_enable_no_resched();
4510 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4511 goto need_resched;
4512}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513EXPORT_SYMBOL(schedule);
4514
4515#ifdef CONFIG_PREEMPT
4516/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004517 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004518 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 * occur there and call schedule directly.
4520 */
4521asmlinkage void __sched preempt_schedule(void)
4522{
4523 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004524
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 /*
4526 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004527 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004529 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 return;
4531
Andi Kleen3a5c3592007-10-15 17:00:14 +02004532 do {
4533 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004534 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004535 sub_preempt_count(PREEMPT_ACTIVE);
4536
4537 /*
4538 * Check again in case we missed a preemption opportunity
4539 * between schedule and now.
4540 */
4541 barrier();
4542 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544EXPORT_SYMBOL(preempt_schedule);
4545
4546/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004547 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 * off of irq context.
4549 * Note, that this is called and return with irqs disabled. This will
4550 * protect us against recursive calling from irq.
4551 */
4552asmlinkage void __sched preempt_schedule_irq(void)
4553{
4554 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004555
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004556 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 BUG_ON(ti->preempt_count || !irqs_disabled());
4558
Andi Kleen3a5c3592007-10-15 17:00:14 +02004559 do {
4560 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004561 local_irq_enable();
4562 schedule();
4563 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004564 sub_preempt_count(PREEMPT_ACTIVE);
4565
4566 /*
4567 * Check again in case we missed a preemption opportunity
4568 * between schedule and now.
4569 */
4570 barrier();
4571 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572}
4573
4574#endif /* CONFIG_PREEMPT */
4575
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004576int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4577 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004579 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581EXPORT_SYMBOL(default_wake_function);
4582
4583/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004584 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4585 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 * number) then we wake all the non-exclusive tasks and one exclusive task.
4587 *
4588 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004589 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4591 */
4592static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4593 int nr_exclusive, int sync, void *key)
4594{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004595 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004597 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004598 unsigned flags = curr->flags;
4599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004601 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 break;
4603 }
4604}
4605
4606/**
4607 * __wake_up - wake up threads blocked on a waitqueue.
4608 * @q: the waitqueue
4609 * @mode: which threads
4610 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004611 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004613void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004614 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615{
4616 unsigned long flags;
4617
4618 spin_lock_irqsave(&q->lock, flags);
4619 __wake_up_common(q, mode, nr_exclusive, 0, key);
4620 spin_unlock_irqrestore(&q->lock, flags);
4621}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622EXPORT_SYMBOL(__wake_up);
4623
4624/*
4625 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4626 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004627void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628{
4629 __wake_up_common(q, mode, 1, 0, NULL);
4630}
4631
4632/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004633 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 * @q: the waitqueue
4635 * @mode: which threads
4636 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4637 *
4638 * The sync wakeup differs that the waker knows that it will schedule
4639 * away soon, so while the target thread will be woken up, it will not
4640 * be migrated to another CPU - ie. the two threads are 'synchronized'
4641 * with each other. This can prevent needless bouncing between CPUs.
4642 *
4643 * On UP it can prevent extra preemption.
4644 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004645void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004646__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647{
4648 unsigned long flags;
4649 int sync = 1;
4650
4651 if (unlikely(!q))
4652 return;
4653
4654 if (unlikely(!nr_exclusive))
4655 sync = 0;
4656
4657 spin_lock_irqsave(&q->lock, flags);
4658 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4659 spin_unlock_irqrestore(&q->lock, flags);
4660}
4661EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4662
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004663/**
4664 * complete: - signals a single thread waiting on this completion
4665 * @x: holds the state of this particular completion
4666 *
4667 * This will wake up a single thread waiting on this completion. Threads will be
4668 * awakened in the same order in which they were queued.
4669 *
4670 * See also complete_all(), wait_for_completion() and related routines.
4671 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004672void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673{
4674 unsigned long flags;
4675
4676 spin_lock_irqsave(&x->wait.lock, flags);
4677 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004678 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 spin_unlock_irqrestore(&x->wait.lock, flags);
4680}
4681EXPORT_SYMBOL(complete);
4682
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004683/**
4684 * complete_all: - signals all threads waiting on this completion
4685 * @x: holds the state of this particular completion
4686 *
4687 * This will wake up all threads waiting on this particular completion event.
4688 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004689void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690{
4691 unsigned long flags;
4692
4693 spin_lock_irqsave(&x->wait.lock, flags);
4694 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004695 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 spin_unlock_irqrestore(&x->wait.lock, flags);
4697}
4698EXPORT_SYMBOL(complete_all);
4699
Andi Kleen8cbbe862007-10-15 17:00:14 +02004700static inline long __sched
4701do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 if (!x->done) {
4704 DECLARE_WAITQUEUE(wait, current);
4705
4706 wait.flags |= WQ_FLAG_EXCLUSIVE;
4707 __add_wait_queue_tail(&x->wait, &wait);
4708 do {
Oleg Nesterov94d3d822008-08-20 16:54:41 -07004709 if (signal_pending_state(state, current)) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04004710 timeout = -ERESTARTSYS;
4711 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004712 }
4713 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004715 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004717 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004719 if (!x->done)
4720 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 }
4722 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04004723 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004724}
4725
4726static long __sched
4727wait_for_common(struct completion *x, long timeout, int state)
4728{
4729 might_sleep();
4730
4731 spin_lock_irq(&x->wait.lock);
4732 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004734 return timeout;
4735}
4736
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004737/**
4738 * wait_for_completion: - waits for completion of a task
4739 * @x: holds the state of this particular completion
4740 *
4741 * This waits to be signaled for completion of a specific task. It is NOT
4742 * interruptible and there is no timeout.
4743 *
4744 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
4745 * and interrupt capability. Also see complete().
4746 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004747void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004748{
4749 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750}
4751EXPORT_SYMBOL(wait_for_completion);
4752
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004753/**
4754 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
4755 * @x: holds the state of this particular completion
4756 * @timeout: timeout value in jiffies
4757 *
4758 * This waits for either a completion of a specific task to be signaled or for a
4759 * specified timeout to expire. The timeout is in jiffies. It is not
4760 * interruptible.
4761 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004762unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4764{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004765 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766}
4767EXPORT_SYMBOL(wait_for_completion_timeout);
4768
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004769/**
4770 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
4771 * @x: holds the state of this particular completion
4772 *
4773 * This waits for completion of a specific task to be signaled. It is
4774 * interruptible.
4775 */
Andi Kleen8cbbe862007-10-15 17:00:14 +02004776int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777{
Andi Kleen51e97992007-10-18 21:32:55 +02004778 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4779 if (t == -ERESTARTSYS)
4780 return t;
4781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782}
4783EXPORT_SYMBOL(wait_for_completion_interruptible);
4784
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004785/**
4786 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
4787 * @x: holds the state of this particular completion
4788 * @timeout: timeout value in jiffies
4789 *
4790 * This waits for either a completion of a specific task to be signaled or for a
4791 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
4792 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004793unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794wait_for_completion_interruptible_timeout(struct completion *x,
4795 unsigned long timeout)
4796{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004797 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798}
4799EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4800
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004801/**
4802 * wait_for_completion_killable: - waits for completion of a task (killable)
4803 * @x: holds the state of this particular completion
4804 *
4805 * This waits to be signaled for completion of a specific task. It can be
4806 * interrupted by a kill signal.
4807 */
Matthew Wilcox009e5772007-12-06 12:29:54 -05004808int __sched wait_for_completion_killable(struct completion *x)
4809{
4810 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4811 if (t == -ERESTARTSYS)
4812 return t;
4813 return 0;
4814}
4815EXPORT_SYMBOL(wait_for_completion_killable);
4816
Dave Chinnerbe4de352008-08-15 00:40:44 -07004817/**
4818 * try_wait_for_completion - try to decrement a completion without blocking
4819 * @x: completion structure
4820 *
4821 * Returns: 0 if a decrement cannot be done without blocking
4822 * 1 if a decrement succeeded.
4823 *
4824 * If a completion is being used as a counting completion,
4825 * attempt to decrement the counter without blocking. This
4826 * enables us to avoid waiting if the resource the completion
4827 * is protecting is not available.
4828 */
4829bool try_wait_for_completion(struct completion *x)
4830{
4831 int ret = 1;
4832
4833 spin_lock_irq(&x->wait.lock);
4834 if (!x->done)
4835 ret = 0;
4836 else
4837 x->done--;
4838 spin_unlock_irq(&x->wait.lock);
4839 return ret;
4840}
4841EXPORT_SYMBOL(try_wait_for_completion);
4842
4843/**
4844 * completion_done - Test to see if a completion has any waiters
4845 * @x: completion structure
4846 *
4847 * Returns: 0 if there are waiters (wait_for_completion() in progress)
4848 * 1 if there are no waiters.
4849 *
4850 */
4851bool completion_done(struct completion *x)
4852{
4853 int ret = 1;
4854
4855 spin_lock_irq(&x->wait.lock);
4856 if (!x->done)
4857 ret = 0;
4858 spin_unlock_irq(&x->wait.lock);
4859 return ret;
4860}
4861EXPORT_SYMBOL(completion_done);
4862
Andi Kleen8cbbe862007-10-15 17:00:14 +02004863static long __sched
4864sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004865{
4866 unsigned long flags;
4867 wait_queue_t wait;
4868
4869 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870
Andi Kleen8cbbe862007-10-15 17:00:14 +02004871 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Andi Kleen8cbbe862007-10-15 17:00:14 +02004873 spin_lock_irqsave(&q->lock, flags);
4874 __add_wait_queue(q, &wait);
4875 spin_unlock(&q->lock);
4876 timeout = schedule_timeout(timeout);
4877 spin_lock_irq(&q->lock);
4878 __remove_wait_queue(q, &wait);
4879 spin_unlock_irqrestore(&q->lock, flags);
4880
4881 return timeout;
4882}
4883
4884void __sched interruptible_sleep_on(wait_queue_head_t *q)
4885{
4886 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888EXPORT_SYMBOL(interruptible_sleep_on);
4889
Ingo Molnar0fec1712007-07-09 18:52:01 +02004890long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004891interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004893 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4896
Ingo Molnar0fec1712007-07-09 18:52:01 +02004897void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004899 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901EXPORT_SYMBOL(sleep_on);
4902
Ingo Molnar0fec1712007-07-09 18:52:01 +02004903long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004905 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907EXPORT_SYMBOL(sleep_on_timeout);
4908
Ingo Molnarb29739f2006-06-27 02:54:51 -07004909#ifdef CONFIG_RT_MUTEXES
4910
4911/*
4912 * rt_mutex_setprio - set the current priority of a task
4913 * @p: task
4914 * @prio: prio value (kernel-internal form)
4915 *
4916 * This function changes the 'effective' priority of a task. It does
4917 * not touch ->normal_prio like __setscheduler().
4918 *
4919 * Used by the rt_mutex code to implement priority inheritance logic.
4920 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004921void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004922{
4923 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004924 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004925 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004926 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004927
4928 BUG_ON(prio < 0 || prio > MAX_PRIO);
4929
4930 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004931 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004932
Andrew Mortond5f9f942007-05-08 20:27:06 -07004933 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004934 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004935 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004936 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004937 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004938 if (running)
4939 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004940
4941 if (rt_prio(prio))
4942 p->sched_class = &rt_sched_class;
4943 else
4944 p->sched_class = &fair_sched_class;
4945
Ingo Molnarb29739f2006-06-27 02:54:51 -07004946 p->prio = prio;
4947
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004948 if (running)
4949 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004950 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004951 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004952
4953 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004954 }
4955 task_rq_unlock(rq, &flags);
4956}
4957
4958#endif
4959
Ingo Molnar36c8b582006-07-03 00:25:41 -07004960void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961{
Ingo Molnardd41f592007-07-09 18:51:59 +02004962 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004964 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
4966 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4967 return;
4968 /*
4969 * We have to be careful, if called from sys_setpriority(),
4970 * the task might be in the middle of scheduling on another CPU.
4971 */
4972 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004973 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 /*
4975 * The RT priorities are set via sched_setscheduler(), but we still
4976 * allow the 'normal' nice value to be set - but as expected
4977 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004978 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004980 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 p->static_prio = NICE_TO_PRIO(nice);
4982 goto out_unlock;
4983 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004984 on_rq = p->se.on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004985 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004986 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004989 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004990 old_prio = p->prio;
4991 p->prio = effective_prio(p);
4992 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Ingo Molnardd41f592007-07-09 18:51:59 +02004994 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004995 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004997 * If the task increased its priority or is running and
4998 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005000 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 resched_task(rq->curr);
5002 }
5003out_unlock:
5004 task_rq_unlock(rq, &flags);
5005}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006EXPORT_SYMBOL(set_user_nice);
5007
Matt Mackalle43379f2005-05-01 08:59:00 -07005008/*
5009 * can_nice - check if a task can reduce its nice value
5010 * @p: task
5011 * @nice: nice value
5012 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005013int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005014{
Matt Mackall024f4742005-08-18 11:24:19 -07005015 /* convert nice value [19,-20] to rlimit style value [1,40] */
5016 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005017
Matt Mackalle43379f2005-05-01 08:59:00 -07005018 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5019 capable(CAP_SYS_NICE));
5020}
5021
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022#ifdef __ARCH_WANT_SYS_NICE
5023
5024/*
5025 * sys_nice - change the priority of the current process.
5026 * @increment: priority increment
5027 *
5028 * sys_setpriority is a more generic, but much slower function that
5029 * does similar things.
5030 */
5031asmlinkage long sys_nice(int increment)
5032{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005033 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
5035 /*
5036 * Setpriority might change our priority at the same moment.
5037 * We don't have to worry. Conceptually one call occurs first
5038 * and we have a single winner.
5039 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005040 if (increment < -40)
5041 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 if (increment > 40)
5043 increment = 40;
5044
5045 nice = PRIO_TO_NICE(current->static_prio) + increment;
5046 if (nice < -20)
5047 nice = -20;
5048 if (nice > 19)
5049 nice = 19;
5050
Matt Mackalle43379f2005-05-01 08:59:00 -07005051 if (increment < 0 && !can_nice(current, nice))
5052 return -EPERM;
5053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 retval = security_task_setnice(current, nice);
5055 if (retval)
5056 return retval;
5057
5058 set_user_nice(current, nice);
5059 return 0;
5060}
5061
5062#endif
5063
5064/**
5065 * task_prio - return the priority value of a given task.
5066 * @p: the task in question.
5067 *
5068 * This is the priority value as seen by users in /proc.
5069 * RT tasks are offset by -200. Normal tasks are centered
5070 * around 0, value goes from -16 to +15.
5071 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005072int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073{
5074 return p->prio - MAX_RT_PRIO;
5075}
5076
5077/**
5078 * task_nice - return the nice value of a given task.
5079 * @p: the task in question.
5080 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005081int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082{
5083 return TASK_NICE(p);
5084}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005085EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
5087/**
5088 * idle_cpu - is a given cpu idle currently?
5089 * @cpu: the processor in question.
5090 */
5091int idle_cpu(int cpu)
5092{
5093 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5094}
5095
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096/**
5097 * idle_task - return the idle task for a given cpu.
5098 * @cpu: the processor in question.
5099 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005100struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101{
5102 return cpu_rq(cpu)->idle;
5103}
5104
5105/**
5106 * find_process_by_pid - find a process with a matching PID value.
5107 * @pid: the pid in question.
5108 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005109static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005111 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112}
5113
5114/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005115static void
5116__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117{
Ingo Molnardd41f592007-07-09 18:51:59 +02005118 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005119
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005121 switch (p->policy) {
5122 case SCHED_NORMAL:
5123 case SCHED_BATCH:
5124 case SCHED_IDLE:
5125 p->sched_class = &fair_sched_class;
5126 break;
5127 case SCHED_FIFO:
5128 case SCHED_RR:
5129 p->sched_class = &rt_sched_class;
5130 break;
5131 }
5132
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005134 p->normal_prio = normal_prio(p);
5135 /* we are holding p->pi_lock already */
5136 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005137 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138}
5139
Rusty Russell961ccdd2008-06-23 13:55:38 +10005140static int __sched_setscheduler(struct task_struct *p, int policy,
5141 struct sched_param *param, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005143 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005145 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005146 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Steven Rostedt66e53932006-06-27 02:54:44 -07005148 /* may grab non-irq protected spin_locks */
5149 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150recheck:
5151 /* double check policy once rq lock held */
5152 if (policy < 0)
5153 policy = oldpolicy = p->policy;
5154 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005155 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5156 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005157 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 /*
5159 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005160 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5161 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 */
5163 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005164 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005165 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005167 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 return -EINVAL;
5169
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005170 /*
5171 * Allow unprivileged RT tasks to decrease priority:
5172 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10005173 if (user && !capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005174 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005175 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005176
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005177 if (!lock_task_sighand(p, &flags))
5178 return -ESRCH;
5179 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5180 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005181
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005182 /* can't set/change the rt policy */
5183 if (policy != p->policy && !rlim_rtprio)
5184 return -EPERM;
5185
5186 /* can't increase priority */
5187 if (param->sched_priority > p->rt_priority &&
5188 param->sched_priority > rlim_rtprio)
5189 return -EPERM;
5190 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005191 /*
5192 * Like positive nice levels, dont allow tasks to
5193 * move out of SCHED_IDLE either:
5194 */
5195 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5196 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005197
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005198 /* can't change other user's priorities */
5199 if ((current->euid != p->euid) &&
5200 (current->euid != p->uid))
5201 return -EPERM;
5202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005204 if (user) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005205#ifdef CONFIG_RT_GROUP_SCHED
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005206 /*
5207 * Do not allow realtime tasks into groups that have no runtime
5208 * assigned.
5209 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02005210 if (rt_bandwidth_enabled() && rt_policy(policy) &&
5211 task_group(p)->rt_bandwidth.rt_runtime == 0)
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005212 return -EPERM;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005213#endif
5214
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005215 retval = security_task_setscheduler(p, policy, param);
5216 if (retval)
5217 return retval;
5218 }
5219
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005221 * make sure no PI-waiters arrive (or leave) while we are
5222 * changing the priority of the task:
5223 */
5224 spin_lock_irqsave(&p->pi_lock, flags);
5225 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 * To be able to change p->policy safely, the apropriate
5227 * runqueue lock must be held.
5228 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005229 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 /* recheck policy now with rq lock held */
5231 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5232 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005233 __task_rq_unlock(rq);
5234 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 goto recheck;
5236 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005237 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005238 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005239 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005240 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005241 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005242 if (running)
5243 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005244
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005246 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005247
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005248 if (running)
5249 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005250 if (on_rq) {
5251 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005252
5253 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005255 __task_rq_unlock(rq);
5256 spin_unlock_irqrestore(&p->pi_lock, flags);
5257
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005258 rt_mutex_adjust_pi(p);
5259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 return 0;
5261}
Rusty Russell961ccdd2008-06-23 13:55:38 +10005262
5263/**
5264 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
5265 * @p: the task in question.
5266 * @policy: new policy.
5267 * @param: structure containing the new RT priority.
5268 *
5269 * NOTE that the task may be already dead.
5270 */
5271int sched_setscheduler(struct task_struct *p, int policy,
5272 struct sched_param *param)
5273{
5274 return __sched_setscheduler(p, policy, param, true);
5275}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276EXPORT_SYMBOL_GPL(sched_setscheduler);
5277
Rusty Russell961ccdd2008-06-23 13:55:38 +10005278/**
5279 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
5280 * @p: the task in question.
5281 * @policy: new policy.
5282 * @param: structure containing the new RT priority.
5283 *
5284 * Just like sched_setscheduler, only don't bother checking if the
5285 * current context has permission. For example, this is needed in
5286 * stop_machine(): we create temporary high priority worker threads,
5287 * but our caller might not have that capability.
5288 */
5289int sched_setscheduler_nocheck(struct task_struct *p, int policy,
5290 struct sched_param *param)
5291{
5292 return __sched_setscheduler(p, policy, param, false);
5293}
5294
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005295static int
5296do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 struct sched_param lparam;
5299 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005300 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
5302 if (!param || pid < 0)
5303 return -EINVAL;
5304 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5305 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005306
5307 rcu_read_lock();
5308 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005310 if (p != NULL)
5311 retval = sched_setscheduler(p, policy, &lparam);
5312 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005313
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 return retval;
5315}
5316
5317/**
5318 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5319 * @pid: the pid in question.
5320 * @policy: new policy.
5321 * @param: structure containing the new RT priority.
5322 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005323asmlinkage long
5324sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325{
Jason Baronc21761f2006-01-18 17:43:03 -08005326 /* negative values for policy are not valid */
5327 if (policy < 0)
5328 return -EINVAL;
5329
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 return do_sched_setscheduler(pid, policy, param);
5331}
5332
5333/**
5334 * sys_sched_setparam - set/change the RT priority of a thread
5335 * @pid: the pid in question.
5336 * @param: structure containing the new RT priority.
5337 */
5338asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5339{
5340 return do_sched_setscheduler(pid, -1, param);
5341}
5342
5343/**
5344 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5345 * @pid: the pid in question.
5346 */
5347asmlinkage long sys_sched_getscheduler(pid_t pid)
5348{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005349 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005350 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
5352 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005353 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
5355 retval = -ESRCH;
5356 read_lock(&tasklist_lock);
5357 p = find_process_by_pid(pid);
5358 if (p) {
5359 retval = security_task_getscheduler(p);
5360 if (!retval)
5361 retval = p->policy;
5362 }
5363 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 return retval;
5365}
5366
5367/**
5368 * sys_sched_getscheduler - get the RT priority of a thread
5369 * @pid: the pid in question.
5370 * @param: structure containing the RT priority.
5371 */
5372asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5373{
5374 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005375 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005376 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
5378 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
5381 read_lock(&tasklist_lock);
5382 p = find_process_by_pid(pid);
5383 retval = -ESRCH;
5384 if (!p)
5385 goto out_unlock;
5386
5387 retval = security_task_getscheduler(p);
5388 if (retval)
5389 goto out_unlock;
5390
5391 lp.sched_priority = p->rt_priority;
5392 read_unlock(&tasklist_lock);
5393
5394 /*
5395 * This one might sleep, we cannot do it with a spinlock held ...
5396 */
5397 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5398
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 return retval;
5400
5401out_unlock:
5402 read_unlock(&tasklist_lock);
5403 return retval;
5404}
5405
Mike Travisb53e9212008-04-04 18:11:08 -07005406long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005409 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005410 struct task_struct *p;
5411 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005413 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 read_lock(&tasklist_lock);
5415
5416 p = find_process_by_pid(pid);
5417 if (!p) {
5418 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005419 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 return -ESRCH;
5421 }
5422
5423 /*
5424 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005425 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 * usage count and then drop tasklist_lock.
5427 */
5428 get_task_struct(p);
5429 read_unlock(&tasklist_lock);
5430
5431 retval = -EPERM;
5432 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5433 !capable(CAP_SYS_NICE))
5434 goto out_unlock;
5435
David Quigleye7834f82006-06-23 02:03:59 -07005436 retval = security_task_setscheduler(p, 0, NULL);
5437 if (retval)
5438 goto out_unlock;
5439
Mike Travisf9a86fc2008-04-04 18:11:07 -07005440 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005442 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005443 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
Paul Menage8707d8b2007-10-18 23:40:22 -07005445 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005446 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005447 if (!cpus_subset(new_mask, cpus_allowed)) {
5448 /*
5449 * We must have raced with a concurrent cpuset
5450 * update. Just reset the cpus_allowed to the
5451 * cpuset's cpus_allowed
5452 */
5453 new_mask = cpus_allowed;
5454 goto again;
5455 }
5456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457out_unlock:
5458 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005459 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 return retval;
5461}
5462
5463static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5464 cpumask_t *new_mask)
5465{
5466 if (len < sizeof(cpumask_t)) {
5467 memset(new_mask, 0, sizeof(cpumask_t));
5468 } else if (len > sizeof(cpumask_t)) {
5469 len = sizeof(cpumask_t);
5470 }
5471 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5472}
5473
5474/**
5475 * sys_sched_setaffinity - set the cpu affinity of a process
5476 * @pid: pid of the process
5477 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5478 * @user_mask_ptr: user-space pointer to the new cpu mask
5479 */
5480asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5481 unsigned long __user *user_mask_ptr)
5482{
5483 cpumask_t new_mask;
5484 int retval;
5485
5486 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5487 if (retval)
5488 return retval;
5489
Mike Travisb53e9212008-04-04 18:11:08 -07005490 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491}
5492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493long sched_getaffinity(pid_t pid, cpumask_t *mask)
5494{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005495 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005498 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 read_lock(&tasklist_lock);
5500
5501 retval = -ESRCH;
5502 p = find_process_by_pid(pid);
5503 if (!p)
5504 goto out_unlock;
5505
David Quigleye7834f82006-06-23 02:03:59 -07005506 retval = security_task_getscheduler(p);
5507 if (retval)
5508 goto out_unlock;
5509
Jack Steiner2f7016d2006-02-01 03:05:18 -08005510 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
5512out_unlock:
5513 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005514 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
Ulrich Drepper9531b622007-08-09 11:16:46 +02005516 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517}
5518
5519/**
5520 * sys_sched_getaffinity - get the cpu affinity of a process
5521 * @pid: pid of the process
5522 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5523 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5524 */
5525asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5526 unsigned long __user *user_mask_ptr)
5527{
5528 int ret;
5529 cpumask_t mask;
5530
5531 if (len < sizeof(cpumask_t))
5532 return -EINVAL;
5533
5534 ret = sched_getaffinity(pid, &mask);
5535 if (ret < 0)
5536 return ret;
5537
5538 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5539 return -EFAULT;
5540
5541 return sizeof(cpumask_t);
5542}
5543
5544/**
5545 * sys_sched_yield - yield the current processor to other threads.
5546 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005547 * This function yields the current CPU to other tasks. If there are no
5548 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 */
5550asmlinkage long sys_sched_yield(void)
5551{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005552 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
Ingo Molnar2d723762007-10-15 17:00:12 +02005554 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005555 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
5557 /*
5558 * Since we are going to call schedule() anyway, there's
5559 * no need to preempt or enable interrupts:
5560 */
5561 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005562 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 _raw_spin_unlock(&rq->lock);
5564 preempt_enable_no_resched();
5565
5566 schedule();
5567
5568 return 0;
5569}
5570
Andrew Mortone7b38402006-06-30 01:56:00 -07005571static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005573#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5574 __might_sleep(__FILE__, __LINE__);
5575#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005576 /*
5577 * The BKS might be reacquired before we have dropped
5578 * PREEMPT_ACTIVE, which could trigger a second
5579 * cond_resched() call.
5580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 do {
5582 add_preempt_count(PREEMPT_ACTIVE);
5583 schedule();
5584 sub_preempt_count(PREEMPT_ACTIVE);
5585 } while (need_resched());
5586}
5587
Herbert Xu02b67cc2008-01-25 21:08:28 +01005588int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589{
Ingo Molnar94142322006-12-29 16:48:13 -08005590 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5591 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 __cond_resched();
5593 return 1;
5594 }
5595 return 0;
5596}
Herbert Xu02b67cc2008-01-25 21:08:28 +01005597EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
5599/*
5600 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5601 * call schedule, and on return reacquire the lock.
5602 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005603 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 * operations here to prevent schedule() from being called twice (once via
5605 * spin_unlock(), once by hand).
5606 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005607int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608{
Nick Piggin95c354f2008-01-30 13:31:20 +01005609 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005610 int ret = 0;
5611
Nick Piggin95c354f2008-01-30 13:31:20 +01005612 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005614 if (resched && need_resched())
5615 __cond_resched();
5616 else
5617 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005618 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005621 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623EXPORT_SYMBOL(cond_resched_lock);
5624
5625int __sched cond_resched_softirq(void)
5626{
5627 BUG_ON(!in_softirq());
5628
Ingo Molnar94142322006-12-29 16:48:13 -08005629 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005630 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 __cond_resched();
5632 local_bh_disable();
5633 return 1;
5634 }
5635 return 0;
5636}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637EXPORT_SYMBOL(cond_resched_softirq);
5638
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639/**
5640 * yield - yield the current processor to other threads.
5641 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005642 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 * thread runnable and calls sys_sched_yield().
5644 */
5645void __sched yield(void)
5646{
5647 set_current_state(TASK_RUNNING);
5648 sys_sched_yield();
5649}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650EXPORT_SYMBOL(yield);
5651
5652/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005653 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 * that process accounting knows that this is a task in IO wait state.
5655 *
5656 * But don't do that if it is a deliberate, throttling IO wait (this task
5657 * has set its backing_dev_info: the queue against which it should throttle)
5658 */
5659void __sched io_schedule(void)
5660{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005661 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005663 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 atomic_inc(&rq->nr_iowait);
5665 schedule();
5666 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005667 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669EXPORT_SYMBOL(io_schedule);
5670
5671long __sched io_schedule_timeout(long timeout)
5672{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005673 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 long ret;
5675
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005676 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 atomic_inc(&rq->nr_iowait);
5678 ret = schedule_timeout(timeout);
5679 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005680 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 return ret;
5682}
5683
5684/**
5685 * sys_sched_get_priority_max - return maximum RT priority.
5686 * @policy: scheduling class.
5687 *
5688 * this syscall returns the maximum rt_priority that can be used
5689 * by a given scheduling class.
5690 */
5691asmlinkage long sys_sched_get_priority_max(int policy)
5692{
5693 int ret = -EINVAL;
5694
5695 switch (policy) {
5696 case SCHED_FIFO:
5697 case SCHED_RR:
5698 ret = MAX_USER_RT_PRIO-1;
5699 break;
5700 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005701 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005702 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 ret = 0;
5704 break;
5705 }
5706 return ret;
5707}
5708
5709/**
5710 * sys_sched_get_priority_min - return minimum RT priority.
5711 * @policy: scheduling class.
5712 *
5713 * this syscall returns the minimum rt_priority that can be used
5714 * by a given scheduling class.
5715 */
5716asmlinkage long sys_sched_get_priority_min(int policy)
5717{
5718 int ret = -EINVAL;
5719
5720 switch (policy) {
5721 case SCHED_FIFO:
5722 case SCHED_RR:
5723 ret = 1;
5724 break;
5725 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005726 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005727 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 ret = 0;
5729 }
5730 return ret;
5731}
5732
5733/**
5734 * sys_sched_rr_get_interval - return the default timeslice of a process.
5735 * @pid: pid of the process.
5736 * @interval: userspace pointer to the timeslice value.
5737 *
5738 * this syscall writes the default timeslice value of a given process
5739 * into the user-space timespec buffer. A value of '0' means infinity.
5740 */
5741asmlinkage
5742long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5743{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005744 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005745 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005746 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
5749 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005750 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
5752 retval = -ESRCH;
5753 read_lock(&tasklist_lock);
5754 p = find_process_by_pid(pid);
5755 if (!p)
5756 goto out_unlock;
5757
5758 retval = security_task_getscheduler(p);
5759 if (retval)
5760 goto out_unlock;
5761
Ingo Molnar77034932007-12-04 17:04:39 +01005762 /*
5763 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5764 * tasks that are on an otherwise idle runqueue:
5765 */
5766 time_slice = 0;
5767 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005768 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005769 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005770 struct sched_entity *se = &p->se;
5771 unsigned long flags;
5772 struct rq *rq;
5773
5774 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005775 if (rq->cfs.load.weight)
5776 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005777 task_rq_unlock(rq, &flags);
5778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005780 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005783
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784out_unlock:
5785 read_unlock(&tasklist_lock);
5786 return retval;
5787}
5788
Steven Rostedt7c731e02008-05-12 21:20:41 +02005789static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005790
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005791void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005794 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005797 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005798 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005799#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005801 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005803 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804#else
5805 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005806 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005808 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809#endif
5810#ifdef CONFIG_DEBUG_STACK_USAGE
5811 {
Al Viro10ebffd2005-11-13 16:06:56 -08005812 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 while (!*n)
5814 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005815 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 }
5817#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005818 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005819 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005821 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822}
5823
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005824void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005826 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827
Ingo Molnar4bd77322007-07-11 21:21:47 +02005828#if BITS_PER_LONG == 32
5829 printk(KERN_INFO
5830 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005832 printk(KERN_INFO
5833 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834#endif
5835 read_lock(&tasklist_lock);
5836 do_each_thread(g, p) {
5837 /*
5838 * reset the NMI-timeout, listing all files on a slow
5839 * console might take alot of time:
5840 */
5841 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005842 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005843 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 } while_each_thread(g, p);
5845
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005846 touch_all_softlockup_watchdogs();
5847
Ingo Molnardd41f592007-07-09 18:51:59 +02005848#ifdef CONFIG_SCHED_DEBUG
5849 sysrq_sched_debug_show();
5850#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005852 /*
5853 * Only show locks if all tasks are dumped:
5854 */
5855 if (state_filter == -1)
5856 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857}
5858
Ingo Molnar1df21052007-07-09 18:51:58 +02005859void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5860{
Ingo Molnardd41f592007-07-09 18:51:59 +02005861 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005862}
5863
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005864/**
5865 * init_idle - set up an idle thread for a given CPU
5866 * @idle: task in question
5867 * @cpu: cpu the idle task belongs to
5868 *
5869 * NOTE: this function does not set the idle thread's NEED_RESCHED
5870 * flag, to make booting more robust.
5871 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005872void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005874 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 unsigned long flags;
5876
Ingo Molnardd41f592007-07-09 18:51:59 +02005877 __sched_fork(idle);
5878 idle->se.exec_start = sched_clock();
5879
Ingo Molnarb29739f2006-06-27 02:54:51 -07005880 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005882 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
5884 spin_lock_irqsave(&rq->lock, flags);
5885 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005886#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5887 idle->oncpu = 1;
5888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 spin_unlock_irqrestore(&rq->lock, flags);
5890
5891 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005892#if defined(CONFIG_PREEMPT)
5893 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
5894#else
Al Viroa1261f52005-11-13 16:06:55 -08005895 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005896#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005897 /*
5898 * The idle tasks have their own, simple scheduling class:
5899 */
5900 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901}
5902
5903/*
5904 * In a system that switches off the HZ timer nohz_cpu_mask
5905 * indicates which cpus entered this state. This is used
5906 * in the rcu update to wait only for active cpus. For system
5907 * which do not switch off the HZ timer nohz_cpu_mask should
5908 * always be CPU_MASK_NONE.
5909 */
5910cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5911
Ingo Molnar19978ca2007-11-09 22:39:38 +01005912/*
5913 * Increase the granularity value when there are more CPUs,
5914 * because with more CPUs the 'effective latency' as visible
5915 * to users decreases. But the relationship is not linear,
5916 * so pick a second-best guess by going with the log2 of the
5917 * number of CPUs.
5918 *
5919 * This idea comes from the SD scheduler of Con Kolivas:
5920 */
5921static inline void sched_init_granularity(void)
5922{
5923 unsigned int factor = 1 + ilog2(num_online_cpus());
5924 const unsigned long limit = 200000000;
5925
5926 sysctl_sched_min_granularity *= factor;
5927 if (sysctl_sched_min_granularity > limit)
5928 sysctl_sched_min_granularity = limit;
5929
5930 sysctl_sched_latency *= factor;
5931 if (sysctl_sched_latency > limit)
5932 sysctl_sched_latency = limit;
5933
5934 sysctl_sched_wakeup_granularity *= factor;
Peter Zijlstra55cd5342008-08-04 08:54:26 +02005935
5936 sysctl_sched_shares_ratelimit *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005937}
5938
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939#ifdef CONFIG_SMP
5940/*
5941 * This is how migration works:
5942 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005943 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 * runqueue and wake up that CPU's migration thread.
5945 * 2) we down() the locked semaphore => thread blocks.
5946 * 3) migration thread wakes up (implicitly it forces the migrated
5947 * thread off the CPU)
5948 * 4) it gets the migration request and checks whether the migrated
5949 * task is still in the wrong runqueue.
5950 * 5) if it's in the wrong runqueue then the migration thread removes
5951 * it and puts it into the right queue.
5952 * 6) migration thread up()s the semaphore.
5953 * 7) we wake up and the migration is done.
5954 */
5955
5956/*
5957 * Change a given task's CPU affinity. Migrate the thread to a
5958 * proper CPU and schedule it away if the CPU it's executing on
5959 * is removed from the allowed bitmask.
5960 *
5961 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005962 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 * call is not atomic; no spinlocks may be held.
5964 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005965int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005967 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005969 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005970 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
5972 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005973 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 ret = -EINVAL;
5975 goto out;
5976 }
5977
David Rientjes9985b0b2008-06-05 12:57:11 -07005978 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
5979 !cpus_equal(p->cpus_allowed, *new_mask))) {
5980 ret = -EINVAL;
5981 goto out;
5982 }
5983
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005984 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005985 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005986 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005987 p->cpus_allowed = *new_mask;
5988 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005989 }
5990
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005992 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 goto out;
5994
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005995 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 /* Need help from migration thread: drop lock and wait. */
5997 task_rq_unlock(rq, &flags);
5998 wake_up_process(rq->migration_thread);
5999 wait_for_completion(&req.done);
6000 tlb_migrate_finish(p->mm);
6001 return 0;
6002 }
6003out:
6004 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 return ret;
6007}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006008EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
6010/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006011 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 * this because either it can't run here any more (set_cpus_allowed()
6013 * away from this CPU, or CPU going down), or because we're
6014 * attempting to rebalance this task on exec (sched_exec).
6015 *
6016 * So we race with normal scheduler movements, but that's OK, as long
6017 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006018 *
6019 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006021static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006023 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006024 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025
Max Krasnyanskye761b772008-07-15 04:43:49 -07006026 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006027 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
6029 rq_src = cpu_rq(src_cpu);
6030 rq_dest = cpu_rq(dest_cpu);
6031
6032 double_rq_lock(rq_src, rq_dest);
6033 /* Already moved. */
6034 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006035 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 /* Affinity changed (again). */
6037 if (!cpu_isset(dest_cpu, p->cpus_allowed))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006038 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
Ingo Molnardd41f592007-07-09 18:51:59 +02006040 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006041 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006042 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006043
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006045 if (on_rq) {
6046 activate_task(rq_dest, p, 0);
Peter Zijlstra15afe092008-09-20 23:38:02 +02006047 check_preempt_curr(rq_dest, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 }
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006049done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07006050 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006051fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006053 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054}
6055
6056/*
6057 * migration_thread - this is a highprio system thread that performs
6058 * thread migration by bumping thread off CPU then 'pushing' onto
6059 * another runqueue.
6060 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006061static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006064 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065
6066 rq = cpu_rq(cpu);
6067 BUG_ON(rq->migration_thread != current);
6068
6069 set_current_state(TASK_INTERRUPTIBLE);
6070 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006071 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 spin_lock_irq(&rq->lock);
6075
6076 if (cpu_is_offline(cpu)) {
6077 spin_unlock_irq(&rq->lock);
6078 goto wait_to_die;
6079 }
6080
6081 if (rq->active_balance) {
6082 active_load_balance(rq, cpu);
6083 rq->active_balance = 0;
6084 }
6085
6086 head = &rq->migration_queue;
6087
6088 if (list_empty(head)) {
6089 spin_unlock_irq(&rq->lock);
6090 schedule();
6091 set_current_state(TASK_INTERRUPTIBLE);
6092 continue;
6093 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006094 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 list_del_init(head->next);
6096
Nick Piggin674311d2005-06-25 14:57:27 -07006097 spin_unlock(&rq->lock);
6098 __migrate_task(req->task, cpu, req->dest_cpu);
6099 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
6101 complete(&req->done);
6102 }
6103 __set_current_state(TASK_RUNNING);
6104 return 0;
6105
6106wait_to_die:
6107 /* Wait for kthread_stop */
6108 set_current_state(TASK_INTERRUPTIBLE);
6109 while (!kthread_should_stop()) {
6110 schedule();
6111 set_current_state(TASK_INTERRUPTIBLE);
6112 }
6113 __set_current_state(TASK_RUNNING);
6114 return 0;
6115}
6116
6117#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006118
6119static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6120{
6121 int ret;
6122
6123 local_irq_disable();
6124 ret = __migrate_task(p, src_cpu, dest_cpu);
6125 local_irq_enable();
6126 return ret;
6127}
6128
Kirill Korotaev054b9102006-12-10 02:20:11 -08006129/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006130 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006131 * NOTE: interrupts should be disabled by the caller
6132 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006133static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006135 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006137 struct rq *rq;
6138 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
Andi Kleen3a5c3592007-10-15 17:00:14 +02006140 do {
6141 /* On same node? */
6142 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6143 cpus_and(mask, mask, p->cpus_allowed);
6144 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
Andi Kleen3a5c3592007-10-15 17:00:14 +02006146 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006147 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006148 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149
Andi Kleen3a5c3592007-10-15 17:00:14 +02006150 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006151 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006152 cpumask_t cpus_allowed;
6153
6154 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006155 /*
6156 * Try to stay on the same cpuset, where the
6157 * current cpuset may be a subset of all cpus.
6158 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006159 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006160 * called within calls to cpuset_lock/cpuset_unlock.
6161 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006162 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006163 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006164 dest_cpu = any_online_cpu(p->cpus_allowed);
6165 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
Andi Kleen3a5c3592007-10-15 17:00:14 +02006167 /*
6168 * Don't tell them about moving exiting tasks or
6169 * kernel threads (both mm NULL), since they never
6170 * leave kernel.
6171 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006172 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006173 printk(KERN_INFO "process %d (%s) no "
6174 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006175 task_pid_nr(p), p->comm, dead_cpu);
6176 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006177 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006178 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179}
6180
6181/*
6182 * While a dead CPU has no uninterruptible tasks queued at this point,
6183 * it might still have a nonzero ->nr_uninterruptible counter, because
6184 * for performance reasons the counter is not stricly tracking tasks to
6185 * their home CPUs. So we just add the counter to another CPU's counter,
6186 * to keep the global sum constant after CPU-down:
6187 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006188static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189{
Mike Travis7c16ec52008-04-04 18:11:11 -07006190 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 unsigned long flags;
6192
6193 local_irq_save(flags);
6194 double_rq_lock(rq_src, rq_dest);
6195 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6196 rq_src->nr_uninterruptible = 0;
6197 double_rq_unlock(rq_src, rq_dest);
6198 local_irq_restore(flags);
6199}
6200
6201/* Run through task list and migrate tasks from the dead cpu. */
6202static void migrate_live_tasks(int src_cpu)
6203{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006204 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006206 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
Ingo Molnar48f24c42006-07-03 00:25:40 -07006208 do_each_thread(t, p) {
6209 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 continue;
6211
Ingo Molnar48f24c42006-07-03 00:25:40 -07006212 if (task_cpu(p) == src_cpu)
6213 move_task_off_dead_cpu(src_cpu, p);
6214 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006216 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217}
6218
Ingo Molnardd41f592007-07-09 18:51:59 +02006219/*
6220 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006221 * It does so by boosting its priority to highest possible.
6222 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 */
6224void sched_idle_next(void)
6225{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006226 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006227 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 struct task_struct *p = rq->idle;
6229 unsigned long flags;
6230
6231 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006232 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
Ingo Molnar48f24c42006-07-03 00:25:40 -07006234 /*
6235 * Strictly not necessary since rest of the CPUs are stopped by now
6236 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 */
6238 spin_lock_irqsave(&rq->lock, flags);
6239
Ingo Molnardd41f592007-07-09 18:51:59 +02006240 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006241
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006242 update_rq_clock(rq);
6243 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
6245 spin_unlock_irqrestore(&rq->lock, flags);
6246}
6247
Ingo Molnar48f24c42006-07-03 00:25:40 -07006248/*
6249 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 * offline.
6251 */
6252void idle_task_exit(void)
6253{
6254 struct mm_struct *mm = current->active_mm;
6255
6256 BUG_ON(cpu_online(smp_processor_id()));
6257
6258 if (mm != &init_mm)
6259 switch_mm(mm, &init_mm, current);
6260 mmdrop(mm);
6261}
6262
Kirill Korotaev054b9102006-12-10 02:20:11 -08006263/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006264static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006266 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267
6268 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006269 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270
6271 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006272 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273
Ingo Molnar48f24c42006-07-03 00:25:40 -07006274 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275
6276 /*
6277 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006278 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 * fine.
6280 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006281 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006282 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006283 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284
Ingo Molnar48f24c42006-07-03 00:25:40 -07006285 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286}
6287
6288/* release_task() removes task from tasklist, so we won't find dead tasks. */
6289static void migrate_dead_tasks(unsigned int dead_cpu)
6290{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006291 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006292 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293
Ingo Molnardd41f592007-07-09 18:51:59 +02006294 for ( ; ; ) {
6295 if (!rq->nr_running)
6296 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006297 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006298 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006299 if (!next)
6300 break;
Dmitry Adamushko79c53792008-06-29 00:16:56 +02006301 next->sched_class->put_prev_task(rq, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02006302 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006303
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 }
6305}
6306#endif /* CONFIG_HOTPLUG_CPU */
6307
Nick Piggine692ab52007-07-26 13:40:43 +02006308#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6309
6310static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006311 {
6312 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006313 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006314 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006315 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006316};
6317
6318static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006319 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006320 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006321 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006322 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006323 .child = sd_ctl_dir,
6324 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006325 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006326};
6327
6328static struct ctl_table *sd_alloc_ctl_entry(int n)
6329{
6330 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006331 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006332
Nick Piggine692ab52007-07-26 13:40:43 +02006333 return entry;
6334}
6335
Milton Miller6382bc92007-10-15 17:00:19 +02006336static void sd_free_ctl_entry(struct ctl_table **tablep)
6337{
Milton Millercd790072007-10-17 16:55:11 +02006338 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006339
Milton Millercd790072007-10-17 16:55:11 +02006340 /*
6341 * In the intermediate directories, both the child directory and
6342 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006343 * will always be set. In the lowest directory the names are
Milton Millercd790072007-10-17 16:55:11 +02006344 * static strings and all have proc handlers.
6345 */
6346 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006347 if (entry->child)
6348 sd_free_ctl_entry(&entry->child);
Milton Millercd790072007-10-17 16:55:11 +02006349 if (entry->proc_handler == NULL)
6350 kfree(entry->procname);
6351 }
Milton Miller6382bc92007-10-15 17:00:19 +02006352
6353 kfree(*tablep);
6354 *tablep = NULL;
6355}
6356
Nick Piggine692ab52007-07-26 13:40:43 +02006357static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006358set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006359 const char *procname, void *data, int maxlen,
6360 mode_t mode, proc_handler *proc_handler)
6361{
Nick Piggine692ab52007-07-26 13:40:43 +02006362 entry->procname = procname;
6363 entry->data = data;
6364 entry->maxlen = maxlen;
6365 entry->mode = mode;
6366 entry->proc_handler = proc_handler;
6367}
6368
6369static struct ctl_table *
6370sd_alloc_ctl_domain_table(struct sched_domain *sd)
6371{
Ingo Molnara5d8c342008-10-09 11:35:51 +02006372 struct ctl_table *table = sd_alloc_ctl_entry(13);
Nick Piggine692ab52007-07-26 13:40:43 +02006373
Milton Millerad1cdc12007-10-15 17:00:19 +02006374 if (table == NULL)
6375 return NULL;
6376
Alexey Dobriyane0361852007-08-09 11:16:46 +02006377 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006378 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006379 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006380 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006381 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006382 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006383 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006384 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006385 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006386 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006387 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006388 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006389 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006390 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006391 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006392 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006393 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006394 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006395 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006396 &sd->cache_nice_tries,
6397 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006398 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006399 sizeof(int), 0644, proc_dointvec_minmax);
Ingo Molnara5d8c342008-10-09 11:35:51 +02006400 set_table_entry(&table[11], "name", sd->name,
6401 CORENAME_MAX_SIZE, 0444, proc_dostring);
6402 /* &table[12] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006403
6404 return table;
6405}
6406
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006407static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006408{
6409 struct ctl_table *entry, *table;
6410 struct sched_domain *sd;
6411 int domain_num = 0, i;
6412 char buf[32];
6413
6414 for_each_domain(cpu, sd)
6415 domain_num++;
6416 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006417 if (table == NULL)
6418 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006419
6420 i = 0;
6421 for_each_domain(cpu, sd) {
6422 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006423 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006424 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006425 entry->child = sd_alloc_ctl_domain_table(sd);
6426 entry++;
6427 i++;
6428 }
6429 return table;
6430}
6431
6432static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006433static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006434{
6435 int i, cpu_num = num_online_cpus();
6436 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6437 char buf[32];
6438
Milton Miller73785472007-10-24 18:23:48 +02006439 WARN_ON(sd_ctl_dir[0].child);
6440 sd_ctl_dir[0].child = entry;
6441
Milton Millerad1cdc12007-10-15 17:00:19 +02006442 if (entry == NULL)
6443 return;
6444
Milton Miller97b6ea72007-10-15 17:00:19 +02006445 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006446 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006447 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006448 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006449 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006450 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006451 }
Milton Miller73785472007-10-24 18:23:48 +02006452
6453 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006454 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6455}
Milton Miller6382bc92007-10-15 17:00:19 +02006456
Milton Miller73785472007-10-24 18:23:48 +02006457/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006458static void unregister_sched_domain_sysctl(void)
6459{
Milton Miller73785472007-10-24 18:23:48 +02006460 if (sd_sysctl_header)
6461 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006462 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006463 if (sd_ctl_dir[0].child)
6464 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006465}
Nick Piggine692ab52007-07-26 13:40:43 +02006466#else
Milton Miller6382bc92007-10-15 17:00:19 +02006467static void register_sched_domain_sysctl(void)
6468{
6469}
6470static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006471{
6472}
6473#endif
6474
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006475static void set_rq_online(struct rq *rq)
6476{
6477 if (!rq->online) {
6478 const struct sched_class *class;
6479
6480 cpu_set(rq->cpu, rq->rd->online);
6481 rq->online = 1;
6482
6483 for_each_class(class) {
6484 if (class->rq_online)
6485 class->rq_online(rq);
6486 }
6487 }
6488}
6489
6490static void set_rq_offline(struct rq *rq)
6491{
6492 if (rq->online) {
6493 const struct sched_class *class;
6494
6495 for_each_class(class) {
6496 if (class->rq_offline)
6497 class->rq_offline(rq);
6498 }
6499
6500 cpu_clear(rq->cpu, rq->rd->online);
6501 rq->online = 0;
6502 }
6503}
6504
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505/*
6506 * migration_call - callback that gets triggered when a CPU is added.
6507 * Here we can start up the necessary migration thread for the new CPU.
6508 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006509static int __cpuinit
6510migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006513 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006515 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516
6517 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006518
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006520 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006521 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 if (IS_ERR(p))
6523 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 kthread_bind(p, cpu);
6525 /* Must be high prio: stop_machine expects to yield to it. */
6526 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006527 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 task_rq_unlock(rq, &flags);
6529 cpu_rq(cpu)->migration_thread = p;
6530 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006531
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006533 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006534 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006536
6537 /* Update our root-domain */
6538 rq = cpu_rq(cpu);
6539 spin_lock_irqsave(&rq->lock, flags);
6540 if (rq->rd) {
6541 BUG_ON(!cpu_isset(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006542
6543 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006544 }
6545 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006547
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548#ifdef CONFIG_HOTPLUG_CPU
6549 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006550 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006551 if (!cpu_rq(cpu)->migration_thread)
6552 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006553 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006554 kthread_bind(cpu_rq(cpu)->migration_thread,
6555 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 kthread_stop(cpu_rq(cpu)->migration_thread);
6557 cpu_rq(cpu)->migration_thread = NULL;
6558 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006559
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006561 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006562 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 migrate_live_tasks(cpu);
6564 rq = cpu_rq(cpu);
6565 kthread_stop(rq->migration_thread);
6566 rq->migration_thread = NULL;
6567 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006568 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006569 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006570 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006572 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6573 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006575 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006576 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 migrate_nr_uninterruptible(rq);
6578 BUG_ON(rq->nr_running != 0);
6579
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006580 /*
6581 * No need to migrate the tasks: it was best-effort if
6582 * they didn't take sched_hotcpu_mutex. Just wake up
6583 * the requestors.
6584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 spin_lock_irq(&rq->lock);
6586 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006587 struct migration_req *req;
6588
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006590 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 list_del_init(&req->list);
6592 complete(&req->done);
6593 }
6594 spin_unlock_irq(&rq->lock);
6595 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006596
Gregory Haskins08f503b2008-03-10 17:59:11 -04006597 case CPU_DYING:
6598 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006599 /* Update our root-domain */
6600 rq = cpu_rq(cpu);
6601 spin_lock_irqsave(&rq->lock, flags);
6602 if (rq->rd) {
6603 BUG_ON(!cpu_isset(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006604 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006605 }
6606 spin_unlock_irqrestore(&rq->lock, flags);
6607 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608#endif
6609 }
6610 return NOTIFY_OK;
6611}
6612
6613/* Register at highest priority so that task migration (migrate_all_tasks)
6614 * happens before everything else.
6615 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006616static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 .notifier_call = migration_call,
6618 .priority = 10
6619};
6620
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006621static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622{
6623 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006624 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006625
6626 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006627 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6628 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6630 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006631
6632 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006634early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635#endif
6636
6637#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006638
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006639#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006640
Gautham R Shenoy099f98c2008-05-29 20:56:32 +05306641static inline const char *sd_level_to_string(enum sched_domain_level lvl)
6642{
6643 switch (lvl) {
6644 case SD_LV_NONE:
6645 return "NONE";
6646 case SD_LV_SIBLING:
6647 return "SIBLING";
6648 case SD_LV_MC:
6649 return "MC";
6650 case SD_LV_CPU:
6651 return "CPU";
6652 case SD_LV_NODE:
6653 return "NODE";
6654 case SD_LV_ALLNODES:
6655 return "ALLNODES";
6656 case SD_LV_MAX:
6657 return "MAX";
6658
6659 }
6660 return "MAX";
6661}
6662
Mike Travis7c16ec52008-04-04 18:11:11 -07006663static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6664 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006665{
6666 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006667 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006668
Mike Travis434d53b2008-04-04 18:11:04 -07006669 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006670 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006671
6672 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6673
6674 if (!(sd->flags & SD_LOAD_BALANCE)) {
6675 printk("does not load-balance\n");
6676 if (sd->parent)
6677 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6678 " has parent");
6679 return -1;
6680 }
6681
Gautham R Shenoy099f98c2008-05-29 20:56:32 +05306682 printk(KERN_CONT "span %s level %s\n",
6683 str, sd_level_to_string(sd->level));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006684
6685 if (!cpu_isset(cpu, sd->span)) {
6686 printk(KERN_ERR "ERROR: domain->span does not contain "
6687 "CPU%d\n", cpu);
6688 }
6689 if (!cpu_isset(cpu, group->cpumask)) {
6690 printk(KERN_ERR "ERROR: domain->groups does not contain"
6691 " CPU%d\n", cpu);
6692 }
6693
6694 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6695 do {
6696 if (!group) {
6697 printk("\n");
6698 printk(KERN_ERR "ERROR: group is NULL\n");
6699 break;
6700 }
6701
6702 if (!group->__cpu_power) {
6703 printk(KERN_CONT "\n");
6704 printk(KERN_ERR "ERROR: domain->cpu_power not "
6705 "set\n");
6706 break;
6707 }
6708
6709 if (!cpus_weight(group->cpumask)) {
6710 printk(KERN_CONT "\n");
6711 printk(KERN_ERR "ERROR: empty group\n");
6712 break;
6713 }
6714
Mike Travis7c16ec52008-04-04 18:11:11 -07006715 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006716 printk(KERN_CONT "\n");
6717 printk(KERN_ERR "ERROR: repeated CPUs\n");
6718 break;
6719 }
6720
Mike Travis7c16ec52008-04-04 18:11:11 -07006721 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006722
Mike Travis434d53b2008-04-04 18:11:04 -07006723 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006724 printk(KERN_CONT " %s", str);
6725
6726 group = group->next;
6727 } while (group != sd->groups);
6728 printk(KERN_CONT "\n");
6729
Mike Travis7c16ec52008-04-04 18:11:11 -07006730 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006731 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6732
Mike Travis7c16ec52008-04-04 18:11:11 -07006733 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006734 printk(KERN_ERR "ERROR: parent span is not a superset "
6735 "of domain->span\n");
6736 return 0;
6737}
6738
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739static void sched_domain_debug(struct sched_domain *sd, int cpu)
6740{
Mike Travis7c16ec52008-04-04 18:11:11 -07006741 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 int level = 0;
6743
Nick Piggin41c7ce92005-06-25 14:57:24 -07006744 if (!sd) {
6745 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6746 return;
6747 }
6748
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6750
Mike Travis7c16ec52008-04-04 18:11:11 -07006751 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6752 if (!groupmask) {
6753 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6754 return;
6755 }
6756
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006757 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006758 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 level++;
6761 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006762 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006763 break;
6764 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006765 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006767#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006768# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006769#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006771static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006772{
6773 if (cpus_weight(sd->span) == 1)
6774 return 1;
6775
6776 /* Following flags need at least 2 groups */
6777 if (sd->flags & (SD_LOAD_BALANCE |
6778 SD_BALANCE_NEWIDLE |
6779 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006780 SD_BALANCE_EXEC |
6781 SD_SHARE_CPUPOWER |
6782 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006783 if (sd->groups != sd->groups->next)
6784 return 0;
6785 }
6786
6787 /* Following flags don't use groups */
6788 if (sd->flags & (SD_WAKE_IDLE |
6789 SD_WAKE_AFFINE |
6790 SD_WAKE_BALANCE))
6791 return 0;
6792
6793 return 1;
6794}
6795
Ingo Molnar48f24c42006-07-03 00:25:40 -07006796static int
6797sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006798{
6799 unsigned long cflags = sd->flags, pflags = parent->flags;
6800
6801 if (sd_degenerate(parent))
6802 return 1;
6803
6804 if (!cpus_equal(sd->span, parent->span))
6805 return 0;
6806
6807 /* Does parent contain flags not in child? */
6808 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6809 if (cflags & SD_WAKE_AFFINE)
6810 pflags &= ~SD_WAKE_BALANCE;
6811 /* Flags needing groups don't count if only 1 group in parent */
6812 if (parent->groups == parent->groups->next) {
6813 pflags &= ~(SD_LOAD_BALANCE |
6814 SD_BALANCE_NEWIDLE |
6815 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006816 SD_BALANCE_EXEC |
6817 SD_SHARE_CPUPOWER |
6818 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006819 }
6820 if (~cflags & pflags)
6821 return 0;
6822
6823 return 1;
6824}
6825
Gregory Haskins57d885f2008-01-25 21:08:18 +01006826static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6827{
6828 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006829
6830 spin_lock_irqsave(&rq->lock, flags);
6831
6832 if (rq->rd) {
6833 struct root_domain *old_rd = rq->rd;
6834
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006835 if (cpu_isset(rq->cpu, old_rd->online))
6836 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006837
Gregory Haskinsdc938522008-01-25 21:08:26 +01006838 cpu_clear(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006839
Gregory Haskins57d885f2008-01-25 21:08:18 +01006840 if (atomic_dec_and_test(&old_rd->refcount))
6841 kfree(old_rd);
6842 }
6843
6844 atomic_inc(&rd->refcount);
6845 rq->rd = rd;
6846
Gregory Haskinsdc938522008-01-25 21:08:26 +01006847 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006848 if (cpu_isset(rq->cpu, cpu_online_map))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006849 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006850
6851 spin_unlock_irqrestore(&rq->lock, flags);
6852}
6853
Gregory Haskinsdc938522008-01-25 21:08:26 +01006854static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006855{
6856 memset(rd, 0, sizeof(*rd));
6857
Gregory Haskinsdc938522008-01-25 21:08:26 +01006858 cpus_clear(rd->span);
6859 cpus_clear(rd->online);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006860
6861 cpupri_init(&rd->cpupri);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006862}
6863
6864static void init_defrootdomain(void)
6865{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006866 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006867 atomic_set(&def_root_domain.refcount, 1);
6868}
6869
Gregory Haskinsdc938522008-01-25 21:08:26 +01006870static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006871{
6872 struct root_domain *rd;
6873
6874 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6875 if (!rd)
6876 return NULL;
6877
Gregory Haskinsdc938522008-01-25 21:08:26 +01006878 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006879
6880 return rd;
6881}
6882
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006884 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 * hold the hotplug lock.
6886 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006887static void
6888cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006890 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006891 struct sched_domain *tmp;
6892
6893 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08006894 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006895 struct sched_domain *parent = tmp->parent;
6896 if (!parent)
6897 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08006898
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006899 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006900 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006901 if (parent->parent)
6902 parent->parent->child = tmp;
Li Zefanf29c9b12008-11-06 09:45:16 +08006903 } else
6904 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006905 }
6906
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006907 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006908 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006909 if (sd)
6910 sd->child = NULL;
6911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912
6913 sched_domain_debug(sd, cpu);
6914
Gregory Haskins57d885f2008-01-25 21:08:18 +01006915 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006916 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917}
6918
6919/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006920static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921
6922/* Setup the mask of cpus configured for isolated domains */
6923static int __init isolated_cpu_setup(char *str)
6924{
Mike Travis13b40c12008-07-01 10:32:50 -07006925 static int __initdata ints[NR_CPUS];
6926 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927
6928 str = get_options(str, ARRAY_SIZE(ints), ints);
6929 cpus_clear(cpu_isolated_map);
6930 for (i = 1; i <= ints[0]; i++)
6931 if (ints[i] < NR_CPUS)
6932 cpu_set(ints[i], cpu_isolated_map);
6933 return 1;
6934}
6935
Ingo Molnar8927f492007-10-15 17:00:13 +02006936__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
6938/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006939 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6940 * to a function which identifies what group(along with sched group) a CPU
6941 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6942 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 *
6944 * init_sched_build_groups will build a circular linked list of the groups
6945 * covered by the given span, and will set each group's ->cpumask correctly,
6946 * and ->cpu_power to 0.
6947 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006948static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006949init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006950 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006951 struct sched_group **sg,
6952 cpumask_t *tmpmask),
6953 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954{
6955 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 int i;
6957
Mike Travis7c16ec52008-04-04 18:11:11 -07006958 cpus_clear(*covered);
6959
Mike Travis363ab6f2008-05-12 21:21:13 +02006960 for_each_cpu_mask_nr(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006961 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006962 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 int j;
6964
Mike Travis7c16ec52008-04-04 18:11:11 -07006965 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 continue;
6967
Mike Travis7c16ec52008-04-04 18:11:11 -07006968 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006969 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970
Mike Travis363ab6f2008-05-12 21:21:13 +02006971 for_each_cpu_mask_nr(j, *span) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006972 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 continue;
6974
Mike Travis7c16ec52008-04-04 18:11:11 -07006975 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 cpu_set(j, sg->cpumask);
6977 }
6978 if (!first)
6979 first = sg;
6980 if (last)
6981 last->next = sg;
6982 last = sg;
6983 }
6984 last->next = first;
6985}
6986
John Hawkes9c1cfda2005-09-06 15:18:14 -07006987#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988
John Hawkes9c1cfda2005-09-06 15:18:14 -07006989#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006990
John Hawkes9c1cfda2005-09-06 15:18:14 -07006991/**
6992 * find_next_best_node - find the next node to include in a sched_domain
6993 * @node: node whose sched_domain we're building
6994 * @used_nodes: nodes already in the sched_domain
6995 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006996 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006997 * finds the closest node not already in the @used_nodes map.
6998 *
6999 * Should use nodemask_t.
7000 */
Mike Travisc5f59f02008-04-04 18:11:10 -07007001static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007002{
7003 int i, n, val, min_val, best_node = 0;
7004
7005 min_val = INT_MAX;
7006
Mike Travis076ac2a2008-05-12 21:21:12 +02007007 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007008 /* Start at @node */
Mike Travis076ac2a2008-05-12 21:21:12 +02007009 n = (node + i) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007010
7011 if (!nr_cpus_node(n))
7012 continue;
7013
7014 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07007015 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007016 continue;
7017
7018 /* Simple min distance search */
7019 val = node_distance(node, n);
7020
7021 if (val < min_val) {
7022 min_val = val;
7023 best_node = n;
7024 }
7025 }
7026
Mike Travisc5f59f02008-04-04 18:11:10 -07007027 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007028 return best_node;
7029}
7030
7031/**
7032 * sched_domain_node_span - get a cpumask for a node's sched_domain
7033 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07007034 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007035 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007036 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007037 * should be one that prevents unnecessary balancing, but also spreads tasks
7038 * out optimally.
7039 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07007040static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007041{
Mike Travisc5f59f02008-04-04 18:11:10 -07007042 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07007043 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007044 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007045
Mike Travis4bdbaad32008-04-15 16:35:52 -07007046 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007047 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007048
Mike Travis4bdbaad32008-04-15 16:35:52 -07007049 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07007050 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007051
7052 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007053 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007054
Mike Travisc5f59f02008-04-04 18:11:10 -07007055 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007056 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007057 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007058}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007059#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007060
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007061int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007062
John Hawkes9c1cfda2005-09-06 15:18:14 -07007063/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07007064 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07007065 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066#ifdef CONFIG_SCHED_SMT
7067static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007068static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007069
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007070static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007071cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7072 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007074 if (sg)
7075 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 return cpu;
7077}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007078#endif /* CONFIG_SCHED_SMT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079
Ingo Molnar48f24c42006-07-03 00:25:40 -07007080/*
7081 * multi-core sched-domains:
7082 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007083#ifdef CONFIG_SCHED_MC
7084static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007085static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007086#endif /* CONFIG_SCHED_MC */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007087
7088#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007089static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007090cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7091 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007092{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007093 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007094
7095 *mask = per_cpu(cpu_sibling_map, cpu);
7096 cpus_and(*mask, *mask, *cpu_map);
7097 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007098 if (sg)
7099 *sg = &per_cpu(sched_group_core, group);
7100 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007101}
7102#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007103static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007104cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7105 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007106{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007107 if (sg)
7108 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007109 return cpu;
7110}
7111#endif
7112
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007114static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007115
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007116static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007117cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7118 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007120 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007121#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007122 *mask = cpu_coregroup_map(cpu);
7123 cpus_and(*mask, *mask, *cpu_map);
7124 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007125#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007126 *mask = per_cpu(cpu_sibling_map, cpu);
7127 cpus_and(*mask, *mask, *cpu_map);
7128 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007130 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007132 if (sg)
7133 *sg = &per_cpu(sched_group_phys, group);
7134 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135}
7136
7137#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007138/*
7139 * The init_sched_build_groups can't handle what we want to do with node
7140 * groups, so roll our own. Now each node has its own list of groups which
7141 * gets dynamically allocated.
7142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007144static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007145
7146static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007147static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007148
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007149static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007150 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007152 int group;
7153
Mike Travis7c16ec52008-04-04 18:11:11 -07007154 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7155 cpus_and(*nodemask, *nodemask, *cpu_map);
7156 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007157
7158 if (sg)
7159 *sg = &per_cpu(sched_group_allnodes, group);
7160 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007162
Siddha, Suresh B08069032006-03-27 01:15:23 -08007163static void init_numa_sched_groups_power(struct sched_group *group_head)
7164{
7165 struct sched_group *sg = group_head;
7166 int j;
7167
7168 if (!sg)
7169 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007170 do {
Mike Travis363ab6f2008-05-12 21:21:13 +02007171 for_each_cpu_mask_nr(j, sg->cpumask) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02007172 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007173
Andi Kleen3a5c3592007-10-15 17:00:14 +02007174 sd = &per_cpu(phys_domains, j);
7175 if (j != first_cpu(sd->groups->cpumask)) {
7176 /*
7177 * Only add "power" once for each
7178 * physical package.
7179 */
7180 continue;
7181 }
7182
7183 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007184 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007185 sg = sg->next;
7186 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007187}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007188#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007190#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007191/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007192static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007193{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007194 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007195
Mike Travis363ab6f2008-05-12 21:21:13 +02007196 for_each_cpu_mask_nr(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007197 struct sched_group **sched_group_nodes
7198 = sched_group_nodes_bycpu[cpu];
7199
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007200 if (!sched_group_nodes)
7201 continue;
7202
Mike Travis076ac2a2008-05-12 21:21:12 +02007203 for (i = 0; i < nr_node_ids; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007204 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7205
Mike Travis7c16ec52008-04-04 18:11:11 -07007206 *nodemask = node_to_cpumask(i);
7207 cpus_and(*nodemask, *nodemask, *cpu_map);
7208 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007209 continue;
7210
7211 if (sg == NULL)
7212 continue;
7213 sg = sg->next;
7214next_sg:
7215 oldsg = sg;
7216 sg = sg->next;
7217 kfree(oldsg);
7218 if (oldsg != sched_group_nodes[i])
7219 goto next_sg;
7220 }
7221 kfree(sched_group_nodes);
7222 sched_group_nodes_bycpu[cpu] = NULL;
7223 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007224}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007225#else /* !CONFIG_NUMA */
Mike Travis7c16ec52008-04-04 18:11:11 -07007226static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007227{
7228}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007229#endif /* CONFIG_NUMA */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007230
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007232 * Initialize sched groups cpu_power.
7233 *
7234 * cpu_power indicates the capacity of sched group, which is used while
7235 * distributing the load between different sched groups in a sched domain.
7236 * Typically cpu_power for all the groups in a sched domain will be same unless
7237 * there are asymmetries in the topology. If there are asymmetries, group
7238 * having more cpu_power will pickup more load compared to the group having
7239 * less cpu_power.
7240 *
7241 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7242 * the maximum number of tasks a group can handle in the presence of other idle
7243 * or lightly loaded groups in the same sched domain.
7244 */
7245static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7246{
7247 struct sched_domain *child;
7248 struct sched_group *group;
7249
7250 WARN_ON(!sd || !sd->groups);
7251
7252 if (cpu != first_cpu(sd->groups->cpumask))
7253 return;
7254
7255 child = sd->child;
7256
Eric Dumazet5517d862007-05-08 00:32:57 -07007257 sd->groups->__cpu_power = 0;
7258
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007259 /*
7260 * For perf policy, if the groups in child domain share resources
7261 * (for example cores sharing some portions of the cache hierarchy
7262 * or SMT), then set this domain groups cpu_power such that each group
7263 * can handle only one task, when there are other idle groups in the
7264 * same sched domain.
7265 */
7266 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7267 (child->flags &
7268 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007269 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007270 return;
7271 }
7272
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007273 /*
7274 * add cpu_power of each child group to this groups cpu_power
7275 */
7276 group = child->groups;
7277 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007278 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007279 group = group->next;
7280 } while (group != child->groups);
7281}
7282
7283/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007284 * Initializers for schedule domains
7285 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7286 */
7287
Ingo Molnara5d8c342008-10-09 11:35:51 +02007288#ifdef CONFIG_SCHED_DEBUG
7289# define SD_INIT_NAME(sd, type) sd->name = #type
7290#else
7291# define SD_INIT_NAME(sd, type) do { } while (0)
7292#endif
7293
Mike Travis7c16ec52008-04-04 18:11:11 -07007294#define SD_INIT(sd, type) sd_init_##type(sd)
Ingo Molnara5d8c342008-10-09 11:35:51 +02007295
Mike Travis7c16ec52008-04-04 18:11:11 -07007296#define SD_INIT_FUNC(type) \
7297static noinline void sd_init_##type(struct sched_domain *sd) \
7298{ \
7299 memset(sd, 0, sizeof(*sd)); \
7300 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007301 sd->level = SD_LV_##type; \
Ingo Molnara5d8c342008-10-09 11:35:51 +02007302 SD_INIT_NAME(sd, type); \
Mike Travis7c16ec52008-04-04 18:11:11 -07007303}
7304
7305SD_INIT_FUNC(CPU)
7306#ifdef CONFIG_NUMA
7307 SD_INIT_FUNC(ALLNODES)
7308 SD_INIT_FUNC(NODE)
7309#endif
7310#ifdef CONFIG_SCHED_SMT
7311 SD_INIT_FUNC(SIBLING)
7312#endif
7313#ifdef CONFIG_SCHED_MC
7314 SD_INIT_FUNC(MC)
7315#endif
7316
7317/*
7318 * To minimize stack usage kmalloc room for cpumasks and share the
7319 * space as the usage in build_sched_domains() dictates. Used only
7320 * if the amount of space is significant.
7321 */
7322struct allmasks {
7323 cpumask_t tmpmask; /* make this one first */
7324 union {
7325 cpumask_t nodemask;
7326 cpumask_t this_sibling_map;
7327 cpumask_t this_core_map;
7328 };
7329 cpumask_t send_covered;
7330
7331#ifdef CONFIG_NUMA
7332 cpumask_t domainspan;
7333 cpumask_t covered;
7334 cpumask_t notcovered;
7335#endif
7336};
7337
7338#if NR_CPUS > 128
7339#define SCHED_CPUMASK_ALLOC 1
7340#define SCHED_CPUMASK_FREE(v) kfree(v)
7341#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7342#else
7343#define SCHED_CPUMASK_ALLOC 0
7344#define SCHED_CPUMASK_FREE(v)
7345#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7346#endif
7347
7348#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7349 ((unsigned long)(a) + offsetof(struct allmasks, v))
7350
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007351static int default_relax_domain_level = -1;
7352
7353static int __init setup_relax_domain_level(char *str)
7354{
Li Zefan30e0e172008-05-13 10:27:17 +08007355 unsigned long val;
7356
7357 val = simple_strtoul(str, NULL, 0);
7358 if (val < SD_LV_MAX)
7359 default_relax_domain_level = val;
7360
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007361 return 1;
7362}
7363__setup("relax_domain_level=", setup_relax_domain_level);
7364
7365static void set_domain_attribute(struct sched_domain *sd,
7366 struct sched_domain_attr *attr)
7367{
7368 int request;
7369
7370 if (!attr || attr->relax_domain_level < 0) {
7371 if (default_relax_domain_level < 0)
7372 return;
7373 else
7374 request = default_relax_domain_level;
7375 } else
7376 request = attr->relax_domain_level;
7377 if (request < sd->level) {
7378 /* turn off idle balance on this domain */
7379 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7380 } else {
7381 /* turn on idle balance on this domain */
7382 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7383 }
7384}
7385
Mike Travis7c16ec52008-04-04 18:11:11 -07007386/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007387 * Build sched domains for a given set of cpus and attach the sched domains
7388 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007390static int __build_sched_domains(const cpumask_t *cpu_map,
7391 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392{
7393 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007394 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007395 SCHED_CPUMASK_DECLARE(allmasks);
7396 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007397#ifdef CONFIG_NUMA
7398 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007399 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007400
7401 /*
7402 * Allocate the per-node list of sched groups
7403 */
Mike Travis076ac2a2008-05-12 21:21:12 +02007404 sched_group_nodes = kcalloc(nr_node_ids, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007405 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007406 if (!sched_group_nodes) {
7407 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007408 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007409 }
John Hawkesd1b55132005-09-06 15:18:14 -07007410#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411
Gregory Haskinsdc938522008-01-25 21:08:26 +01007412 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007413 if (!rd) {
7414 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007415#ifdef CONFIG_NUMA
7416 kfree(sched_group_nodes);
7417#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007418 return -ENOMEM;
7419 }
7420
Mike Travis7c16ec52008-04-04 18:11:11 -07007421#if SCHED_CPUMASK_ALLOC
7422 /* get space for all scratch cpumask variables */
7423 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7424 if (!allmasks) {
7425 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7426 kfree(rd);
7427#ifdef CONFIG_NUMA
7428 kfree(sched_group_nodes);
7429#endif
7430 return -ENOMEM;
7431 }
7432#endif
7433 tmpmask = (cpumask_t *)allmasks;
7434
7435
7436#ifdef CONFIG_NUMA
7437 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7438#endif
7439
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007441 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442 */
Mike Travis363ab6f2008-05-12 21:21:13 +02007443 for_each_cpu_mask_nr(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007445 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446
Mike Travis7c16ec52008-04-04 18:11:11 -07007447 *nodemask = node_to_cpumask(cpu_to_node(i));
7448 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449
7450#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007451 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007452 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007453 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007454 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007455 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007456 sd->span = *cpu_map;
Mike Travis7c16ec52008-04-04 18:11:11 -07007457 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007458 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007459 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007460 } else
7461 p = NULL;
7462
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007464 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007465 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007466 sched_domain_node_span(cpu_to_node(i), &sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007467 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007468 if (p)
7469 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007470 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471#endif
7472
7473 p = sd;
7474 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007475 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007476 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007477 sd->span = *nodemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007479 if (p)
7480 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007481 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007483#ifdef CONFIG_SCHED_MC
7484 p = sd;
7485 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007486 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007487 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007488 sd->span = cpu_coregroup_map(i);
7489 cpus_and(sd->span, sd->span, *cpu_map);
7490 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007491 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007492 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007493#endif
7494
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495#ifdef CONFIG_SCHED_SMT
7496 p = sd;
7497 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007498 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007499 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007500 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007501 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007503 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007504 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505#endif
7506 }
7507
7508#ifdef CONFIG_SCHED_SMT
7509 /* Set up CPU (sibling) groups */
Mike Travis363ab6f2008-05-12 21:21:13 +02007510 for_each_cpu_mask_nr(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007511 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7512 SCHED_CPUMASK_VAR(send_covered, allmasks);
7513
7514 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7515 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7516 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517 continue;
7518
Ingo Molnardd41f592007-07-09 18:51:59 +02007519 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007520 &cpu_to_cpu_group,
7521 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522 }
7523#endif
7524
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007525#ifdef CONFIG_SCHED_MC
7526 /* Set up multi-core groups */
Mike Travis363ab6f2008-05-12 21:21:13 +02007527 for_each_cpu_mask_nr(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007528 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7529 SCHED_CPUMASK_VAR(send_covered, allmasks);
7530
7531 *this_core_map = cpu_coregroup_map(i);
7532 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7533 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007534 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007535
Ingo Molnardd41f592007-07-09 18:51:59 +02007536 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007537 &cpu_to_core_group,
7538 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007539 }
7540#endif
7541
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542 /* Set up physical groups */
Mike Travis076ac2a2008-05-12 21:21:12 +02007543 for (i = 0; i < nr_node_ids; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007544 SCHED_CPUMASK_VAR(nodemask, allmasks);
7545 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546
Mike Travis7c16ec52008-04-04 18:11:11 -07007547 *nodemask = node_to_cpumask(i);
7548 cpus_and(*nodemask, *nodemask, *cpu_map);
7549 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 continue;
7551
Mike Travis7c16ec52008-04-04 18:11:11 -07007552 init_sched_build_groups(nodemask, cpu_map,
7553 &cpu_to_phys_group,
7554 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 }
7556
7557#ifdef CONFIG_NUMA
7558 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007559 if (sd_allnodes) {
7560 SCHED_CPUMASK_VAR(send_covered, allmasks);
7561
7562 init_sched_build_groups(cpu_map, cpu_map,
7563 &cpu_to_allnodes_group,
7564 send_covered, tmpmask);
7565 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007566
Mike Travis076ac2a2008-05-12 21:21:12 +02007567 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007568 /* Set up node groups */
7569 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007570 SCHED_CPUMASK_VAR(nodemask, allmasks);
7571 SCHED_CPUMASK_VAR(domainspan, allmasks);
7572 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007573 int j;
7574
Mike Travis7c16ec52008-04-04 18:11:11 -07007575 *nodemask = node_to_cpumask(i);
7576 cpus_clear(*covered);
7577
7578 cpus_and(*nodemask, *nodemask, *cpu_map);
7579 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007580 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007581 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007582 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007583
Mike Travis4bdbaad32008-04-15 16:35:52 -07007584 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007585 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007586
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007587 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007588 if (!sg) {
7589 printk(KERN_WARNING "Can not alloc domain group for "
7590 "node %d\n", i);
7591 goto error;
7592 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007593 sched_group_nodes[i] = sg;
Mike Travis363ab6f2008-05-12 21:21:13 +02007594 for_each_cpu_mask_nr(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007595 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007596
John Hawkes9c1cfda2005-09-06 15:18:14 -07007597 sd = &per_cpu(node_domains, j);
7598 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007599 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007600 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007601 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007602 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007603 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007604 prev = sg;
7605
Mike Travis076ac2a2008-05-12 21:21:12 +02007606 for (j = 0; j < nr_node_ids; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007607 SCHED_CPUMASK_VAR(notcovered, allmasks);
Mike Travis076ac2a2008-05-12 21:21:12 +02007608 int n = (i + j) % nr_node_ids;
Mike Travisc5f59f02008-04-04 18:11:10 -07007609 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007610
Mike Travis7c16ec52008-04-04 18:11:11 -07007611 cpus_complement(*notcovered, *covered);
7612 cpus_and(*tmpmask, *notcovered, *cpu_map);
7613 cpus_and(*tmpmask, *tmpmask, *domainspan);
7614 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007615 break;
7616
Mike Travis7c16ec52008-04-04 18:11:11 -07007617 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7618 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007619 continue;
7620
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007621 sg = kmalloc_node(sizeof(struct sched_group),
7622 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007623 if (!sg) {
7624 printk(KERN_WARNING
7625 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007626 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007627 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007628 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007629 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007630 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007631 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007632 prev->next = sg;
7633 prev = sg;
7634 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636#endif
7637
7638 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007639#ifdef CONFIG_SCHED_SMT
Mike Travis363ab6f2008-05-12 21:21:13 +02007640 for_each_cpu_mask_nr(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007641 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7642
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007643 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007644 }
7645#endif
7646#ifdef CONFIG_SCHED_MC
Mike Travis363ab6f2008-05-12 21:21:13 +02007647 for_each_cpu_mask_nr(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007648 struct sched_domain *sd = &per_cpu(core_domains, i);
7649
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007650 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007651 }
7652#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653
Mike Travis363ab6f2008-05-12 21:21:13 +02007654 for_each_cpu_mask_nr(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007655 struct sched_domain *sd = &per_cpu(phys_domains, i);
7656
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007657 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658 }
7659
John Hawkes9c1cfda2005-09-06 15:18:14 -07007660#ifdef CONFIG_NUMA
Mike Travis076ac2a2008-05-12 21:21:12 +02007661 for (i = 0; i < nr_node_ids; i++)
Siddha, Suresh B08069032006-03-27 01:15:23 -08007662 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007663
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007664 if (sd_allnodes) {
7665 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007666
Mike Travis7c16ec52008-04-04 18:11:11 -07007667 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7668 tmpmask);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007669 init_numa_sched_groups_power(sg);
7670 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007671#endif
7672
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673 /* Attach the domains */
Mike Travis363ab6f2008-05-12 21:21:13 +02007674 for_each_cpu_mask_nr(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675 struct sched_domain *sd;
7676#ifdef CONFIG_SCHED_SMT
7677 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007678#elif defined(CONFIG_SCHED_MC)
7679 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680#else
7681 sd = &per_cpu(phys_domains, i);
7682#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007683 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007685
Mike Travis7c16ec52008-04-04 18:11:11 -07007686 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007687 return 0;
7688
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007689#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007690error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007691 free_sched_groups(cpu_map, tmpmask);
7692 SCHED_CPUMASK_FREE((void *)allmasks);
Li Zefanca3273f92008-11-07 14:47:21 +08007693 kfree(rd);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007694 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696}
Paul Jackson029190c2007-10-18 23:40:20 -07007697
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007698static int build_sched_domains(const cpumask_t *cpu_map)
7699{
7700 return __build_sched_domains(cpu_map, NULL);
7701}
7702
Paul Jackson029190c2007-10-18 23:40:20 -07007703static cpumask_t *doms_cur; /* current sched domains */
7704static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007705static struct sched_domain_attr *dattr_cur;
7706 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007707
7708/*
7709 * Special case: If a kmalloc of a doms_cur partition (array of
7710 * cpumask_t) fails, then fallback to a single sched domain,
7711 * as determined by the single cpumask_t fallback_doms.
7712 */
7713static cpumask_t fallback_doms;
7714
Heiko Carstens22e52b02008-03-12 18:31:59 +01007715void __attribute__((weak)) arch_update_cpu_topology(void)
7716{
7717}
7718
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007719/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007720 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007721 * For now this just excludes isolated cpus, but could be used to
7722 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007723 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007724static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007725{
Milton Miller73785472007-10-24 18:23:48 +02007726 int err;
7727
Heiko Carstens22e52b02008-03-12 18:31:59 +01007728 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007729 ndoms_cur = 1;
7730 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7731 if (!doms_cur)
7732 doms_cur = &fallback_doms;
7733 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007734 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007735 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007736 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007737
7738 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007739}
7740
Mike Travis7c16ec52008-04-04 18:11:11 -07007741static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7742 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743{
Mike Travis7c16ec52008-04-04 18:11:11 -07007744 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007745}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007746
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007747/*
7748 * Detach sched domains from a group of cpus specified in cpu_map
7749 * These cpus will now be attached to the NULL domain
7750 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007751static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007752{
Mike Travis7c16ec52008-04-04 18:11:11 -07007753 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007754 int i;
7755
Milton Miller6382bc92007-10-15 17:00:19 +02007756 unregister_sched_domain_sysctl();
7757
Mike Travis363ab6f2008-05-12 21:21:13 +02007758 for_each_cpu_mask_nr(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007759 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007760 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007761 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007762}
7763
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007764/* handle null as "default" */
7765static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7766 struct sched_domain_attr *new, int idx_new)
7767{
7768 struct sched_domain_attr tmp;
7769
7770 /* fast path */
7771 if (!new && !cur)
7772 return 1;
7773
7774 tmp = SD_ATTR_INIT;
7775 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7776 new ? (new + idx_new) : &tmp,
7777 sizeof(struct sched_domain_attr));
7778}
7779
Paul Jackson029190c2007-10-18 23:40:20 -07007780/*
7781 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007782 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007783 * doms_new[] to the current sched domain partitioning, doms_cur[].
7784 * It destroys each deleted domain and builds each new domain.
7785 *
7786 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007787 * The masks don't intersect (don't overlap.) We should setup one
7788 * sched domain for each mask. CPUs not in any of the cpumasks will
7789 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007790 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7791 * it as it is.
7792 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007793 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7794 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007795 * failed the kmalloc call, then it can pass in doms_new == NULL,
7796 * and partition_sched_domains() will fallback to the single partition
Max Krasnyanskye761b772008-07-15 04:43:49 -07007797 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07007798 *
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007799 * If doms_new==NULL it will be replaced with cpu_online_map.
7800 * ndoms_new==0 is a special case for destroying existing domains.
7801 * It will not create the default domain.
7802 *
Paul Jackson029190c2007-10-18 23:40:20 -07007803 * Call with hotplug lock held
7804 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007805void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7806 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007807{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007808 int i, j, n;
Paul Jackson029190c2007-10-18 23:40:20 -07007809
Heiko Carstens712555e2008-04-28 11:33:07 +02007810 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007811
Milton Miller73785472007-10-24 18:23:48 +02007812 /* always unregister in case we don't destroy any domains */
7813 unregister_sched_domain_sysctl();
7814
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007815 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07007816
7817 /* Destroy deleted domains */
7818 for (i = 0; i < ndoms_cur; i++) {
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007819 for (j = 0; j < n; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007820 if (cpus_equal(doms_cur[i], doms_new[j])
7821 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007822 goto match1;
7823 }
7824 /* no match - a current sched domain not in new doms_new[] */
7825 detach_destroy_domains(doms_cur + i);
7826match1:
7827 ;
7828 }
7829
Max Krasnyanskye761b772008-07-15 04:43:49 -07007830 if (doms_new == NULL) {
7831 ndoms_cur = 0;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007832 doms_new = &fallback_doms;
7833 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
7834 dattr_new = NULL;
7835 }
7836
Paul Jackson029190c2007-10-18 23:40:20 -07007837 /* Build new domains */
7838 for (i = 0; i < ndoms_new; i++) {
7839 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007840 if (cpus_equal(doms_new[i], doms_cur[j])
7841 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007842 goto match2;
7843 }
7844 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007845 __build_sched_domains(doms_new + i,
7846 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007847match2:
7848 ;
7849 }
7850
7851 /* Remember the new sched domains */
7852 if (doms_cur != &fallback_doms)
7853 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007854 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007855 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007856 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007857 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007858
7859 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007860
Heiko Carstens712555e2008-04-28 11:33:07 +02007861 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007862}
7863
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007864#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007865int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007866{
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007867 get_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007868
7869 /* Destroy domains first to force the rebuild */
7870 partition_sched_domains(0, NULL, NULL);
7871
Max Krasnyanskye761b772008-07-15 04:43:49 -07007872 rebuild_sched_domains();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007873 put_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007874
Max Krasnyanskye761b772008-07-15 04:43:49 -07007875 return 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007876}
7877
7878static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7879{
7880 int ret;
7881
7882 if (buf[0] != '0' && buf[0] != '1')
7883 return -EINVAL;
7884
7885 if (smt)
7886 sched_smt_power_savings = (buf[0] == '1');
7887 else
7888 sched_mc_power_savings = (buf[0] == '1');
7889
7890 ret = arch_reinit_sched_domains();
7891
7892 return ret ? ret : count;
7893}
7894
Adrian Bunk6707de002007-08-12 18:08:19 +02007895#ifdef CONFIG_SCHED_MC
Andi Kleenf718cd42008-07-29 22:33:52 -07007896static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
7897 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007898{
7899 return sprintf(page, "%u\n", sched_mc_power_savings);
7900}
Andi Kleenf718cd42008-07-29 22:33:52 -07007901static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
Adrian Bunk6707de002007-08-12 18:08:19 +02007902 const char *buf, size_t count)
7903{
7904 return sched_power_savings_store(buf, count, 0);
7905}
Andi Kleenf718cd42008-07-29 22:33:52 -07007906static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
7907 sched_mc_power_savings_show,
7908 sched_mc_power_savings_store);
Adrian Bunk6707de002007-08-12 18:08:19 +02007909#endif
7910
7911#ifdef CONFIG_SCHED_SMT
Andi Kleenf718cd42008-07-29 22:33:52 -07007912static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
7913 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007914{
7915 return sprintf(page, "%u\n", sched_smt_power_savings);
7916}
Andi Kleenf718cd42008-07-29 22:33:52 -07007917static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
Adrian Bunk6707de002007-08-12 18:08:19 +02007918 const char *buf, size_t count)
7919{
7920 return sched_power_savings_store(buf, count, 1);
7921}
Andi Kleenf718cd42008-07-29 22:33:52 -07007922static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
7923 sched_smt_power_savings_show,
Adrian Bunk6707de002007-08-12 18:08:19 +02007924 sched_smt_power_savings_store);
7925#endif
7926
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007927int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7928{
7929 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007930
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007931#ifdef CONFIG_SCHED_SMT
7932 if (smt_capable())
7933 err = sysfs_create_file(&cls->kset.kobj,
7934 &attr_sched_smt_power_savings.attr);
7935#endif
7936#ifdef CONFIG_SCHED_MC
7937 if (!err && mc_capable())
7938 err = sysfs_create_file(&cls->kset.kobj,
7939 &attr_sched_mc_power_savings.attr);
7940#endif
7941 return err;
7942}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007943#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007944
Max Krasnyanskye761b772008-07-15 04:43:49 -07007945#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946/*
Max Krasnyanskye761b772008-07-15 04:43:49 -07007947 * Add online and remove offline CPUs from the scheduler domains.
7948 * When cpusets are enabled they take over this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949 */
7950static int update_sched_domains(struct notifier_block *nfb,
7951 unsigned long action, void *hcpu)
7952{
Max Krasnyanskye761b772008-07-15 04:43:49 -07007953 switch (action) {
7954 case CPU_ONLINE:
7955 case CPU_ONLINE_FROZEN:
7956 case CPU_DEAD:
7957 case CPU_DEAD_FROZEN:
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007958 partition_sched_domains(1, NULL, NULL);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007959 return NOTIFY_OK;
7960
7961 default:
7962 return NOTIFY_DONE;
7963 }
7964}
7965#endif
7966
7967static int update_runtime(struct notifier_block *nfb,
7968 unsigned long action, void *hcpu)
7969{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007970 int cpu = (int)(long)hcpu;
7971
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007974 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007975 disable_runtime(cpu_rq(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976 return NOTIFY_OK;
7977
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007979 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007981 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007982 enable_runtime(cpu_rq(cpu));
Max Krasnyanskye761b772008-07-15 04:43:49 -07007983 return NOTIFY_OK;
7984
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 default:
7986 return NOTIFY_DONE;
7987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989
7990void __init sched_init_smp(void)
7991{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007992 cpumask_t non_isolated_cpus;
7993
Mike Travis434d53b2008-04-04 18:11:04 -07007994#if defined(CONFIG_NUMA)
7995 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7996 GFP_KERNEL);
7997 BUG_ON(sched_group_nodes_bycpu == NULL);
7998#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007999 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02008000 mutex_lock(&sched_domains_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008001 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08008002 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07008003 if (cpus_empty(non_isolated_cpus))
8004 cpu_set(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02008005 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008006 put_online_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07008007
8008#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009 /* XXX: Theoretical race here - CPU may be hotplugged now */
8010 hotcpu_notifier(update_sched_domains, 0);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008011#endif
8012
8013 /* RT runtime code needs to handle some hotplug events */
8014 hotcpu_notifier(update_runtime, 0);
8015
Peter Zijlstrab328ca12008-04-29 10:02:46 +02008016 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07008017
8018 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07008019 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07008020 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01008021 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008022}
8023#else
8024void __init sched_init_smp(void)
8025{
Ingo Molnar19978ca2007-11-09 22:39:38 +01008026 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027}
8028#endif /* CONFIG_SMP */
8029
8030int in_sched_functions(unsigned long addr)
8031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032 return in_lock_functions(addr) ||
8033 (addr >= (unsigned long)__sched_text_start
8034 && addr < (unsigned long)__sched_text_end);
8035}
8036
Alexey Dobriyana9957442007-10-15 17:00:13 +02008037static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02008038{
8039 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02008040 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02008041#ifdef CONFIG_FAIR_GROUP_SCHED
8042 cfs_rq->rq = rq;
8043#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02008044 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02008045}
8046
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008047static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
8048{
8049 struct rt_prio_array *array;
8050 int i;
8051
8052 array = &rt_rq->active;
8053 for (i = 0; i < MAX_RT_PRIO; i++) {
8054 INIT_LIST_HEAD(array->queue + i);
8055 __clear_bit(i, array->bitmap);
8056 }
8057 /* delimiter for bitsearch: */
8058 __set_bit(MAX_RT_PRIO, array->bitmap);
8059
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008060#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01008061 rt_rq->highest_prio = MAX_RT_PRIO;
8062#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008063#ifdef CONFIG_SMP
8064 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008065 rt_rq->overloaded = 0;
8066#endif
8067
8068 rt_rq->rt_time = 0;
8069 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008070 rt_rq->rt_runtime = 0;
8071 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008072
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008073#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01008074 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008075 rt_rq->rq = rq;
8076#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008077}
8078
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008079#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008080static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
8081 struct sched_entity *se, int cpu, int add,
8082 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008083{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008084 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008085 tg->cfs_rq[cpu] = cfs_rq;
8086 init_cfs_rq(cfs_rq, rq);
8087 cfs_rq->tg = tg;
8088 if (add)
8089 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
8090
8091 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008092 /* se could be NULL for init_task_group */
8093 if (!se)
8094 return;
8095
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008096 if (!parent)
8097 se->cfs_rq = &rq->cfs;
8098 else
8099 se->cfs_rq = parent->my_q;
8100
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008101 se->my_q = cfs_rq;
8102 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008103 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008104 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008105}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008106#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008107
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008108#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008109static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8110 struct sched_rt_entity *rt_se, int cpu, int add,
8111 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008112{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008113 struct rq *rq = cpu_rq(cpu);
8114
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008115 tg->rt_rq[cpu] = rt_rq;
8116 init_rt_rq(rt_rq, rq);
8117 rt_rq->tg = tg;
8118 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008119 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008120 if (add)
8121 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8122
8123 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008124 if (!rt_se)
8125 return;
8126
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008127 if (!parent)
8128 rt_se->rt_rq = &rq->rt;
8129 else
8130 rt_se->rt_rq = parent->my_q;
8131
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008132 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008133 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008134 INIT_LIST_HEAD(&rt_se->run_list);
8135}
8136#endif
8137
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138void __init sched_init(void)
8139{
Ingo Molnardd41f592007-07-09 18:51:59 +02008140 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008141 unsigned long alloc_size = 0, ptr;
8142
8143#ifdef CONFIG_FAIR_GROUP_SCHED
8144 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8145#endif
8146#ifdef CONFIG_RT_GROUP_SCHED
8147 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8148#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008149#ifdef CONFIG_USER_SCHED
8150 alloc_size *= 2;
8151#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008152 /*
8153 * As sched_init() is called before page_alloc is setup,
8154 * we use alloc_bootmem().
8155 */
8156 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07008157 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07008158
8159#ifdef CONFIG_FAIR_GROUP_SCHED
8160 init_task_group.se = (struct sched_entity **)ptr;
8161 ptr += nr_cpu_ids * sizeof(void **);
8162
8163 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8164 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008165
8166#ifdef CONFIG_USER_SCHED
8167 root_task_group.se = (struct sched_entity **)ptr;
8168 ptr += nr_cpu_ids * sizeof(void **);
8169
8170 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8171 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008172#endif /* CONFIG_USER_SCHED */
8173#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008174#ifdef CONFIG_RT_GROUP_SCHED
8175 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8176 ptr += nr_cpu_ids * sizeof(void **);
8177
8178 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008179 ptr += nr_cpu_ids * sizeof(void **);
8180
8181#ifdef CONFIG_USER_SCHED
8182 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8183 ptr += nr_cpu_ids * sizeof(void **);
8184
8185 root_task_group.rt_rq = (struct rt_rq **)ptr;
8186 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008187#endif /* CONFIG_USER_SCHED */
8188#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008189 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008190
Gregory Haskins57d885f2008-01-25 21:08:18 +01008191#ifdef CONFIG_SMP
8192 init_defrootdomain();
8193#endif
8194
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008195 init_rt_bandwidth(&def_rt_bandwidth,
8196 global_rt_period(), global_rt_runtime());
8197
8198#ifdef CONFIG_RT_GROUP_SCHED
8199 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8200 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008201#ifdef CONFIG_USER_SCHED
8202 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8203 global_rt_period(), RUNTIME_INF);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008204#endif /* CONFIG_USER_SCHED */
8205#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008206
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008207#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008208 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008209 INIT_LIST_HEAD(&init_task_group.children);
8210
8211#ifdef CONFIG_USER_SCHED
8212 INIT_LIST_HEAD(&root_task_group.children);
8213 init_task_group.parent = &root_task_group;
8214 list_add(&init_task_group.siblings, &root_task_group.children);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008215#endif /* CONFIG_USER_SCHED */
8216#endif /* CONFIG_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008217
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008218 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008219 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008220
8221 rq = cpu_rq(i);
8222 spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07008223 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008224 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008225 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008226#ifdef CONFIG_FAIR_GROUP_SCHED
8227 init_task_group.shares = init_task_group_load;
8228 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008229#ifdef CONFIG_CGROUP_SCHED
8230 /*
8231 * How much cpu bandwidth does init_task_group get?
8232 *
8233 * In case of task-groups formed thr' the cgroup filesystem, it
8234 * gets 100% of the cpu resources in the system. This overall
8235 * system cpu resource is divided among the tasks of
8236 * init_task_group and its child task-groups in a fair manner,
8237 * based on each entity's (task or task-group's) weight
8238 * (se->load.weight).
8239 *
8240 * In other words, if init_task_group has 10 tasks of weight
8241 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8242 * then A0's share of the cpu resource is:
8243 *
8244 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8245 *
8246 * We achieve this by letting init_task_group's tasks sit
8247 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8248 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008249 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008250#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008251 root_task_group.shares = NICE_0_LOAD;
8252 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008253 /*
8254 * In case of task-groups formed thr' the user id of tasks,
8255 * init_task_group represents tasks belonging to root user.
8256 * Hence it forms a sibling of all subsequent groups formed.
8257 * In this case, init_task_group gets only a fraction of overall
8258 * system cpu resource, based on the weight assigned to root
8259 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8260 * by letting tasks of init_task_group sit in a separate cfs_rq
8261 * (init_cfs_rq) and having one entity represent this group of
8262 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8263 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008264 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008265 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008266 &per_cpu(init_sched_entity, i), i, 1,
8267 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008268
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008269#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008270#endif /* CONFIG_FAIR_GROUP_SCHED */
8271
8272 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008273#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008274 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008275#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008276 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008277#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008278 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008279 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008280 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008281 &per_cpu(init_sched_rt_entity, i), i, 1,
8282 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008283#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008284#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285
Ingo Molnardd41f592007-07-09 18:51:59 +02008286 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8287 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008288#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008289 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008290 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008292 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008294 rq->cpu = i;
Gregory Haskins1f11eb62008-06-04 15:04:05 -04008295 rq->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008296 rq->migration_thread = NULL;
8297 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008298 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008300 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302 }
8303
Peter Williams2dd73a42006-06-27 02:54:34 -07008304 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008305
Avi Kivitye107be32007-07-26 13:40:43 +02008306#ifdef CONFIG_PREEMPT_NOTIFIERS
8307 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8308#endif
8309
Christoph Lameterc9819f42006-12-10 02:20:25 -08008310#ifdef CONFIG_SMP
Carlos R. Mafra962cf362008-05-15 11:15:37 -03008311 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
Christoph Lameterc9819f42006-12-10 02:20:25 -08008312#endif
8313
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008314#ifdef CONFIG_RT_MUTEXES
8315 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8316#endif
8317
Linus Torvalds1da177e2005-04-16 15:20:36 -07008318 /*
8319 * The boot idle thread does lazy MMU switching as well:
8320 */
8321 atomic_inc(&init_mm.mm_count);
8322 enter_lazy_tlb(&init_mm, current);
8323
8324 /*
8325 * Make us the idle thread. Technically, schedule() should not be
8326 * called from this thread, however somewhere below it might be,
8327 * but because we are the idle thread, we just pick up running again
8328 * when this runqueue becomes "idle".
8329 */
8330 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008331 /*
8332 * During early bootup we pretend to be a normal task:
8333 */
8334 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008335
8336 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008337}
8338
8339#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8340void __might_sleep(char *file, int line)
8341{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008342#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343 static unsigned long prev_jiffy; /* ratelimiting */
8344
Ingo Molnaraef745f2008-08-28 11:34:43 +02008345 if ((!in_atomic() && !irqs_disabled()) ||
8346 system_state != SYSTEM_RUNNING || oops_in_progress)
8347 return;
8348 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8349 return;
8350 prev_jiffy = jiffies;
8351
8352 printk(KERN_ERR
8353 "BUG: sleeping function called from invalid context at %s:%d\n",
8354 file, line);
8355 printk(KERN_ERR
8356 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
8357 in_atomic(), irqs_disabled(),
8358 current->pid, current->comm);
8359
8360 debug_show_held_locks(current);
8361 if (irqs_disabled())
8362 print_irqtrace_events(current);
8363 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008364#endif
8365}
8366EXPORT_SYMBOL(__might_sleep);
8367#endif
8368
8369#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008370static void normalize_task(struct rq *rq, struct task_struct *p)
8371{
8372 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02008373
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008374 update_rq_clock(rq);
8375 on_rq = p->se.on_rq;
8376 if (on_rq)
8377 deactivate_task(rq, p, 0);
8378 __setscheduler(rq, p, SCHED_NORMAL, 0);
8379 if (on_rq) {
8380 activate_task(rq, p, 0);
8381 resched_task(rq->curr);
8382 }
8383}
8384
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385void normalize_rt_tasks(void)
8386{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008387 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008389 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008391 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008392 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008393 /*
8394 * Only normalize user tasks:
8395 */
8396 if (!p->mm)
8397 continue;
8398
Ingo Molnardd41f592007-07-09 18:51:59 +02008399 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008400#ifdef CONFIG_SCHEDSTATS
8401 p->se.wait_start = 0;
8402 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008403 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008404#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008405
8406 if (!rt_task(p)) {
8407 /*
8408 * Renice negative nice level userspace
8409 * tasks back to 0:
8410 */
8411 if (TASK_NICE(p) < 0 && p->mm)
8412 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008416 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008417 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418
Ingo Molnar178be792007-10-15 17:00:18 +02008419 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008420
Ingo Molnarb29739f2006-06-27 02:54:51 -07008421 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008422 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008423 } while_each_thread(g, p);
8424
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008425 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426}
8427
8428#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008429
8430#ifdef CONFIG_IA64
8431/*
8432 * These functions are only useful for the IA64 MCA handling.
8433 *
8434 * They can only be called when the whole system has been
8435 * stopped - every CPU needs to be quiescent, and no scheduling
8436 * activity can take place. Using them for anything else would
8437 * be a serious bug, and as a result, they aren't even visible
8438 * under any other configuration.
8439 */
8440
8441/**
8442 * curr_task - return the current task for a given cpu.
8443 * @cpu: the processor in question.
8444 *
8445 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8446 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008447struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008448{
8449 return cpu_curr(cpu);
8450}
8451
8452/**
8453 * set_curr_task - set the current task for a given cpu.
8454 * @cpu: the processor in question.
8455 * @p: the task pointer to set.
8456 *
8457 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008458 * are serviced on a separate stack. It allows the architecture to switch the
8459 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008460 * must be called with all CPU's synchronized, and interrupts disabled, the
8461 * and caller must save the original value of the current task (see
8462 * curr_task() above) and restore that value before reenabling interrupts and
8463 * re-starting the system.
8464 *
8465 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8466 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008467void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008468{
8469 cpu_curr(cpu) = p;
8470}
8471
8472#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008473
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008474#ifdef CONFIG_FAIR_GROUP_SCHED
8475static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008476{
8477 int i;
8478
8479 for_each_possible_cpu(i) {
8480 if (tg->cfs_rq)
8481 kfree(tg->cfs_rq[i]);
8482 if (tg->se)
8483 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008484 }
8485
8486 kfree(tg->cfs_rq);
8487 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008488}
8489
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008490static
8491int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008492{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008493 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008494 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008495 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008496 int i;
8497
Mike Travis434d53b2008-04-04 18:11:04 -07008498 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008499 if (!tg->cfs_rq)
8500 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008501 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008502 if (!tg->se)
8503 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008504
8505 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008506
8507 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008508 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008509
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008510 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8511 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008512 if (!cfs_rq)
8513 goto err;
8514
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008515 se = kmalloc_node(sizeof(struct sched_entity),
8516 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008517 if (!se)
8518 goto err;
8519
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008520 parent_se = parent ? parent->se[i] : NULL;
8521 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008522 }
8523
8524 return 1;
8525
8526 err:
8527 return 0;
8528}
8529
8530static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8531{
8532 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8533 &cpu_rq(cpu)->leaf_cfs_rq_list);
8534}
8535
8536static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8537{
8538 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8539}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008540#else /* !CONFG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008541static inline void free_fair_sched_group(struct task_group *tg)
8542{
8543}
8544
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008545static inline
8546int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008547{
8548 return 1;
8549}
8550
8551static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8552{
8553}
8554
8555static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8556{
8557}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008558#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008559
8560#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008561static void free_rt_sched_group(struct task_group *tg)
8562{
8563 int i;
8564
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008565 destroy_rt_bandwidth(&tg->rt_bandwidth);
8566
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008567 for_each_possible_cpu(i) {
8568 if (tg->rt_rq)
8569 kfree(tg->rt_rq[i]);
8570 if (tg->rt_se)
8571 kfree(tg->rt_se[i]);
8572 }
8573
8574 kfree(tg->rt_rq);
8575 kfree(tg->rt_se);
8576}
8577
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008578static
8579int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008580{
8581 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008582 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008583 struct rq *rq;
8584 int i;
8585
Mike Travis434d53b2008-04-04 18:11:04 -07008586 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008587 if (!tg->rt_rq)
8588 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008589 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008590 if (!tg->rt_se)
8591 goto err;
8592
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008593 init_rt_bandwidth(&tg->rt_bandwidth,
8594 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008595
8596 for_each_possible_cpu(i) {
8597 rq = cpu_rq(i);
8598
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008599 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8600 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8601 if (!rt_rq)
8602 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008603
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008604 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8605 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8606 if (!rt_se)
8607 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008608
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008609 parent_se = parent ? parent->rt_se[i] : NULL;
8610 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008611 }
8612
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008613 return 1;
8614
8615 err:
8616 return 0;
8617}
8618
8619static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8620{
8621 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8622 &cpu_rq(cpu)->leaf_rt_rq_list);
8623}
8624
8625static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8626{
8627 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8628}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008629#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008630static inline void free_rt_sched_group(struct task_group *tg)
8631{
8632}
8633
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008634static inline
8635int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008636{
8637 return 1;
8638}
8639
8640static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8641{
8642}
8643
8644static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8645{
8646}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008647#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008648
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008649#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008650static void free_sched_group(struct task_group *tg)
8651{
8652 free_fair_sched_group(tg);
8653 free_rt_sched_group(tg);
8654 kfree(tg);
8655}
8656
8657/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008658struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008659{
8660 struct task_group *tg;
8661 unsigned long flags;
8662 int i;
8663
8664 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8665 if (!tg)
8666 return ERR_PTR(-ENOMEM);
8667
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008668 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008669 goto err;
8670
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008671 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008672 goto err;
8673
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008674 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008675 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008676 register_fair_sched_group(tg, i);
8677 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008678 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008679 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008680
8681 WARN_ON(!parent); /* root should already exist */
8682
8683 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008684 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08008685 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008686 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008687
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008688 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008689
8690err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008691 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008692 return ERR_PTR(-ENOMEM);
8693}
8694
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008695/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008696static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008697{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008698 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008699 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008700}
8701
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008702/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008703void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008704{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008705 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008706 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008707
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008708 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008709 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008710 unregister_fair_sched_group(tg, i);
8711 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008712 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008713 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008714 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008715 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008716
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008717 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008718 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008719}
8720
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008721/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008722 * The caller of this function should have put the task in its new group
8723 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8724 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008725 */
8726void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008727{
8728 int on_rq, running;
8729 unsigned long flags;
8730 struct rq *rq;
8731
8732 rq = task_rq_lock(tsk, &flags);
8733
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008734 update_rq_clock(rq);
8735
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008736 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008737 on_rq = tsk->se.on_rq;
8738
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008739 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008740 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008741 if (unlikely(running))
8742 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008743
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008744 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008745
Peter Zijlstra810b3812008-02-29 15:21:01 -05008746#ifdef CONFIG_FAIR_GROUP_SCHED
8747 if (tsk->sched_class->moved_group)
8748 tsk->sched_class->moved_group(tsk);
8749#endif
8750
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008751 if (unlikely(running))
8752 tsk->sched_class->set_curr_task(rq);
8753 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008754 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008755
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008756 task_rq_unlock(rq, &flags);
8757}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008758#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008759
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008760#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008761static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008762{
8763 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008764 int on_rq;
8765
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008766 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008767 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008768 dequeue_entity(cfs_rq, se, 0);
8769
8770 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008771 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008772
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008773 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008774 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008775}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008776
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008777static void set_se_shares(struct sched_entity *se, unsigned long shares)
8778{
8779 struct cfs_rq *cfs_rq = se->cfs_rq;
8780 struct rq *rq = cfs_rq->rq;
8781 unsigned long flags;
8782
8783 spin_lock_irqsave(&rq->lock, flags);
8784 __set_se_shares(se, shares);
8785 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008786}
8787
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008788static DEFINE_MUTEX(shares_mutex);
8789
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008790int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008791{
8792 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008793 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008794
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008795 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008796 * We can't change the weight of the root cgroup.
8797 */
8798 if (!tg->se[0])
8799 return -EINVAL;
8800
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008801 if (shares < MIN_SHARES)
8802 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008803 else if (shares > MAX_SHARES)
8804 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008805
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008806 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008807 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008808 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008809
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008810 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008811 for_each_possible_cpu(i)
8812 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008813 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008814 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008815
8816 /* wait for any ongoing reference to this group to finish */
8817 synchronize_sched();
8818
8819 /*
8820 * Now we are free to modify the group's share on each cpu
8821 * w/o tripping rebalance_share or load_balance_fair.
8822 */
8823 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008824 for_each_possible_cpu(i) {
8825 /*
8826 * force a rebalance
8827 */
8828 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008829 set_se_shares(tg->se[i], shares);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008830 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008831
8832 /*
8833 * Enable load balance activity on this group, by inserting it back on
8834 * each cpu's rq->leaf_cfs_rq_list.
8835 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008836 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008837 for_each_possible_cpu(i)
8838 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008839 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008840 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008841done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008842 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008843 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008844}
8845
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008846unsigned long sched_group_shares(struct task_group *tg)
8847{
8848 return tg->shares;
8849}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008850#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008851
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008852#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008853/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008854 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008855 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008856static DEFINE_MUTEX(rt_constraints_mutex);
8857
8858static unsigned long to_ratio(u64 period, u64 runtime)
8859{
8860 if (runtime == RUNTIME_INF)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008861 return 1ULL << 20;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008862
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008863 return div64_u64(runtime << 20, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008864}
8865
Dhaval Giani521f1a242008-02-28 15:21:56 +05308866/* Must be called with tasklist_lock held */
8867static inline int tg_has_rt_tasks(struct task_group *tg)
8868{
8869 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008870
Dhaval Giani521f1a242008-02-28 15:21:56 +05308871 do_each_thread(g, p) {
8872 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8873 return 1;
8874 } while_each_thread(g, p);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008875
Dhaval Giani521f1a242008-02-28 15:21:56 +05308876 return 0;
8877}
8878
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008879struct rt_schedulable_data {
8880 struct task_group *tg;
8881 u64 rt_period;
8882 u64 rt_runtime;
8883};
8884
8885static int tg_schedulable(struct task_group *tg, void *data)
8886{
8887 struct rt_schedulable_data *d = data;
8888 struct task_group *child;
8889 unsigned long total, sum = 0;
8890 u64 period, runtime;
8891
8892 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8893 runtime = tg->rt_bandwidth.rt_runtime;
8894
8895 if (tg == d->tg) {
8896 period = d->rt_period;
8897 runtime = d->rt_runtime;
8898 }
8899
Peter Zijlstra4653f802008-09-23 15:33:44 +02008900 /*
8901 * Cannot have more runtime than the period.
8902 */
8903 if (runtime > period && runtime != RUNTIME_INF)
8904 return -EINVAL;
8905
8906 /*
8907 * Ensure we don't starve existing RT tasks.
8908 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008909 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
8910 return -EBUSY;
8911
8912 total = to_ratio(period, runtime);
8913
Peter Zijlstra4653f802008-09-23 15:33:44 +02008914 /*
8915 * Nobody can have more than the global setting allows.
8916 */
8917 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
8918 return -EINVAL;
8919
8920 /*
8921 * The sum of our children's runtime should not exceed our own.
8922 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008923 list_for_each_entry_rcu(child, &tg->children, siblings) {
8924 period = ktime_to_ns(child->rt_bandwidth.rt_period);
8925 runtime = child->rt_bandwidth.rt_runtime;
8926
8927 if (child == d->tg) {
8928 period = d->rt_period;
8929 runtime = d->rt_runtime;
8930 }
8931
8932 sum += to_ratio(period, runtime);
8933 }
8934
8935 if (sum > total)
8936 return -EINVAL;
8937
8938 return 0;
8939}
8940
8941static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8942{
8943 struct rt_schedulable_data data = {
8944 .tg = tg,
8945 .rt_period = period,
8946 .rt_runtime = runtime,
8947 };
8948
8949 return walk_tg_tree(tg_schedulable, tg_nop, &data);
8950}
8951
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008952static int tg_set_bandwidth(struct task_group *tg,
8953 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008954{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008955 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008956
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008957 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308958 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008959 err = __rt_schedulable(tg, rt_period, rt_runtime);
8960 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05308961 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008962
8963 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008964 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8965 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008966
8967 for_each_possible_cpu(i) {
8968 struct rt_rq *rt_rq = tg->rt_rq[i];
8969
8970 spin_lock(&rt_rq->rt_runtime_lock);
8971 rt_rq->rt_runtime = rt_runtime;
8972 spin_unlock(&rt_rq->rt_runtime_lock);
8973 }
8974 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008975 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308976 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008977 mutex_unlock(&rt_constraints_mutex);
8978
8979 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008980}
8981
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008982int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8983{
8984 u64 rt_runtime, rt_period;
8985
8986 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8987 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8988 if (rt_runtime_us < 0)
8989 rt_runtime = RUNTIME_INF;
8990
8991 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8992}
8993
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008994long sched_group_rt_runtime(struct task_group *tg)
8995{
8996 u64 rt_runtime_us;
8997
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008998 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008999 return -1;
9000
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009001 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009002 do_div(rt_runtime_us, NSEC_PER_USEC);
9003 return rt_runtime_us;
9004}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009005
9006int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
9007{
9008 u64 rt_runtime, rt_period;
9009
9010 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
9011 rt_runtime = tg->rt_bandwidth.rt_runtime;
9012
Raistlin619b0482008-06-26 18:54:09 +02009013 if (rt_period == 0)
9014 return -EINVAL;
9015
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009016 return tg_set_bandwidth(tg, rt_period, rt_runtime);
9017}
9018
9019long sched_group_rt_period(struct task_group *tg)
9020{
9021 u64 rt_period_us;
9022
9023 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
9024 do_div(rt_period_us, NSEC_PER_USEC);
9025 return rt_period_us;
9026}
9027
9028static int sched_rt_global_constraints(void)
9029{
Peter Zijlstra4653f802008-09-23 15:33:44 +02009030 u64 runtime, period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009031 int ret = 0;
9032
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07009033 if (sysctl_sched_rt_period <= 0)
9034 return -EINVAL;
9035
Peter Zijlstra4653f802008-09-23 15:33:44 +02009036 runtime = global_rt_runtime();
9037 period = global_rt_period();
9038
9039 /*
9040 * Sanity check on the sysctl variables.
9041 */
9042 if (runtime > period && runtime != RUNTIME_INF)
9043 return -EINVAL;
Peter Zijlstra10b612f2008-06-19 14:22:27 +02009044
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009045 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009046 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02009047 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009048 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009049 mutex_unlock(&rt_constraints_mutex);
9050
9051 return ret;
9052}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009053#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009054static int sched_rt_global_constraints(void)
9055{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009056 unsigned long flags;
9057 int i;
9058
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07009059 if (sysctl_sched_rt_period <= 0)
9060 return -EINVAL;
9061
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009062 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
9063 for_each_possible_cpu(i) {
9064 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
9065
9066 spin_lock(&rt_rq->rt_runtime_lock);
9067 rt_rq->rt_runtime = global_rt_runtime();
9068 spin_unlock(&rt_rq->rt_runtime_lock);
9069 }
9070 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
9071
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009072 return 0;
9073}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009074#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009075
9076int sched_rt_handler(struct ctl_table *table, int write,
9077 struct file *filp, void __user *buffer, size_t *lenp,
9078 loff_t *ppos)
9079{
9080 int ret;
9081 int old_period, old_runtime;
9082 static DEFINE_MUTEX(mutex);
9083
9084 mutex_lock(&mutex);
9085 old_period = sysctl_sched_rt_period;
9086 old_runtime = sysctl_sched_rt_runtime;
9087
9088 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
9089
9090 if (!ret && write) {
9091 ret = sched_rt_global_constraints();
9092 if (ret) {
9093 sysctl_sched_rt_period = old_period;
9094 sysctl_sched_rt_runtime = old_runtime;
9095 } else {
9096 def_rt_bandwidth.rt_runtime = global_rt_runtime();
9097 def_rt_bandwidth.rt_period =
9098 ns_to_ktime(global_rt_period());
9099 }
9100 }
9101 mutex_unlock(&mutex);
9102
9103 return ret;
9104}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009105
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009106#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009107
9108/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009109static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009110{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009111 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
9112 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009113}
9114
9115static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02009116cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009117{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009118 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009119
Paul Menage2b01dfe2007-10-24 18:23:50 +02009120 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009121 /* This is early initialization for the top cgroup */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009122 return &init_task_group.css;
9123 }
9124
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009125 parent = cgroup_tg(cgrp->parent);
9126 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009127 if (IS_ERR(tg))
9128 return ERR_PTR(-ENOMEM);
9129
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009130 return &tg->css;
9131}
9132
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009133static void
9134cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009135{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009136 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009137
9138 sched_destroy_group(tg);
9139}
9140
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009141static int
9142cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9143 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009144{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009145#ifdef CONFIG_RT_GROUP_SCHED
9146 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009147 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009148 return -EINVAL;
9149#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009150 /* We don't support RT-tasks being in separate groups */
9151 if (tsk->sched_class != &fair_sched_class)
9152 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009153#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009154
9155 return 0;
9156}
9157
9158static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009159cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009160 struct cgroup *old_cont, struct task_struct *tsk)
9161{
9162 sched_move_task(tsk);
9163}
9164
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009165#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009166static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009167 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009168{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009169 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009170}
9171
Paul Menagef4c753b2008-04-29 00:59:56 -07009172static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009173{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009174 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009175
9176 return (u64) tg->shares;
9177}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009178#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009179
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009180#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07009181static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009182 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009183{
Paul Menage06ecb272008-04-29 01:00:06 -07009184 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009185}
9186
Paul Menage06ecb272008-04-29 01:00:06 -07009187static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009188{
Paul Menage06ecb272008-04-29 01:00:06 -07009189 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009190}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009191
9192static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9193 u64 rt_period_us)
9194{
9195 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9196}
9197
9198static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9199{
9200 return sched_group_rt_period(cgroup_tg(cgrp));
9201}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009202#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009203
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009204static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009205#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009206 {
9207 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009208 .read_u64 = cpu_shares_read_u64,
9209 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009210 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009211#endif
9212#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009213 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009214 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009215 .read_s64 = cpu_rt_runtime_read,
9216 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009217 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009218 {
9219 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009220 .read_u64 = cpu_rt_period_read_uint,
9221 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009222 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009223#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009224};
9225
9226static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9227{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009228 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009229}
9230
9231struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009232 .name = "cpu",
9233 .create = cpu_cgroup_create,
9234 .destroy = cpu_cgroup_destroy,
9235 .can_attach = cpu_cgroup_can_attach,
9236 .attach = cpu_cgroup_attach,
9237 .populate = cpu_cgroup_populate,
9238 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009239 .early_init = 1,
9240};
9241
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009242#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009243
9244#ifdef CONFIG_CGROUP_CPUACCT
9245
9246/*
9247 * CPU accounting code for task groups.
9248 *
9249 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9250 * (balbir@in.ibm.com).
9251 */
9252
9253/* track cpu usage of a group of tasks */
9254struct cpuacct {
9255 struct cgroup_subsys_state css;
9256 /* cpuusage holds pointer to a u64-type object on every cpu */
9257 u64 *cpuusage;
9258};
9259
9260struct cgroup_subsys cpuacct_subsys;
9261
9262/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309263static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009264{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309265 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009266 struct cpuacct, css);
9267}
9268
9269/* return cpu accounting group to which this task belongs */
9270static inline struct cpuacct *task_ca(struct task_struct *tsk)
9271{
9272 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9273 struct cpuacct, css);
9274}
9275
9276/* create a new cpu accounting group */
9277static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309278 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009279{
9280 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9281
9282 if (!ca)
9283 return ERR_PTR(-ENOMEM);
9284
9285 ca->cpuusage = alloc_percpu(u64);
9286 if (!ca->cpuusage) {
9287 kfree(ca);
9288 return ERR_PTR(-ENOMEM);
9289 }
9290
9291 return &ca->css;
9292}
9293
9294/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009295static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309296cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009297{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309298 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009299
9300 free_percpu(ca->cpuusage);
9301 kfree(ca);
9302}
9303
9304/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309305static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009306{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309307 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009308 u64 totalcpuusage = 0;
9309 int i;
9310
9311 for_each_possible_cpu(i) {
9312 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9313
9314 /*
9315 * Take rq->lock to make 64-bit addition safe on 32-bit
9316 * platforms.
9317 */
9318 spin_lock_irq(&cpu_rq(i)->lock);
9319 totalcpuusage += *cpuusage;
9320 spin_unlock_irq(&cpu_rq(i)->lock);
9321 }
9322
9323 return totalcpuusage;
9324}
9325
Dhaval Giani0297b802008-02-29 10:02:44 +05309326static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9327 u64 reset)
9328{
9329 struct cpuacct *ca = cgroup_ca(cgrp);
9330 int err = 0;
9331 int i;
9332
9333 if (reset) {
9334 err = -EINVAL;
9335 goto out;
9336 }
9337
9338 for_each_possible_cpu(i) {
9339 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9340
9341 spin_lock_irq(&cpu_rq(i)->lock);
9342 *cpuusage = 0;
9343 spin_unlock_irq(&cpu_rq(i)->lock);
9344 }
9345out:
9346 return err;
9347}
9348
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009349static struct cftype files[] = {
9350 {
9351 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009352 .read_u64 = cpuusage_read,
9353 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009354 },
9355};
9356
Dhaval Giani32cd7562008-02-29 10:02:43 +05309357static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009358{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309359 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009360}
9361
9362/*
9363 * charge this task's execution time to its accounting group.
9364 *
9365 * called with rq->lock held.
9366 */
9367static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9368{
9369 struct cpuacct *ca;
9370
9371 if (!cpuacct_subsys.active)
9372 return;
9373
9374 ca = task_ca(tsk);
9375 if (ca) {
9376 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9377
9378 *cpuusage += cputime;
9379 }
9380}
9381
9382struct cgroup_subsys cpuacct_subsys = {
9383 .name = "cpuacct",
9384 .create = cpuacct_create,
9385 .destroy = cpuacct_destroy,
9386 .populate = cpuacct_populate,
9387 .subsys_id = cpuacct_subsys_id,
9388};
9389#endif /* CONFIG_CGROUP_CPUACCT */